We’d like to build a robot leg for our Resens hexapod, and decided to get a £10 digital servo on eBay for testing.


Control servo motor

For the hardware, servo motor usually comes with a three-pin header: GND, Power, and control signal; and this servo, Q20-5C-180, had that header too. While applying a high-curent power supply to Power and GND pins, a pulse-width modulation (PWM) signal applied to the control pin determines the angular position of the servo. The servo specification usually publishes the frequency of the required PWM signal and the relationship between pulse width (duty cycle) and angular position.


For the servo we bought, as printed on the cover, its power supply ranges from 4.8 to 7.4-V. In order to control the angular position from 0 to 180 degrees, the pulse width should change from 500 to 2500-microseconds, respectively.

Hardware connection

Using an Arduino board such as Arduino Mega 2560, the hardware connection to control the servo is quite simple. We only need one pin operating as the control signal. In our case, pin 9 on the Arduino board connected to the control pin on the servo header.

It should be noted that the power applied to the servo should not be taken from the +5V pin on the Arduino board. This is because the current requires by this high-torque servo may be larger than what could be supplied the Arduino pin, especially with load, which may damage the board. Instead, a separate power supply with a minimum current capacity of 1-A should be used. We’ve used a 5-V, 2-A charger for this purpose.

Firmware development

I’ve mentioned that we use a PWM signal to control the angular position. This is correct for both analog and digital servos.

In practice, digital servos may already integrate an electronic module to control the PWM signal. And we only need to tell this control module which position we want. This is the case for our servo.

As you can see on the motor cover, we should use a pulse with a width ranging from 500 to 2500 micro-seconds to control the angle from 0 to 180 degrees. Of course we can still apply a PWM signal with the width as above to control the servo. However, doing that would draw more current from the power supply, and therefore, result in more power consumption.

The code to do this job is very simple as follows:

#define T_SAFE 10 // minimum low/high in one duty cycle to avoid zero value
#define T_MIN 500 // min value for HIGH period
#define T_MAX 2500 // max value for HIGH period
#define T T_MAX+T_SAFE // duty cycle for servo motor in micro-seconds
int servo_pin = 9; // PWM pin
void setup() {
pinMode(servo_pin, OUTPUT); // sets the pin as output
// start at angle = 0
th = T_MIN;
tl = T – th;
// rotate motor using pulse-width
digitalWrite(servo_pin, HIGH);
digitalWrite(servo_pin, LOW);

Below is a demonstration of receiving the required angle through serial port and control the servo moving to that angle.

A simple code to receive the required angle string from your computer is here.

Next step

We’ll need to design sections of the leg of our Resens hexapod robot to hold servos and see whether we can smoothly control its movement.