This EasyStep will show you the basics of driving an RC (hobby) servo motor off an analog (PWM) output.
RC servos are so called because they originated as the actuators in Radio Controlled model aircraft. Early radio systems would use a number of tone frequencies, one per control function, and transmit timed pulses of tone to the aircraft. In the aircraft a number of separate tone detector circuits (often based on LM567 chips) would separate out the pulses and feed them to servo mechanisms designed to respond to pulse width.
What you will need
- An EC1 all ready to plug into your PC via USB;
- Firmware V4.7 or later in the EC1;
- SPLat/PC installed and running, and the basic skills to enter and download programs;
- A hobby servo. I did this with a Power Pro Micro Servo 9g, which cost me the princely sum of $3 each (here);
- (optional) A 4.5 to 6V power supply.
Basic servo theory
The video clip shows how pulse width affects the position. The program for this is given at the end of this EasyStep
An RC servo is a small motorised device that will rotate to a position determined by an input signal. It has 3 wires. 2 wires are for power (typically 4.5V to 6V, corresponding to 3 or 4 typical primary cells). The third wire is for the position control signal. The signal is a train of pulses. The width (duration) of the pulses determines the angular position the servo will seek. The repetition frequency of the pulses is generally 50Hz, i.e. one pulse every 20mS. The pulse width can range between 0.5mS and 2.5mS for the servo I used. Other servos may use other pulse widths. An often quoted figure is 1mS to 2mS (1.5mS±0.5mS) for 90° rotation. The servo I used has 180° rotation, so its 1.5mS±1mS makes sense.
|Sketchily drawn waveforms with two different pulse widths|
Expect unit to unit variations in the angle versus pulse width behaviour — these are very cheap and you can't expect them to be very consistent!
Note that it is the width (duration, in mS) of the pulses that matters, not the duty cycle (percentage of high time vs total period). The SPLat fAnOut instruction uses duty cycle, so we have to calculate the duty cycle that will give the required pulse width.
Connecting the servo
The servo has 3 wires. The colours can vary between brands. There will generally be a red wire, which is the positive supply voltage. The 0V supply (ground, negative) will generally be an "earth" colour, like brown (as on mine) or black. The control signal is typically yellow. There is a very good guide here.
To make it easy to connect to the EC1, I replaced the 3-pin plastic housing that came on the servo with three separate 1-pin housings taken from M-F jumper leads. In both cases the metal insert was held in place by a fine plastic tab. I used a pin to gently lift the tab and slip the metal contact inserts out.
The photo below shows the connections to the EC1. For a single servo the power for the servo (red wire) can be taken out of a +5V power out pin on the EC1. The servo ground (0V, negative) wire (brown on my servos) goes to a 0V pin on the EC1. For this EasyStep the PWM control signal (yellow wire) comes from the Analog Out 0 pin of the EC1. The "analog outputs" of the EC1 are actually pulse width modulated (PWM) digital outputs. Click here for more details on that.
The simplest driver program
The following program will drive the servo back and forth across its full 180° range. You can modify the end points by changing the target pulse widths (given in mS)
The duty cycle figures are calculated as:
fAnOutValue = Required Pulse width in mS * PWM base frequency / 1000
Say I want a 1.5mS pulse (about mid-range)fAnOutValue = 1.5 * 50 / 1000 = 0.075
Feel free to edit the numbers 0.025 and 0.125 for other target servo positions.
More than one servo (or a bigger servo)
It is only possible to draw 400-500mA out of the 5V power out pins on the EC1. We deliberately include a 600mA current limiter in the EC1, to give your computer some measure of protection against bad things happening to the EC1. There is enough capacity there for one small servo, but for more servos or bigger servos you need an separate 5V (or so) power supply.
The following diagram shows the connections for an external power supply for large or multiple servos. The dotted red line shows how you would supply power to the EC1 from the same power supply only if you are not powering it from your computer via the USB cable.
Note: Do not allow an external power supply to power the EC1 while it is plugged into your computer. The result of doing that could be serious damage to the computer.
The YouTube demo program
The program used in the YouTube clip at the top of this page is more elaborate. You can use it to study a few programming techniques.