Ottawa Robotics Enthusiasts

Controlling Servos - By Aaron Ramsey

Overview

Servos are a very handy resource for people involved in robotics. Servos are basically a small geared motor with a controller. You send an electrical pulse to the servo to tell it where to turn to. Normally a servo can only turn in a 120 to 180 degree range and is used for positioning, but a servo can be taken apart and modified so that it turns the full 360 degrees, esentially creating a geared motor. This article is about how to position the servo using pulses, not how to hack the servos.

What good it it?

Servos are normally used by hobbiests to position rudders, flaps, etc.. on hobby airplanes. What can we use them for? Well, as I describe in my GP2D02 article, servos can be used to sweep sensors arrays back and forth. They can also be used to create legs on a robot. There are many other uses. They come in all different sizes, from less than an inch in length to several inches long. The larger ones are very strong and can be used to move very large loads.

Alright, how do we use it?

A servo has three wires. A signal line, power line and ground line. Different manufacturers have a different orders and colours for the wires, but they are fairly easy to identify which is which. Red and black wires are the power wires, and the third is the signal line. I have servos with either orange or white signal lines. You can test a servo by connecting +5V and GND to the appropriate wires, then just tapping the control wire on +5V quickly. This simulates the control pulse, and the servo will move left or right.

The control pulse?

A servo is controlled by a series of pulses with certain high and low times. The high time controls the position of the servo. A typical servo accepts a 1 ms to 2 ms high pulse in order to position itself over a 110 degree sweep. The low time can vary from 10 ms to 20 ms, and plays no role in the position of the servo. The control signals are linear, so that a pulse of 1.5ms will position the servo very close to centre. Some brands of servos will accept pulses lower than 1ms amd higher than 2ms. This lets you position the servo over a greater angle. I've had servos which can sweep 180 degree with servo pulses ranging from 0.7ms to 2.4ms. Just how far you can sweep a servo is determined by experimental means. Just be sure to gradually push the servo rather than feeding it extreme values to see how far it can go. You can physically damage the servo gears by trying to move it too far in one direction or the other. You'll know when you are going too far by the sounds that the servo makes. <grin> Below is a sample servo pulse.

Servo Pulse
Servo Pulse Width

A typical servo that I use is a Cirrus CS-70 Standard Pro Servo. It is rated 0.15 sec/60 degrees at 4.8 volts and 0.12 sec/60 degrees at 6.0 volts. It is powered by 5 volts. In order to position it accurately, I actually need to give it 6 pulses, each around 12 ms long. Usually 2 pulses are enough to get the servo in the general position that I want it to be, and the 4 additonal pulses are just fine tuning. If accuracy is not important to your application, you can cut back on the number of pulses that you send. If you are sweeping the servo over a great distance, you need to send it more pulses.

Something to note is that the servo is only powered when you are actually sending it a pulse. When it is not receiving pulses, the motor will not hold the servo arm in place. The gearing helps to hold it in place, but if you are creating a walker which needs the servo to physically hold it off the floor, you need to continuously send pulses to the servo. If you are just sweeping a sensor back and forth, you only need to send the pulses while you are moving the servo.

So, how do I generate that pulse?

Below, I present some C code for the CCS compiler for the PIC processors. The code is fairly readable, even though I use some functions specific to the CCS compiler. You should be able to easily move this code to another compiler or language. Basically I loop 6 times. Inside the loop I output a high on pin B0 of the PIC, delay for 1.5ms, output a low on pin B0, and delay for 10ms. It's really that easy!

Sample Servo Code

#define SERVO_CONTROL   pin_B0

// Move the servo to the centre
// send 6 pulses to make sure it gets there accurately
for (counter2=0;counter2<6;counter2++) {
	output_high(SERVO_CONTROL); // high pulse
	delay_us(1500);  	    // 1500us = 1.5ms
	output_low(SERVO_CONTROL);  // low pulse
	delay_ms(10);
}

You don't need a microprocessor to control the servo. You can easily build a 555 timer circuit or other circuits which will do the same thing as above. All you need is something that can output pulses.

Conclusion

Well, hopefully by now you understand how to use a servo. They are really easy to control, and very useful for many things. Now, if only I could justify to my wife buying 12 of them to build a 6 legged walker. <grin>


July 21st 2000