electroSome

Interfacing Servo Motor with PIC Microcontroller

Servo Motor uses error sensing negative feedback to control the precise angular position. Servos are used for precise positioning in robotic arms, legs, RC Aeroplanes, Helicopters etc. Please read the article Servo Motor for more information about its working and construction. Hobby Servo Motors have three wires, two of them (RED and BLACK) are used to given power and the third one is used to give control signals. Servo can be easily be controlled using microcontrollers using Pulse Width Modulated (PWM) signals on  the control wire. Here we are using a servo whose angular rotation is limited to 0 – 180°. We can control the exact angular position by using  a pulse, whose width varying from 1 millisecond to 2 millisecond on the control wire. The actual behavior of a particular motor depends upon its manufacture, please refer the datasheet of the particular motor for that.

Here I am using VIGOR VS-10A servo motor.

Servo Angular Rotation Pulse Width Modulation

Circuit Diagram

Interfacing Servo Motor with PIC Microcontroller

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

MikroC Programming

void servoRotate0() //0 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(800);
    PORTB.F0 = 0;
    Delay_us(19200);
  }
}

void servoRotate90() //90 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(1500);
    PORTB.F0 = 0;
    Delay_us(18500);
  }
}

void servoRotate180() //180 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(2200);
    PORTB.F0 = 0;
    Delay_us(17800);
  }
}

void main()
{
  TRISB = 0; // PORTB as Ouput Port
  do
  {
    servoRotate0(); //0 Degree
    Delay_ms(2000);
    servoRotate90(); //90 Degree
    Delay_ms(2000);
    servoRotate180(); //180 Degree
  }while(1);
}

Control wire of the Servo  is connected to RB0 of the PIC Microcontroller. Delay_ms() is used to make delay in milliseconds during the program execution while Delay_us() is used to make delay in microsecond during the program execution.

Download

You can download MikroC Code, Proteus files etc here…..
Interfacing Servo Motor with PIC Microcontroller

Want to See the Output ?