Servo Motor Control by Using Microcontroller PIC16F877A

PIC16F877A is an easy micro controller for the beginners
Here in project:
1) How PIC switches servo motor forward and back ward
When we push one switch it goes to positive 90 degree , on second switch it moves backward to  negative 90 degree

a. MickroC  PRO        // for programming of MICRO CONTROLLER
b. Proteous  8 professional       //For simulation of CKT and Design

Eng. Mahmoud Abuzayed

Teacher Notes

Teachers! Did you use this instructable in your classroom?

Step 1: R/C Servo Control Theory

Servos designed for use in radio-controlled airplanes, cars, and boats can be easily interfaced to a PIC MCU. They are often used for robots and applications where simple mechanical movement is required. This might be surprising because a positional servo is considered to be an analog device. The output of an RC servo is usually a wheel that can be rotated from 0 to 90 degrees. (some servos can turn from 0 to 180 and others have very high torque outputs for special applications). Typically, they only require +5 V, ground, and an input signal.

An RC servo is an analog device; the input is a PWM signal at digital voltage level. This pulse is between 1.0 and 2.0 ms long and repeats every 20 ms .The length of the PWM pulse determines the position of the servo's wheel. A 1.0-ms pulse will cause the wheel to go to 0 degrees and a 2.0-ms pulse will cause the wheel to go to 90 degrees.

Pulse Width Modulation (PWM)
       Pulse Width Modulation (PWM) is one of the powerful techniques used in control systems today. They are not only employed in wide range of control application which includes: speed control, power control, measure and communication.

Basic Principal of PWM



Consider a square wave shown in the figure above. Ton is the time for which the output is high and Toff is the time for which output is low. Let Ttotal be time period of the wave such that,

Duty cycle of a square wave is defined as:

The output voltage varies with duty cycle as:


If Ton is 0, Vout is also 0.

If Ton is Ttotal then Vout is Vin or say maximum.

Step 2: PIC PWM Module

In Pulse Width Modulation mode, the CCP1(RC2) pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1pin an output.
Figure below shows a simplified block diagram of the CCP module in PWM mode.

A PWM output has a time-base (period) and a time that the output stays high (duty cycle). The frequency of the PWM is the inverse of the period (1/period).


The PWM period is specified by writing to the PR2 register. The PWM period can be calculated using the following formula:

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

PWM frequency is defined as 1 / [PWM period]. When TMR2 is equal to PR2, the following three events occur on the next increment cycle:

 TMR2 is cleared
 The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be set)
 The PWM duty cycle is latched from CCPR1L into CCPR1H

The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. Up to 10-bit resolution is available. The CCPR1L contains the eight MSbs and the CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON<5:4>. The following equation is used to calculate the PWM duty cycle in time:

PWM duty cycle =(CCPR1L:CCP1CON<5:4>) •TOSC • (TMR2 prescale value)

The following steps should be taken when configuring the CCP module for PWM operation:
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPR1L register and CP1CON<5:4> bits.
3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
5. Configure the CCP1 module for PWM operation.

Step 4: Circuit Diagram

Circuit Diagram

Step 5: Code for PIC 16F877A

void main()
  //int x = 125;
  TRISC = (1<<3)|(1<<4) ;   //   configure RC3 as input (1 for input and 0 for output)

  PR2 =  0xFA;                 //oxFA for 250  (used to set time period)
  CCPR2L =  125;               // (used to set duty cycle)
  CCP2CON |= (1<<2)|(1<<3);            //  select for PWM mode
  T2CON |= (1<<2)|(1<<1);              // enabling timer 2, prescalar 16


       if(PORTC.F3==1)                        // forward
        // x=x+1
        CCPR2L =  250+125;               // (used to set duty cycle)
       if(PORTC.F4==1)                      // back ward
       // x=x-1
        CCPR2L =  250-245;               // (used to set duty cycle)

Step 6: OUT PUT

output of the code :

123D Circuits Contest

Participated in the
123D Circuits Contest

2 People Made This Project!


  • CNC Contest

    CNC Contest
  • Make it Move

    Make it Move
  • Teacher Contest

    Teacher Contest

3 Discussions

Richard chelsea

2 years ago

sir how can i control 2 servos bidirectional with different buttons


3 years ago

Frequecy of the crystal oscillator is not shown in the schematic?

If the PIC is running at 20MHz crystal, I dont think it is possible to generate a low frequency PWM (20ms cycle time or 50Hz) using the CCP module

1 reply

Reply 3 years ago


you can't control the servo while your clock is 20 MHz. the minimum frequency for the PWM will be too high my be 5 KHz.

i did it by write a for loop and control the servo with out use the PWM model.

Best regards

Mohammed Jassim