Using a Servo in a Hobby Electronics Project
A servo is a position control mechanism that uses negative feedback to achieve positional accuracy. Small hobby servos of the type discussed here are commonly used to activate the steering and flight controls of radio controlled models.
You would use a servo if you need to accurately move a rotating mechanism to a particular angle. For example, you might mount a camera on a servo, so as to be able to rotate accurately to preset positions.
Another example that comes easily to mind is using a servo to control the air damper position of a wood furnace.
In hobby robotics, small servos are commonly used to actuate sensors such as sonar scanners.
Understand how a servo works
Numerous types of small hobby servos are available. They differ significantly in power requirements, power output (torque), rotational angle, and signal timing.
Once rotated to a particular angle, the servo will attempt to remain in this position as long as power and the control signal are supplied.
Hobby servos operate by converting a stream of electrical pulses of a particular duration or width, into an angle of rotation. The pulse width determines the rotational position of the servo and is on constantly.
Typically, the control signal consists of pulses occurring approximately 30 – 50 times a second. The width of these pulses varies according to where we want to position the servo. A “long” pulse of approximately 2500 microseconds duration will move the servo to its maximum rotational angle. A “short” pulse of about 200 microseconds will move it to its minimum angle. Pulses of intermediate duration result in rotational angles between the low and high limits.
Get the right kind of servo
One rather important criteria for a servo is, obviously, that it has to physically fit your project. Robot chassis and the like are often provided with an aperature for mounting a standard sized servo. Make sure you get one that fits.
You also need to get a servo with the appropriate angle of rotation for your project. The maximum angle of rotation is usually 180 degrees, corresponding to a half-circle but you will occasionally see 90 degree servos.
Servos which can rotate through 360 degrees are available but strictly speaking are not really servos at all. They don't hold a fixed position. They just rotate. The pulse width controls the speed and direction of rotation. These have their uses, though. Two of them will serve nicely as motors for a small robot.
One of the most important characteristics of a servo is whether you can obtain technical documentation for it, especially documentation regarding signal timing.
I use Futaba FP-S148 servos for most of my experiments. There's good data for these available on line.
Get timing information for your servo
Whether or not you can obtain accurate information on the pulse timing requirements of your servo, may make the difference between success and failure. There are numerous manufacturers of hobby servos but no standard for control signal timing.
This is rarely a problem for a radio control model application since the same manufacturer's radio receiver and servos are generally used together for a more or less plug and play solution. Using one with a computer is not so easy.
The Arduino servo library defaults to a pulse width range of 540 microseconds for 0 degrees of rotation up to 2400 microseconds for 180 degrees.
Chances are this is not what you need. Using incorrect timing parameters will usually cause the servo to provide less than its full range of rotation and cause over or under-range rotation, which can be hard on the servo mechanicals.
This information is not easy to find. You have some Googling to do.
For the Futaba FP-S148 servos, I found a range of 220 to 2200 microseconds was just about perfect. Hitec brand servos, on the other hand, use a range of 900 to 2100 microseconds.
If all else fails, you may have to experiment with the min/max pulse width parameters to the Servo.attach() method (see below).
Use of a servo in an Arduino environment
Servos use more power than most things people typically connect to microcontrollers. An Arduino can safely power one or two from its built-in regulator but for more than that you will need to provide them with their own power source.
A hobby servo is designed to receive its control signal from a small radio receiver. To use it with a computer requires a means of generating the control signal. In the Arduino, that is handled by “Servo.h”, the servo driver library.
Here's the basic code for using a servo in an Arduino sketch:
...and by the lines:
This causes the servo library to be included in the sketch. The <Servo.h> syntax is used rather than "Servo.h" because Servo.h is a "system" library rather than a local one.
This line defines the output pin number the servo is connected to. In this case, pin 4. This line isn't exactly necessary as long as we remember the pin number but it's handy and might avoid an error as the program becomes more complicated.
Here we're defining an instance of the Servo class called "servo1". This is the servo object that we shall use in our code.
On into the setup code. The attach method of the servo object associates the object with a particular output pin and optionally specifies the timing parameters. In this case we have the servo connected to pin 4 (remember that servoPin is defined to be 4). We are using 220 microseconds as our minimum pulse width, which corresponds to 0 degrees of rotation. We are using 2200 microseconds as the maximum pulse width, which corresponds to 180 degrees of rotation.
This is an example of how to actually move the servo. These three lines do a little power-up test by rotating the servo to the 45 degree position, pausing for 750 milliseconds, and then moving the servo to 90 degrees.
Considerations for coding and project design
You can connect a servo to any digital output pin on an Arduino device. It doesn't have to be a PWM-capable pin.
The servo library uses interrupts to generate the control signal. Interrupts use timers. In case you can't guess, using the servo library gives rise to another confusing potential timer conflict case.
An Arduino UNO can drive 12 servos. A Mega 2560 can drive 48. One timer is used for each group of 1-12 servos you connect to the device. As each timer is allocated to the servo library, it ceases to become available for other purposes, and PWM capability will be disabled on the pins that depend on that timer.
In the case of a UNO with 1 to 12 servos attached, this means that analogWrite() will be disabled on pins 9 and 10.
For a Mega 2560, the first group of 1 to 12 servos disables analogWrite() on pins 44,45 and 46. If you attach yet more servos to the device, the next group of 1 to 12 will disable pins 11,12 in addition to 44,45 and 46.
This link has more information on that.
Being a largely mechanical device, a servo requires time to do its thing. Rotating between zero and 180 degrees takes most hobby servos about 1.5 seconds. You need to allow for this in your code by adding a small time delay after each call to Servo.write().
Servos are relatively power hungry devices, especially when they first start to move. If you don't have enough reserve power available from your project's power supply when the servo moves, you might experience glitches caused by the supply voltage momentarily dropping.
This can be prevented in many cases by connecting a capacitor of 10uF or so between the servo's power supply line and ground. The value of the capacitor isn't critical as long as it's in the range of approximately 10 - 40uF.
You need to be cognizant of the fact that the servo library is generating interrupts all the time when your project is running. If you turn off interrupts in your code - maybe during one of your own interrupt service routines, expect side effects with the servo.
Bruce Grant's Web Page is licensed under a Creative Commons Attribution 4.0 International License.
Based on a work at http://www.wbga.ca..