Servos

[Servo]

A servo is a box with a little motor inside. You can tell it how many degrees you want it to rotate, from 0 degrees to 180 degrees. (They're not very precise, so it might overshoot or undershoot a bit.)

You can drive two servos from the Arduino at once. Ideally you should use a separate battery pack to drive the servo, but sometimes you can get away without doing that. For more than one or two servos you definitely need a separate battery pack.

Wiring your servo

[JR servo connector] The servos we have are for model airplanes and have a "JR" plug that's wired like the picture at right.

The Red wire goes to the Arduino's 5v plug; the Brown wire goes to Gnd. The Orange wire is your signal wire; it can go on any of the digital pins. (Most of the examples online start with pin 9, but any pin from 2 on up is fine.)

The end of the servo connector is like a breadboard -- you can stick a wire right into the hole at the end of the connector, then plug the other end of the wire into your breadboard or your Arduino.

If you add a second servo, the power and ground are the same (use your breadboard since the Arduino only has one 5v plug) but the signal wires will be different -- e.g. pins 7 and 8, or pins 9 and 10.

Programming a Servo

[Servo, wired up to Arduino]

To talk to a servo you need the Servo library. There are some examples to get you started in the menu File->Examples->Servo (maybe start with Sweep).
Here's what the code looks like:

#include <Servo.h>
 
Servo myservo;  // Create a servo object
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
    myservo.attach(9); // Use pin 9 for the servo
}

void loop()
{
    for (pos = 0; pos < 180; pos += 1)
    {
        myservo.write(pos);
        delay(15);           // wait 15 ms
    }
    for (pos = 180; pos>=1; pos-=1)
    {                                
        myservo.write(pos);
        delay(15);
    }
}