RC Servo Library

1. Overview

The library allows Ansteron Board to easily control up to 4 RC servos at the same time. It is actually a signal generator with 4 outputs that can be directed to any I/O pin. Each output is a PWM signal with frequency of 50 Hz and pulse width varies from 0 to 3000 microseconds at minimum 16 microseconds step.

RC Servo library runs as a background task on Ansteron Board and generates output without the need for attention from the main program. Usually, the signal needed for controlling a basic RC servo should have pulse width between 900 to 2100 microseconds, corresponding with 0 to 180 degree moving angles. However, these numbers are different depends of manufactures, types, size of the particular RC servo.

If full torque on the RC servo is not needed, more RC servos can be controlled by redirecting signal outputs to different servos. In this mode, up to 3 RC servos with full torque and 17 others can be controlled by one Ansteron Board.

Important note: RC Servo may draw much more power than Ansteron Board can provide on 5V pins. A Separate power supply must be used for RC servo(s). Also be careful that electrical surges that occur when the motor inside of RC servo turns on and off may damage Ansteron Board if connected directly (adding a large capacitor between +5V and GND could help).

2. How to use the library

The circuit below is an example for one RC servo connected to pin B1 of Ansteron Board. A battery pack or power supply with voltage between 4.5V and 6V is used for the RC servo.

Example program below will initialize the signal generator and redirect output number 0 to pin B1. The program will then turn the RC servo between 0 and 180 degree every one second. For the servo using in this example, 900 microseconds pulse will make it turn to 0 degree and 2100 microseconds will be 180 degree. These numbers may be different for other RC servos.



This function starts the signal generator. It should be called at the beginning of the program. The function only starts background task and none of the 4 output channels will be initialized . The program should only initialize the channel that it will use.

RC_Servo_setup_channel(channel, pin);

Setup a channel and direct output to specified pin. There are 4 channels can be used 0, 1, 2 and 3. Value for input pin is similar to other I/O functions (PIN_Cx, PIN_Bx, PIN_Dx). After a channel is set up, the output will be high (no pulse) until a value is set to the channel.

RC_Servo_set_microsecond(channel, microsecond);

Set the pulse width of output signal for the given channel. Values for channel can be 0, 1, 2 or 3. The channel must be set up before using this function. The value for microsecond can range from 16 to 3000. If the value is less than 16 or higher than 3000, the signal output will stay high and no pulse is generated.

RC_Servo_redirect_channel(channel, pin);

Change the output of a channel without changing the current value. The old pin will be left at output and stay high or low depend on when the output is switched. Either way, the servo attached to the old pin will stay at current position since there is no pulse. This function can be used to control more than 4 servos when full torque is not needed.


Put a channel back to inactive state. The pin attached to that channel will no longer be output and no pulse is generated.


Stop the signal generator. Note that the pins will still be output and stay high or low depends on when the generator is turned off. All initialized channels must be disabled before turn off the generator


The example below shows how to control more RC servos using only one channel. Basically, the signal is directed to each RC servo for a period of time. When the RC servo has reached desired position, the signal can then be sent to other ones. This way, more RC servos can be connected if full torque on each one is not important. Besides, if the program can manage to repeatedly send the signal to each RC servo at a certain rate, more torque can be achieved.

3. Note

RC Servo library create a background task for generating signals. The task is based on hardware interrupt and it will be disabled if function system_disable_interrupt() is called. However, temporally disable interrupts for a short period of time (less than 20 milliseconds) should only have a little effect on RC servos.

Function delay_ms() of standard library will be prolonged since there is only 66.5% of CPU time for it when the background task of RC Servo library is running. Command delay_ms(1000); will actually delay for about 1.504 second instead of 1 as usual. To calculate the new value for delay function, multiply the desired time with the percentage of CPU time available for delay function.

RC Servo library can work with TimeStamp library in a program without any collision. The RC servo library itself will consume about 33.5% of CPU processing time regardless how many channels are in use.