Servo How To

A servo is a motor that can rotate in 180 degrees by sending a pulse every 20 microseconds. For a detailed description of servos, check out the servo tutorial put together by the Seattle Robotics Club. There are three wires on a servo the most common colors are: black, red, and yellow. To hook it up to Arduino, attach the black wire to the ground (GND or negative), the red wire into +6V (or +5V, the positive voltage), and the yellow wire to a pin with PWM (Pulse Width Modulation) with a 220 Ohm resistor. Sometimes there is a white wire instead of a yellow wire.

It is important to note that this servo works on digital signals (on vs. off) rather than analog which is why it has to be in PWM.

This is a basic example of arduino programming (check this for more information on how servos work with arduino) that allows the servo to move from 50 degrees to 120 degrees and back:

int servoPin = 3; //Connects the yellow wire to pin 3

int myAngle; //This sets the variable for your servo angle

int pulseWidth; // servoPulse function variable

void setup ()

{

pinMode(servoPin, OUTPUT); //sets pin 3 as output

}

void servoPulse(int servoPin, int myAngle)

{

pulseWidth = (myAngle * 10) + 600; //This Procedure translates myAngle to the correct delay in order to make

digitalWrite(servoPin, HIGH); //the servo move to the correct set angle.

delayMicroseconds(pulseWidth);

digitalWrite(servoPin, LOW);

}

void loop()

{

for (myAngle=50; myAngle<=120; myAngle++) //The angle starts at 50 degrees and increases by increments

//of one until it reaches 120

{

servoPulse(servoPin, myAngle); //Calls the servoPulse Procedure and sets pin and angle

delay(20); //refreshes cycle

}

for (myAngle=120; myAngle>=50; myAngle--) //The angle starts at 120 degrees and decreases by increments

//of one until it reaches 50

{

servoPulse(servoPin, myAngle); //sets pin and angle

delay(20); //refreshes cycle

}

}

// Code sample adapted from the Arduino Notebook.

USING A PUSH BUTTON TO INITIALIZE SERVO ROTATION

Essentially, this uses a lot of the same programing as the above example. However, you have to incorporate an "if...else" loop. In the following example, the servo is programed to return to its initial position after the push button is released.

int servoPin = 3;

int myAngle;

int pulseWidth;

int button = 2; //sets the button to pin2

int val = 0; //sets the value to 0

void setup ()

{

pinMode(servoPin, OUTPUT);

pinMode(button, INPUT); //sets the push button as an input

Serial.begin (9600); //sets serial to 9600 bits per second

}

void servoPulse(int servoPin, int myAngle)

{

pulseWidth = (myAngle * 10) + 600;

digitalWrite(servoPin, HIGH);

delayMicroseconds(pulseWidth);

digitalWrite(servoPin, LOW);

}

void loop()

{

val = digitalRead (button); //sets the value to the state of the button

if (val==HIGH){ //if the push button is pressed proceed with the for loop

for (myAngle = 30; myAngle <= 150; myAngle++)

{

val = digitalRead (button); //Reads the button between every angle change

if (val==LOW) //if the button is released, the angle goes back to 30

{

myAngle = 30;

}

servoPulse (servoPin, myAngle);

delay (20);

}

for (myAngle = 150; myAngle >=30; myAngle--) //If the button is not released, the loop returns to 30 and continues

{

servoPulse (servoPin, myAngle);

delay (20);

}

}

}