Archive for March 6, 2011

Servo Motor Control Using Microcontroller

A Servo is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio controlled cars, puppets, and of course, robots.

The guts of a servo motor are shown in the picture below. You can see the control circuitry, the motor, a set of gears, and the case. You can also see the 3 wires that connect to the outside world. One is for power (+5volts), ground, and the white wire is the control wire.

Servo motor

So, how does a servo work? The servo motor has some control circuits and a potentiometer (a variable resistor, aka pot) that is connected to the output shaft. In the picture above, the pot can be seen on the right side of the circuit board. This pot allows the control circuitry to monitor the current angle of the servo motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not correct, it will turn the motor the correct direction until the angle is correct. The output shaft of the servo is capable of travelling somewhere around 180 degrees. Usually, its somewhere in the 210 degree range, but it varies by manufacturer. A normal servo is used to control an angular motion of between 0 and 180 degrees. A normal servo is mechanically not capable of turning any farther due to a mechanical stop built on to the main output gear.

The amount of power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control.

How do you communicate the angle at which the servo should turn? The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90 degree position (often called the neutral position). If the pulse is shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degress. If the pulse is longer than 1.5ms, the shaft turns closer to 180 degress.

Standard time vs. angle


If you want to control your servo motor from a microcontroller,then you must follow-

bring high a digital port
wait between 1-2ms
bring low the same digital port
cycle a few dozen times per second

Here,i have used PIC16F887  microcontroller.Code is written in C using mikroC PRO for PIC.

Source Code

void main(){

int i;
ANSEL  = 0;                                    // Configure AN pins as digital I/O



//Move Anti Clockwise direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(1000); //want servo to move to 0 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);


//Move Clockwise Direction
for(i=1;i<=500; i++){
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(2000); //want servo to move to 180 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);



Circuit Diagram