Servo Tutorial

Next we need to learn a little bit about servos. Easy Arduino Servo tutorial!

A servo is a kind of mechanical actuator, there are a couple different kinds of servos, continuous rotation servos and non-continuous rotation servos, the difference is that non-continuous servos won’t spin around and around like a motor, rather will typically go 1 rotation or less, they also don’t spin terribly fast but they have quite a lot more torque than small motors of similar size. Servos are used commonly in radio controlled airplanes, and robots. Here is the one we will use, it is a 180 degree rotation servo.

Servos typically only work as outputs, you write some code in your sketch to control the servo by specifying the position you want it to move to. For this project, we also want to get input from the Servos, that is we want to be able to read the current position of the servo. The servos used in this project were not intended to be used in this way, so we will need to hack them. We’ll get to that part later, for now we just want to learn about controlling a servo.

Using servos with an Arduino Due is incredibly easy, it’s even easier than using the LCD. The Arduino IDE comes preloaded with a servo library and a couple of examples. You first need to connect your servo, the black wire is connected to GND, the red wire to 5V, and the white wire to port 9.

Lets start with one of the examples included in with the Arduino software. You can get to this by clicking File->Examples->Servo->Sweep. The following code should get loaded.

// 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 voidsetup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
voidloop()
{
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
}
}

Using a Servo is about as straight forward as the LCD, we include the library

#include <Servo.h>

Create a Servo Object

Servo myservo; // create servo object to control a servo

associate that object with a port number

myservo.attach(9); // attaches the servo on pin 9 to the servo object ,

then all we need to do is call myservo.write(pos); with a value between 0 and 180.

myservo.write(pos);

I always try and force myself to play with the examples a little bit, change or tweak something. Lets write our own little program will simulate the motion of the sprinkler head like you use to water your yard, we won’t actually hook anything up to the servo, just rotate through 180° very quickly one direction and then we’ll go back the other direction and short discrete steps. The sweep example contains all the code we need do this, we just need a few changes to the parameters. We’ll decrement by 10 degrees, and delay for 150 mS.

The nice thing about using a library is that you don’t actually have to learn about any of the internals of how the servos are actually controlled, but later on when we actually get to building our theremin we actually do need to know a little bit about the servo internals. Nearly all servos are controlled the same way and that is you need to send square wave pulses to them, and the length the pulse is how you communicate to the servo the position you want to move to. The length of these pulses is typically something like half a millisecond to 1.5 ms. Typically the way a servo is controlled is to send a continuous stream of pulses, one pulse every 20 mS. If you want to hold the position then you just keep sending the same length of pulse every 20 mS. It’s important to understand that you would not do this using one of the PWM outputs on your Arduino. A PWM signal is varying a duty cycle, that is the amount of time a signal is high versus the time signal is low so if you wanted to send a 1.5 ms pulse using PWM you could set the duty cycle such you get a 1.5 ms pulse. If the duty cycle is 50% you’ll get a 1.5 ms of high than 1.5 ms of low than 1.5 ms of high than 1.5 ms of low, but this is not what you want to control a servo. With a servo you would want 1.5 ms high than 18.5 ms of low so that the rising edge of to pulses was 20 ms apart. We can write a simple program to demonstrate this, but you don’t want to actually control servos like this, use the library.