SERVO MOTOR interfacing with PIC16F877A MICROCONTROLLER,In this tutorial, you will learn the interfacing of a servo motor with PIC16F877A microcontroller. Programming to control servo motor and hardware connections of servo motor with PIC16F877A microcontroller. Lets start with basic introduction of servo motor, then I will move forwared to its circuit diagram and programming.
What is servo motor?
A servo motor is a special kind of motor that operates upon the given instructions. It provides angular precision, which means,unlike other electrical motors that keep on rotating until power is applied to them and stops only when the power is switched off, the servo motor rotates only to a certain degree or until it is required to and then the motor stops and waits for the next instruction to carry out further action.Servo motors are controlled with the help of servomechanism. Its angular rotation and final movement is dictated by position feedback. The input to its control line determines the position demanded for the output shaft.
TASK
To control the working of a servo motor when interfaced with PIC16F877A microcontroller.
CIRCUIT DIAGRAM FOR SERVO MOTOR INTERFACED WITH PIC 16F877A :

It has a very simple circuit diagram. The control wire of the servo motor is directly connected to the RB0 pin of the microcontroller. This pin will provide the required angular displacement of the motor. In this project, suppose we are working with a servo motor whose angular rotation is limited to 0° – 180°.We can control the rotation of the motor within this limit with utmost precision by using a pulse whose width is varied to attain the desired angle.
A pulse is fed to the servo motor after every 20ms (or 20,000us). The angular position of the motor is determined by the length of this pulse. The angular positions 0°, 90° and 180° are demonstrated in the code.
C-CODE FOR SERVO MOTOR INTERFACING WITH PIC MCU
Write the following code of servo motor interfaced with PIC in mikroC Compiler.
void Rotation0() //0 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(800); // pulse of 800us
PORTB.F0 = 0;
Delay_us(19200);
}
}
void Rotation90() //90 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(1500); // pulse of 1500us
PORTB.F0 = 0;
Delay_us(18500);
}
}
void Rotation180() //180 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(2200); // pulse of 2200us
PORTB.F0 = 0;
Delay_us(17800);
}
}
void main()
{
TRISB = 0; // PORTB as Ouput Port
do
{
Rotation0(); //0 Degree
Delay_ms(2000);
Rotation90(); //90 Degree
Delay_ms(2000);
Rotation180(); //180 Degree
}while(1);
}
Individual functions for angular rotation of the motor by 0°, 90° and 180° have been declared in the beginning of the code.In this tutorial, we have not used the actual Pulse Width Modulation feature of PIC16F877A to generate a pulse.Instead, the pulse has been created with the help of delays in the program. The delay duration for a particular angle is equivalent to the length of the pulse required for the motor to rotate up to thatcorresponding angle. That is, for 0° angle, the pulse width is approx. 800ms, so a delay of 800ms is introduced with the PORT pin RB0 set to high. Similarly,a pulse of 1500ms is required for a rotation up to 90° and 2200ms for a180° angle.
In the main program, PORTB is set as an output port and all the three functions are called, separated by a delay of 2000ms between them. The program causes the motor to rotate in a pattern; 0° – 90° – 180° – 0° and so on. This continues in an infinite ‘do-while’ loop, until the program is aborted.
APPLICATION
Servo motor has a large number of application in industries. In industrial manufacturing, the conveyor belt uses servo motors to control the precise movement of the belt. Servo motors are most popular in robotics. The precise angular displacement of a robot’s arm is controlled by a servo motor. The physicalmovement of the solar panel with respect to the sun is also carried out by servo motors in a solar tracking system.
To download circuit diagram and code. Click on following link. Don’t forget to share this article with your friends. Thanks 🙂
// your codes are straight understandable….. have a long life
can i use this code to run the motor using L293D
thank you so much, it’s really useful
sir why r u using a loop of 50 also the pulse that are generated on oscilloscope on proteus are of different time and servo is not going to 90 instead it is going to -33 in my case im using pic18f4520 i have made the changes accordingly to the code for pic 18
i need to be assited to write a code of running 5 servo motors using c language and MPLAB compiler.
Where to buy servomotor with pIc 16f877a can install with one half square meter solar panel
you can buy from amazon and aliexpress
How mich the servo motor with pic16f877a can caŕry solar panel about 2kls
Please i want to control the speed of rotation of the servo motor so that it can get the right angle slower.
If any one could help me !
unsigned int rpm;
unsigned int speed;
unsigned int pulse=0;
float *x;
char ms,sec,minu,hr;
int timer;
char text[15];
void main()
{
TRISD.F0=1;
TRISA=0X00;
PORTA=0;
TRISC=0X00;
PORTC=0;
rpm=0;
speed=0;
#define sw PORTD.RD0//RD0 PIN pulse input
TRISB=0; // COnfigurePortB as output Port.
LATB=0x01; // Turn on LED on PORT B pin0
T0CON=0x07; // Prescaler= 1:256, 16-bit mode, Internal Clock
while(1)
{
if(sw==1){
while(sw==1);
pulse++ ;
speed=1000*pulse++/1;
}
if(pulse>=721)pulse=0;
if(RD0_bit==1)pulse++;
if(pulse==pulse=129 && pulse<=180 ){
PORTA.F2=1;
}
// Values calculated for 1 second delay with 20MHz crystal
TMR0H=0xB3; // Placing Lower byte in TMR0L
TMR0L=0xB4; // Placing Lower byte in TMR0L
T0CON.TMR0ON=1; // Timer0 On
while(INTCON.TMR0IF==0); // Wait until TMR0IF gets flagged
T0CON.TMR0ON=0; // Timer0 Off
INTCON.TMR0IF=0; // Clear Timer0 interrupt flag
LATB=(LATB<>7); // Circular right shift at PortB
}
}
please help me to make real velocity and rpm
C’est possible de commander le servo moteur SUR 45°?