| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

Controlling Servos

Page history last edited by Jonathan Dietz 7 years, 7 months ago

 

Video: MakerShed: Controlling Servos

 

Controlling Parallax Standard and Continuous Rotation Servos

 

http://learn.parallax.com/KickStart/900-00005

 

 

 

// Sweep
// by BARRAGAN <
http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}


void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

-----------------------------------------------------------------------------------------------------

 

 

Code Examples:

 

include <Servo.h> // Use Servo library, included with IDE

Servo myServo; // Create Servo object to control the servo

void setup() {
myServo.attach(9); // Servo is connected to digital pin 9
}

void loop() {
myServo.write(180); // Rotate servo counter clockwise
delay(2000); // Wait 2 seconds
myServo.write(0); // Rotate servo clockwise
delay(2000);
myServo.write(90); // Rotate servo to center
delay(2000);
}

 

// Try playing with the servo write positions if you have a continuous rotation servo!

 

 

 

 

What It Can Do

 

  • Moves to any position between 0 and 180 degrees
  • Keeps its position as long as power is applied
  • Motorizes most anything, such as a robot or mechanical puppet

 

Servo motors are made for hobby radio-control tasks, like model airplanes and model racing cars. They’re also very popular in such things as robotics and animatronics.

R/C servos are made to spin in a limited circle, no more than about 180°. You set the precise position of the output shaft of the servo motor by using a timing signal; this signal is provided by a microcontroller. Once the signal is applied, the motor moves to that position, and stays there.

 

Important! Servo motors draw only minimal current when they’re not attached to a mechanical load, such as the leg of a robot. The example connection diagrams shown here provide only modest current to the servo, so they are not intended to move heavy loads. They’re useful for demonstration only.

To operate bigger loads the servo motor should be connected to its own 4.8 V to 6 V battery supply. See the links below for information on how to use separate power to operate one or more R/C servo motors.

  

 --------------------------------------------------------------------------------

 

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
 
int potpin = 0;  // analog pin 0 used to connect the potentiometer- other potentiometer pins go to +V and GND
int val;    // variable to read the value from the analog pin
 
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

 

// VAL  could be a sensor reading

------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Comments (0)

You don't have permission to comment on this page.