Arduino 10: Servos

A servo is a special-purpose motor. Instead of spinning continuously, a servo is meant to rotate to a specific angle (usually only between 0 and 180 degrees) and hold there. Servos are typically used as actuators, to control the throttle on an engine or the angle of a flap on a model plane, for example. Typically servos have three wires: red to +5V power, black to ground, and a white or colored control line for the angle information, which we would run to a digital output on the Arduino.

The Arduino software comes with a servo library that makes it easy to handle servos (though note that software version 16 and older will only control a servo on pins 9 or 10). A library is a pre-made set of small programs that someone wrote, which you can use just as if they were commands built-in to the programming language. To make use of a library, you have to include it in your program right at the beginning. In the Arduino program editor, from the top menu select Sketch > Import Library, and from the drop-down menu select Servo. This inserts the line in your program

#include <Servo.h>

The command #include < > does just what it says, it includes all the code form a given library in your program.  The name of the library goes in the angle brackets < >. Library file names always end in .h, like Servo.h does.

Example: Connect a servo, which we’ll simply call s, to digital pin 9 on the Arduino, and connect the +5V and ground pins to the servo as appropriate. Upload the following program.

#include <Servo.h>
Servo s;
void setup(){
   s.attach(9);
   s.write(0);
}
void loop(){
   for (int angle=1; angle<180;angle++){
      s.write(angle);
      delay(10);
   }
   for (int angle=180;angle>0;angle--){
      s.write(angle);
      delay(10);
   }
}

There are three commands in the servo library that are most useful. First,

Servo name;

declares a variable name for your servo “object”. This goes at the beginning of the program with your other variable declarations. Next in the setup function, use

name.attach(pinNumber);

where name is the name you gave your servo, and pinNumber is the digital pin to which you attached the servo. This is sort of like declaring a pin mode to be output. Finally

name.write(angle);

turns the servo to an angle between 0 and 180 degrees.

For a second example, control the servo with a potentiometer. Keep the servo attached as before. Now connect the middle pin of a 10K potentiometer to analog pin 0 on the Arduino, one end pin to +5V, and the other end to ground. As an error check, the program prints out the raw reading from the potentiometer, which should vary between 0 and 1023 as you turn it. Upload the following program:

#include <Servo.h>
Servo s;
int reading = 0;
int angle = 0;
int potPin = 0;
void setup(){
   s.attach(9);
   s.write(0);
   Serial.begin(9600);
}
void loop(){
   reading = analogRead(potPin);
   angle = map(reading, 0, 1023, 0, 179);
   Serial.println(reading);
   s.write(angle);
   delay(10);
}

Note the use of the map function.

var1 = map(var2, fromLow, fromHigh, toLow, toHigh);

If you have some data in var2 that can vary in the range fromLow to fromHigh, map will proportionally map the value to a new range between toLow and tohigh and store the result in var1. In this case, the potentiometer returns a value from 0 to 1023, but we want to map that to the servo range of 0 to 179 degrees.

2 responses to “Arduino 10: Servos

  1. thanks i agree, very easy to read.

  2. Thank you for this tutorial; it’s one of the easiest to read and understand tutorials on using a servo motor with the arduino platform.

    Could you possibly explain how to control the speed of the rotation? I believe it has something to do with controlling the length of the PWM, but I’ve had a hard time finding any information on this. Thanks!

Leave a reply to Growing Cancel reply