Basic PICAXE Servo Interfacing

Servos are clever little devices that are used to precisely control movement. They are made up of a motor, gearbox, and controller circuitry. In response to a control signal, the output shaft of the servo will rotate to a specific position. Servos are found in a number of hobby applications such as the steering mechanisms of RC (radio control) cars, the flaps of RC planes, and the arms and legs of robotics projects.

Servos are so common in these hobby applications that a loose, informal standard has evolved. Although there are still many variations among the different manufacturers, what this informal standardization means is that there exists a class of device commonly referred to as RC or hobby servos that are reasonably interchangeable and reasonably easy to interface with the PICAXE and other microcontrollers. In fact, the PICAXE has two commands - SERVO and SERVOPOS - specifically for controlling hobby servos.

Some of the major servo manufacturers include Futaba, Hitec, JR Radios, Airtronics, and Tower Pro.

You don't need to become an expert in how servos work in order to use them in your projects, but a general understanding is helpful. The shaft of the motor inside the servo mechanism is attached to a potentiometer (a variable resistor). The controller circuitry within the servo is configured in a feedback loop with the potentiometer. As the motor turns, the resistance of the potentiometer changes, and when the resistance reaches its target state (the correct rotation of the shaft) then the motor will stop rotating. Servos cannot continuously rotate due to both the feedback control mechanism and the construction of the gearing. The range of motion is generally somewhere between 180 and 210.

In addition to the device itself, servos are usually shipped with a small assortment of servo horns (also called servo arms). These attachments slide onto the output shaft of the servo which is called the spline. Horns provide a method of attaching the servo to whatever it is you plan to move. In robotics, arms and legs are often glued or bolted directly to the horn. In planes and cars you will more commonly find the object to be moved attached via a push rod. Although you can occasionally mix and match parts, horns tend to be fairly specific to a servo manufacturer, and sometimes will only fit a particular servo model.

Because the servo is essentially self-contained, interfacing with the PICAXE is very simple. The typical servo has only three wires: positive voltage; ground; and signal. The color-coding of these wires varies. Positive voltage is almost always red, ground is either black or brown, and the signal is whatever is left - white, orange and yellow all being common. The signal wire is connected to an output pin on the PICAXE via a 330 ohm resistor. The other two wires connect to the power supply.

Hobby servos tend to run on fairly low voltages. A range of 2.5 to 6 volts would be common. The manual recommends a separate power supply for the servos, but on smaller projects this is not always necessary. The reason for the dual power supplies is that the servos generate electrical noise which can lead to erratic behavior in the circuit. If your project suffers from symptoms such as frequent resetting of the PICAXE, or servos jittering instead of finding the proper position and holding it, noise is the likely culprit. Often, this electrical noise can be surpressed by using capacitors across the power rails as close to the source of the noise as practical. If this method of noise suppression fails then you will need to use a separate power supply - but don't forget to share a common ground.

The position of the servo shaft is controlled using a technique called pulse width modulation (PWM). Using PWM, a control device such as the PICAXE sends an electronic signal, and the duration of the signal (its pulse width) determines the angle of rotation in the servo shaft. A pulse of 0.75 milliseconds could, for example, set the servo to 0 and a pulse of 2.25 milliseconds could set the servo to 180.

The actual timing of the pulse widths required to achieve specific angles will vary somewhat among manufacturers, and you will likely need to experiment a bit to determine the required signals for your servo. The PICAXE manual presents the typical range of pulse widths for servo control as being between 0.75 and 2.25 milliseconds and this is often true. However, a range of between 1.0 and 2.0 milliseconds is also frequently used. One end of the range would send the servo to the extreme counter-clockwise position while the other end of the range would send the servo to the extreme clockwise position. 1.5 milliseconds is almost always the neutral, center position.

PICAXE BASIC provides two commands for controlling servos. The SERVO command is used to initialize the servo and takes the syntax:

  SERVO pin, pulse
  SERVO [preload], pin, pulse

The optional preload parameter is only available on X2 parts and is used to provide a timing constant. In order to hold the position the timing pulse must be refreshed periodically. The refresh rate on the non-X2 parts, and the default on the X2 parts, is 20 milliseconds. The pin parameter specifies the output pin to send the signal to, and the pulse parameter is a value between 75 and 225 which specifies the pulse width. Note that the pulse width is in 10 microsecond increments so that in order to use millisecond values you multiply by 100: for a 1 millisecond pulse enter a value of 100 as the parameter.

Once the SERVO command has been used to initiate the servo, the SERVOPOS command is used to reposition when necessary using the syntax:

  SERVOPOS pin, pulse

A simple program example might be:

    servo 4, 150      ; start servo on pin four and move to center

      pause 500       ; wait a half second
    servopos 4, 200   ; move servo clockwise
      pause 500       ; wait a half second
    servopos 4, 150   ; move servo to center
      pause 500       ; wait a half second
    servopos 4, 100   ; move servo counter-clockwise
      pause 500       ; wait a half second
    servopos 4, 150   ; move servo to center

  goto main           ; return to main program loop

In general you can run up to 8 servos from a single PICAXE (provided it has 8 ports). This limitation is a fundamental function of the timing required to maintain pulses. In order to hold position, the commands start a pulse which continues until another SERVOPOS or a HIGH or LOW command is issued. If each servo requires a pulse of up to 2.25 milliseconds every 20 milliseconds, then by the time you have issued 8 pulses it's time to start again. Note that if you reduce the delay on an X2 part you will reduce the number of servos you can control - a 10 millisecond delay will only enable the support of 4 servos.

The nature of the SERVO and SERVOPOS commands makes them very demanding on the PICAXE and special considerations must be made. Maintaining the pulse consumes a timer and therefore TIMER or PWMOUT/HPWM cannot be used while a SERVO or SERVOPOS is running. The commands will also cause the PAUSE command to take longer than expected, and in an opposite fashion, SERVO and SERVOPOS will be briefly suspended during timing sensitive commands. Because of these limitations, you may wish to use a multiprocessor configuration and dedicate one or more PICAXEs exclusively to servo control.

Other Articles You Might Find Enjoyable

Design and Build Your Own Robot

K'nexapod - A Hexapod Robot Built With K'nex and PICAXE

Flexinol and other Nitinol Muscle Wires

PICAXE USB to Serial Conversion

Introduction to the PICAXE Microcontroller

Adding Memory with i2c EEPROMs

Guide to PICAXE Selection

Robot Obstacle Detection and Avoidance with the Devantech SRF05 Ultrasonic Range Finder

Calculating Right Triangles with PICAXE BASIC

Understanding Variables and Symbols in PICAXE BASIC

Analog Sensors and the MSP430 Launchpad

Things to Do Here

All PICAXE Articles

Other Places to Go