SPLat Logo

Example: Controlling an RC servo

NOTICE: We are updating our website. For product enquiries and orders please contact us directly.
NOTICE: SPLat Controls has moved. We are now at 1/85 Brunel Rd, Seaford, 3198. map

Example: Controlling an RC servo

RC (Radio Control) servos are small devices that position a physical output, usually a rotating shaft, to a particular point under control of a digital pulse width modulated (PWM) input signal. RC servos exist primarily in the hobby sphere.

This example will show you how to control a servo using an output from a SPice connector on a suitably equipped SPLat Controller.

Note: This procedure requires connecting wires directly to the pins of a SPice connector, which have no protection circuitry. Take particular care that none of the wires accidentally touches a terminal it is not supposed to. Always disconnect power before making or breaking connections, and double check wiring before applying power. Keep wires a short as possible and route away from any wires carrying high voltage (120/240V) or current. Any damage to the SPLat attributable to careless practices will void the warranty.

Wiring

An RC servo has 3 wires. These are:

  V+ Positive power. This needs to be wired to a power supply suitable for the servo itself. Typically servo devices require 4.5 to 6V. You need a power supply rated for the current specified by the servo manufacturer. Connect the servo V+ wire to the positive terminal of the power supply (red below).
  Common/Gnd/0V/V- Different servo manufacturers will use different designations. This needs to go to the negative terminal of the servo power supply (above) and also to the 0V pin of the SPLat SPice connector (Green below)
  Signal This is the pin/wire on the servo that receives the pulse width modulated control signal. It needs to be connected to a SPice pin designated as capable of being an analog output (Yellow below)

The diagram above shows a single RC servo connected to SPice pin 8. On an MMi202 this is equivalent to analog output 9 when used with an fAnout instruction.

Programming

Programming is quite simple. It exploits the fact that the "analog" outputs on a SPice connector are actually pulse width modulated 5V signals ... exactly what an RC servo wants. The base frequency of the PWM signal has to be adjusted away from default in order to suit the servo. That means any other analog outputs will suffer, so you can't for example have a 0-10V analog output or thermistor drive at the same time off the same controller.

Here is some sample code: This small program will control 2 RC servos connected to an MMi202 SPice connector.

* Set PWM output repetition frequency to 125 Hz. This gives us a PWM pulse width range of
* 0 - 8ms. The servo used in this example accepts 0.6ms --> 2.4ms. 
* Using the floating point Fanout instruction,
* this gives an allowable range of value of 0.6/8.0 = 0.075 --> 2.4/8 = 0.30. As the normalised
* range of 0 --> 1 is realised with a PWM resolution of 10 bits (0 - 1023), this gives us a 
* control range of (0.3 - 0.075) * 1024 = 230 counts or 1/230 = 0.43% 
        
        setu            0,7             ;select 125 Hz PWM operation
        SpxCmd1         0,!cpu
        
        setu            0,0             ;set up SPICE pin 4 as a PWM output
        setu            1,0
        setu            2,0
        setu            3,0
        setu            4,4             ;SPICE pin 4 set up as a PWM output
        setu            5,0             ;SPICE pin 5 is already a PWM output
        spiceconfigu

loopy   floadw          0.075           ;go to one extreme of the travel
        fAnout          8               ;Spice pin 9 on an MMi202
        fAnout          9               ;Spice pin 8 on an MMi202
        pause           200             ;wait...
        
        floadw          0.30            ;go to the other extreme of the travel
        fanout          8
        fanout          9
        pause           200             ;wait...
        goto            loopy