These devices are among the basic building blocks of robots. Combine and coordinate their movements in a suitable mechanical assembly, and a group of servomotors can provide motion and locomotion for systems controlled locally or remotely—from classroom projects to sophisticated robotic designs. The limits are your imagination and willingness to learn.
“Give me a place to stand, and I will move the world,” Archimedes of Syracuse is said to have declared, perhaps carried away by his discovery of the law of the lever. That idea leads us to simple machines—and the device we’ll explore and put to work here is a fine example.
A servomotor is essentially a mechanical actuator built around a motor and a set of gears that multiply the torque at the output. Its control system continuously monitors the position of a mechanical part connected to the outside world. In other words, an electronic command produces a controlled mechanical response. The motors and paper-feed or reverse mechanisms in a printer, for example, form part of a servosystem.
Servos are especially common in aeromodelling and robotics, but their uses go well beyond those fields. Any system that needs precise, controlled mechanical positioning can rely on a servosystem or servomechanism driven by a servomotor. Camera zoom, optical autofocus, surveillance-camera positioning, and even automatic elevator doors are familiar examples.
The motor itself spins quickly. A reduction gearbox slows it down while converting that speed into useful force. The final gear turns the output shaft much more slowly, even though the motor inside is still rotating at high speed.
Stops on the final gear limit travel, typically giving the output arm a 180° range. In most servomotors, a potentiometer mechanically linked to the output shaft tracks this angular movement. The resistive sensor reports the actuator’s position to the control system, which can then check that the requested position has been reached accurately.
Wiring a servo
Connections are simple and generally follow a basic color convention: red is the main positive supply; black or brown is ground; and yellow or white carries the position-control signal. The first image shows the red, black, and yellow leads used on HITEC servomotors.
Servos usually operate from 3 to 7 volts. 5 volts is common in fixed installations powered from a mains-connected supply, while 6 volts is often used for battery-powered mobile equipment. In every case, the control signal needs an amplitude of 5 volts.
The servo control signal
Servomotors are controlled by pulses of variable width, which must be refreshed periodically. If the control signal stops while the servo needs it, the energized motor will no longer hold its set position and may move under the load acting on it. A lever arm could drop the object it is holding; an upright assembly could topple forward or backward once the servo stops supporting it.
To hold a servo at a chosen position, keep sending the corresponding signal. The control system then remains active, preserving the position and resisting external forces that might otherwise move the mechanism.
The signal is commonly refreshed 50 times per second. Refresh intervals between 10 and 30 milliseconds are also normal and effective, as the diagram shows. Pulse width determines the actuator’s position or orientation. Sending precisely timed pulses continuously—without unwanted mechanical oscillation—is the key challenge when working with servomotors.
A microcontroller may need to perform other tasks as well as controlling a servo, raising the concern that it might miss the required timing. A 20 MHz high-frequency crystal helps it complete additional work before the next control pulse is due. You must also study the timing diagram for your particular application carefully.
Moving a servo with BASIC
Control is straightforward in this language: write SERVO Pin, Position and the servo moves to the specified orientation. Pin is the microcontroller’s digital I/O pin connected to the servo’s control lead. Position is a WORD variable; typical values are 1500 for the center, 500 for the starting limit, and 2500 for the far end of travel.
Here is an illustrative program listing:
With a simple program like this, you can control surveillance cameras, fluid valves, or model railway points, among countless other devices. Increase or decrease the POS variable using a button to move in either direction while staying within the limits above. A third button can quickly return the actuator to its center position.
The demonstration videos show the SERVO instruction in action. One obvious improvement is to speed up movement: instead of changing the value one unit at a time with INC and DEC, use a larger step such as POS = POS + 5.
Start building a camera surveillance system
Two servomotors can provide a much wider range of motion. Arrange them mechanically so that their combined movement turns a linear path into motion in two dimensions.
The lower, or azimuth, servo is fixed to a base and moves a small L-shaped piece of aluminium. It provides movement on the Cartesian x-axis, panning 180° to the left and right.
Above it, a straight U-shaped section supports the lever arm of the second servo. This arrangement moves the assembly on the Cartesian y-axis, adjusting its height—also called elevation or tilt. A notable feature is that the second servo’s body moves to produce the motion, while its actuator arm remains screwed firmly to the lower structure.
To see the range and complexity of this setup, and how it could be used in a surveillance-camera system or for simple observation, watch the video. It shows the moving mechanism and may give you ideas for your own mechanical designs.
Coordinate the servo actuators and you may come up with an even better setup—or something more ambitious, such as robotic arms, joints that let a bipedal android walk, or a hexapod. Programming a gait is a rewarding challenge: several servos may need to coordinate the legs, making sure none trips over another as the “insect” moves from place to place like a real one.
Programming servos with BASIC
For this project, we’ll use a topic covered before: UHF radio modules, which let us operate the system remotely and at greater distance. This capability greatly expands the possibilities of this branch of electronics, also known as mechatronics.
Operating electromechanical systems remotely is a first step toward more complex applications, such as handling radioactive material inside a nuclear reactor. Servomechanisms operated wirelessly can work wherever people cannot safely reach. Such systems require far more complex mechanisms and precise movement than the example here, but their basic principles are the same.
At the top of the program, specify the device and clock frequency. This example uses a 16F628A running at 20 MHz, suitable for the BASIC compiler used here to program microcontrollers, Proton Lite. Define variables for the position of each servo; for clarity, we’ll call them GIRO and ALTO.
GIRO sets the lower servo’s position and movement along the x-axis. ALTO controls the elevation servo, which moves the webcam along the y-axis. Next, define the microcontroller pins that send variable-width pulses to the servos. We’ll call them AZIMUT and ALTURA to make their roles clear.
Set the microcontroller to digital mode with ALL_DIGITAL, disabling the comparators on the 16F628A. The equivalent instruction is CMCON = 7; both forms have the same effect.
Now set the variables’ initial values and configure the microcontroller port pins. A value of 1500 centers a servo, so both actuators start at the middle of their range when the system is powered up. Use TRIS instructions to set pins as inputs or outputs. In both ports, the first three pins are inputs and the remaining pins are outputs.
The main loop, MAIN, handles movement and reads the input pins. Pin 1 of port A returns the system to its initial, or RESET, position by setting the pulse-width variables to 1500. The other inputs increase or decrease those variables by two rather than one: GIRO = GIRO + 2 and GIRO = GIRO - 2. Their values determine the servos’ movement and chosen positions.
Many NeoTeo readers are probably already thinking about extracting data from the webcam’s bitmap image to build a Sentry-Gun—I can imagine it! Improve the system’s response speed and this simple webcam-orientation rig could become whatever you want. How about a celestial-object tracker for a telescope, or a steering system for model aircraft? The possibilities are limited only by your imagination.
Connecting the UHF module
As in our earlier experiments with LEDs and UHF radio modules, we’ll use the same general program approach for the 12F675. This time, instead of switching LEDs, each high logic state at its output activates an NPN transistor, as shown in the circuit.
A high state at a 12F675 output makes the corresponding transistor’s collector go low. For the 16F628A that controls the servos, this is equivalent to a pushbutton connecting the input to ground. When the 12F675 output is low, the transistor does not conduct and its collector goes high. Each input pin on the 16F628A therefore behaves as though it had a pushbutton.
The diagram shows just one transistor; use as many as the number of buttons you want to emulate. The transmitter remains unchanged from the previous article. Power the receiver module and 12F675 from the same 5 volts as the 16F628A. Servomotor noise can make the system unstable, so filter the receiver and PIC supplies carefully. Place 100-nanofarad (100 nF) ceramic capacitors as close as possible to the microcontrollers. A couple of 100 μF electrolytic capacitors distributed around the circuit will also help.
Want to see it working with a webcam? Here are the demonstrations.
We’d love to hear what applications you can imagine for a system like this. Let your imagination run and share your ideas with us. Perhaps we’ll get excited enough to help you develop one. What do you think?