RC servo motor is commonly as actuator which requires precise control of angular position. An example of application of servo motor is robotic arm. Inside a servo motor, there is a motor, gearbox, potentiometer and control circuit. The potentiometer is used to measure the position of the shaft and the control circuit will control the shaft accordingly.

servo

Servo motor usually has 3 pins which is the power, ground and signal. The colour code for pins might varies with servo manufacturer.

servo3

So how do we control a servo motor? Servo motor is controlled by inputting pulse width modulation (PWM) signal. The period of PWM has to be 20ms or frequency, 50Hz to control it. The angle of servo motor is control by the pulse width (the time of HIGH) over the period.

Usually there is a minimum and maximum pulse width allowable according the different servo motors. Always check the datasheets for more details. For example, a pulse width of 1.5ms would gives a neutral position 90 degree. A minimal pulse width, 1ms would set the servo position to 0 degree, whereas a maximal pulse width, 2ms would set the servo position to 180 degree. Note that a normal servo motor can only rotate from 0 to 180 degree. However, hacking a servo motor allows it to rotate 360 degree.

servo1

A servo motor usually requires a supply voltage around 5V to 6V. If you are using a servo motor in your circuit from a 9V battery, the servo motor might works fine. However, if your circuit consists of more than 1 servo motor, you need to consider the power supply well as they will drain a lot of current.

servo2
Circuit diagram
servo
Wiring diagram

The servo I had set up with Arduino. The Arduino is powered by wall plug adapter while the servo motor is powered by four AA battery (Total 6V).

DSC06805

A simple code to rotate from 0 degree to 180 degree repeatedly:

#include <Servo.h>

Servo servo1;
int pos = 0;
void setup()
{
 servo1.attach(9);
}

void loop()
{
 for(pos=0;pos<180;pos++)
 {
 servo1.write(pos);
 delay(15);
 }
 for(pos=180;pos>0;pos--)
 {
 servo1.write(pos);
 delay(15);
 }
}

Code explanation:

As explained earlier, controlling the servo motor, you need to control the pulse width of signal. However, with the Servo.h library, you can control the servo motor with angle.

servo.attach(pin)

– This is used to attach the variable to servo pin. For example if you want to control 2 servo motors, you can make some adjustment of code as below.

#include <Servo.h>

Servo servo1;
Servo servo2;

int pos = 0;

void setup()
{
 servo1.attach(9);
 servo2.attach(10);
}

servo.write(angle)

– Control the angle of servo motor. Note that you need to replace the xxxx in xxxx.write() with your declared variable name.

Video demo for servo motor.

Advertisements