# Servo motor control by Microcontroller PIC16f877 and MATLAB GUI

By | January 2, 2014

Here you see how a Servo motor controls by Microcontroller PIC16f877 and MATLAB GUI ? It is well known to us that now a day’s use of servo motor extended in the field of Robotics. Servo motor can rotate with definite angle. So we can easily use it in design of robot arm movements. And also it can use extremely in Remote-car and Helicopters. Now in my project “ Servo motor control by Microcontroller PIC16f877 and MATLAB GUI ” I will show you how a servo motor is rotate with 15° interval. And how it control by MATLAB GUI ? Most useful and interesting thing I shall also show you how it simulate in Proteus with help of MATLAB GUI.

## Software required for PROJECT:

1)      Mikro C Pro for PIC  ( For write C Program and generate HEX file)

2)      Proteus ISIS               (For simulate the project)

3)      MATLAB                      ( For making GUI and Controlling the motor rotation)

4)      Virtual Serial Port Emulator. ( to making virtual com port and pairing them)

Servo motor Control: We can control a rotation of Servo motor by PWM (Pulse Width Modulation) technique. Only changing the duty cycle of a pulse signal, we can vary the rotation of servo motor with specific angle. Using Mikro C Pro, we can easily sets PWM duty ratio for PIC Microcontroller. Parameter duty ratio takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as `(Percent*255)/100`.

Here I used in my project setting the specific value of Duty ratio to obtain specific angle. In bellow I give you the table of specific Duty ratio Vs. Specific Angle.

 Duty ratio Angle in Degree 0 0 21 15 42 30 63 45 83 60 103 75 125 90 145 105 165 120 187 135 207 150 228 165 255 180

Circuit Diagram in Proteus:

See the circuit diagram of servo motor control by Microcontroller PIC16f877 and MATLAB GUI in bellow. From the bellow Figure, we can notice that we feed input to servo motor from CCP1/Rc2 pin of Pic16f877. This CCP1 pin is used for PWM 1 so we have to take input from that pin only. ## How to make MATLAB GUI:

It is better to see the video of the process to How to make MATLAB GUI rather written the steps for that. See the bellow video.

## C code for the project Servo motor control by Microcontroller PIC16f877 and MATLAB GUI:

To write C code, we have to know some command for initialising PWM unit and UART unit. Because we connect Matlab with proteus through UART port.

To interfacing UART with Pic we have to use some new command those are

UART1_Init (Baudrate): is Initialize UART module at specified baudrate.

UART1_Write_Text(“Start”): use to write declared text on uart

UART1_Write(): use for transmit uart data.

To interfacing PWM with Pic we have to use some new command those are

PWM1_Init(500): is Initialize PWM module at specified frequency.

PWM1_Set_Duty(i): use to set duty Ratio of PWM.

See the Code

Embedded C Code
// Name : Interfacing GSM module with PIC Microcontroller
// Purpose : To send SMS from PIC Microcontroller.
// Author : Subham Dutta
// Date : 03-01-14
// Website : www.nbcafe.in

unsigned int uart_rd;

main(){

int i;

PWM1_Init(500);

UART1_Init(9600);               // Initialize UART module at 9600 bps

Delay_ms(100);                  // Wait for UART module to stabilize

while (1) {                     // Endless loop

UART1_Write(uart_rd);

}

if (uart_rd == ‘a’){

i=21;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘b’){

i=42;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘c’){

i=63;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘d’){

i=83;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘e’){

i=103;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘f’){

i=125;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘g’){

i=145;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘h’){

i=165;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘i’){

i=187;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘j’){

i=207;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘k’){

i=228;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘l’){

i=255;

PWM1_Set_Duty(i);

PWM1_Start();

}

if (uart_rd == ‘m’){

i=0;

PWM1_Set_Duty(i);

PWM1_Start();

}

}

}