GENERATING PWM USING PIC MICROCONTROLLER,In this tutorial, you will learn to generate a PWM signal with the help of PIC microcontroller 16F877A. PWM is supposed to be a new concept for the beginners and by the end of the tutorial, you will have a sound knowledge of this term and will be able to work with it in different projects.

What is pulse width modulation ?

PWM (Pulse Width Modulation) is a powerful technique used to control analog circuits with the digital output from the microcontroller. There are two major components of a PWM signal that defines its behavior; PWM duty cycle and frequency.

Duty cycle describes the ‘on-time’ of a signal. An on-time is the duration of a signal for which the signal stays HIGH. Duty cycle is measured in percentage. For example, if a digital signal is on for half of the time duration and off for the other half, the digital signal is said to have a duty cycle of 50%. Similarly, if a signal stays high for a longer period of time than it stays low, the signal will have a duty cycle greater than 50%.

The frequency determines the amount of time taken by PWM to complete one cycle. For example a frequency of 1000Hz would mean 1000 cycles completed per second.


To generate PWM with the help of a microcontroller, built-in CCP modules are used. CCP stands for Capture/Compare/PWM. There are two CCP modules present in PIC16F877A; CCP1 and CCP2 at pins RC2 and RC1 respectively.


PWM using pic16f877a microcontroller

PWM using pic16f877a microcontroller

Make the above circuit diagram in Proteus. Four switches (SW1, SW2, SW3, and SW4) are connected to the lower four bits of PORTB of the controller with pull-up resisters of 10K. The other ends of the switches are grounded. These switches are used to control theduty ratio of PWM which is generated by the CCP modules. An oscilloscope has been connected to the CCP1 and CCP2 pins of the microcontroller which is used to display the waveforms and variations in it, as the duty cycle is increased or decreased. The remaining standard circuit includes the crystal oscillator connection and that of the MCLR pin.

The figure on the right shows the resulting waveforms received from the two CCP modules. CCP2 pin of controller is connected to the channel ‘A’ (yellow) of the oscilloscope and CCP1 pin to channel ‘B’ (blue). The PWM duty ratio, generated from CCP2 pin, has been increased via switch SW3.

Pulse width modulation

Pulse width modulation



Write the following code in mikroC compiler:

  1. void main()
  2. {
  3. short current_duty_1 = 16; // initial value for current_duty_1
  4. short current_duty_2 = 16; // initial value for current_duty_2
  5. TRISB = 0xFF; // PORTB as input
  6. TRISC = 0x00;  // PORTC as output
  7. PWM1_Init(5000); // Initialize PWM1
  8. PWM2_Init(5000); // Initialize PWM2
  9. PWM1_Start(); // start PWM1
  10. PWM2_Start(); // start PWM2
  11. PWM1_Set_Duty(current_duty_1); // Set current duty for PWM1
  12. PWM2_Set_Duty(current_duty_2); // Set current duty for PWM2
  13. while (1) // endless loop
  14. {
  15. if (!RB0_bit) // SW1 is pressed
  16. {
  17. Delay_ms(40);
  18. current_duty_1++; // increment current_duty_1
  19. PWM1_Set_Duty(current_duty_1); //Change the duty cycle
  20. }
  21. if (!RB1_bit) // SW2 is pressed
  22. {
  23. Delay_ms(40);
  24. current_duty_1–; // decrement current_duty_1
  25. PWM1_Set_Duty(current_duty_1);
  26. }
  27. if (!RB2_bit)   // SW3 is pressed
  28. {
  29. Delay_ms(40);
  30. current_duty_2++; // increment current_duty_2
  31. PWM2_Set_Duty(current_duty_2);
  32. }
  33. if (!RB3_bit) // SW4 is pressed
  34. {
  35. Delay_ms(40);
  36. current_duty_2–; // decrement current_duty_2
  37. PWM2_Set_Duty(current_duty_2);
  38. }
  39. Delay_ms(10); // slow down change pace a little
  40. }
  41. }

 This code uses many built-in library functions of PWM present in the mikroC Compiler. Initial value of the duty cycle has been declared in the beginning of the code. PORTB is set as an input port on which the switches are connected. PORTC is set as an output port for generating PWM signals. The four ‘if’ statements determine the state of the two waveforms. If switch SW1 is pressed, the duty ratio of PWM produced by CCP1 is increased andif SW2 is pressed, the duty ratio produced by CCP1 is decreased. Similarly for CCP2 module, SW3 increases the PWM duty ratio and SW4 decreases the PWM duty ratio.


PWM can be used in a variety of applications. It can be used to control a servo motor as well as other motors requiring speed control. PWM is also used to control the average power delivered to a load.

To download files of this article click on this link.

PWM Article files.

    We highly recommend EasyEDA for Circuit Design and PCB Prototype

Low Cost PCB Manufacturing & Fabrication Service - EasyEDA   

 PCB Prototype: Only $8.21 for 10pcs,100x100mm PCBs, 2-3 days' delivery    


  1. Aishah Aziz April 18, 2015
  2. Lakiesha Vaughan August 18, 2016

Add Comment