SPWM generation using PIC16F877A microcontroller

SPWM (Sinusoidal pulse width modulation) using pic16f877a: In this article I will discuss how to use microcontroller to generate sinusoidal pulse width modulation? how to use SPWM signal as a gating signals to MOSFETS of H bridge to get neat and clean sine wave output from h  bridge after using LC filter? Sinusoidal pulse width modulation technique is used by many inverter manufactures and it is used in much industrial application to generate pure sine wave line. This technique is also used in online ups to get pure sine wave output. At the end I will discuss how to use these Sinusoidal pulse width modulation signals with H Bridge.

SPWM technique basically converts the half of the sine wave into small number of pulse each having different width. Sinusoidal pulse width modulation technique produce pulsating wave in which the width of pulses change according to amplitude of sine wave voltage. For example in sine wave amplitude of voltage is minimum at the start of sine wave at 0 degree and then its start increasing up to 90 degree and sine peak amplitude reach at 90 degree. After that amplitude start decreasing again with reverse fashion as it is increased. In SPWM technique we follow the same procedure to make pulsating wave signal in which width of each pulse varied according to same fashion as follow by sine wave magnitude. For example:

                                                                                   { 1,2,3,4,5,6,5,4,3,2,1}

Above data shows the maximum duty cycle or width of pulse is 6 and then its start decreasing in reverse as it is increased.Figure below shows pulses of SPWM.

SPWM sample

SPWM sample

There are two ways to generate SPWM:

  1. By comparing reference sinusoidal wave with triangular carrier wave of frequency fc.

In this method frequency of reference sinusoidal signal is the frequency of sine wave output. This method can be implement using analog electronics components like Amplifies, resistors and capacitor.

2.  By using microcontroller or digital electronics.

This method is preferable and it is used in almost all pure sine wave inverters available in market. Because this method is cheap and easy to implement. All you need a piece of information i.e code, to implement SPWM through this technique. Same microcontroller can also be used to for other functions in inverter like protection, voltage and current reading, digital display of current and voltage and any other functionality you want to have in your inverter. It is the reason I always prefer microcontrollers or digital electronics over analog electronics .It make your life easier by adding extra functionality in your project. In this article I will also discuss how to use PIC16F877A to generate SPWM.

SPWM implementation using PIC16F877A microcontroller

Before implementation of SPWM with microcontroller, you should know about frequency of sine wave you want to get. I have used 50 Hz frequency in this project. 50 Hz mean time period of sine wave is 20ms. So the time period of half cycle is 10 ms, we only generate for half cycle and it is use for both positive and negative cycle. Because H bridge serve the purpose to generate negative or positive cycle. SPWM for half wave contains many pulses and width of each pulse varied according to amplitude of sine wave. But the total time of all pulses should be equal to 10ms ( time period of half cycle of sine wave). But the question is what should be the time period of each pulse? Time period of each pulse depend on frequency of PWM. For example we have chosen a PWM of frequency 20 kHz. Hence Time period of each pulse is equal 100us. So number of pulses we can used to make SPWM is equal to

                                                               Number of pulses = 10ms / 100us =100 pulses.

So we can use 100 pulses for SPWM with 20 kHz frequency. I have already explained SPWM functionality and working. Now I will explain how to generate 100 pulses with variable width or duty cycle according to amplitude of sine wave. We know the relationship of sin wave with its phase angle and peak value. y = A * sin (angle); We know that half cycle of sine wave consist of 180 degree. For example we need 10 pulses. We divide 180 degree into 10 equal parts. To divide 180 degree into 10 equal parts, value of each step is 180/10 = 18 degree.

  1. Y = sin (18) = .3090
  2. Y= sin (36) = .5877
  3. Y = sin (54) = .8090
  4. Y = sin (72) = .9511
  5. Y = sin (90) =1
  6. Y = sin (90) =1
  7. Y = sin (72) = .9511
  8. Y = sin (54) = .8090
  9. Y= sin (36) = .5877
  10. Y = sin (18) = .3090

To convert above values into duty cycle multiply them with maximum duty cycle value which a microcontroller use to generate duty cycle. In PIC16F877A a duty cycle changes from 0-255 i.e. 0 mean 0% duty cycle and 255 means 100% duty cycle. I recommend you to multiply with a value less than 255 because it will help you to get rid from gate turn on or turn off time circuitry. It will give sometime between turn off and turn on mosfets sides of H Bridge.

  1. Y’ = sin (18) = .3090 * 250= 77
  2. Y’= sin (36) = .5877 *250=147
  3. Y’ = sin (54) = .8090 *250=202
  4. Y’ = sin (72) = .9511 *250=238
  5. Y’ = sin (90) = 1 *250=250
  6. Y’ = sin (90) = 1 *250=250
  7. Y’ = sin (72) = .9511 *250=238
  8. Y’ = sin (54) = .8090 * 250=202
  9. Y’= sin (36) = .5877 *250 =147
  10. Y’= sin (36) = .5877 *250= 77

Hence duty cycle or width of each pulse is {77, 147, 202, 238, 250, 250 238, 202, 147, 77 }. Similarly you can calculate duty cycle or pulse width for 20, 30, 100 or any number of pulses you want to use in your SPWM. Greater the number of pulses, more pure sine wave will produce. I have explained you each and everything you need to know about sinusoidal pulse modulation. How choose number of pulse? how to calculate width of each pulse? and what is the relation between time of total pulses and timer of half cycle of sine wave? Now you can develop an algorithm by using any microcontroller to generate SPWM. I have used PIC16F877A microcontroller to generate SPWM of 100 pulses and frequency of each pulse is 20 KHz. Frequency of output sine wave is 50 Hz.

Steps to make algorithm :

  •  Make an array containing duty cycle values of each width
  • Generate a PWM of frequency 20 KHz
  • call each in a function such a way that it repeat itself after a complete cycle.

This is all you need to do with microcontroller to generate SPWM. For more information about how to write code for SPWM using PIC16F877A microcontroller and how to use SPWM for producing gating signals for H bridge and complete circuit diagram of pure sine wave inverter. Go to following article.

Complete Project of pure sine wave inverter

If you have any issue regarding SPWM, feel free to comment on this post. kindly share it with your friends on social media, if you have gained useful knowledge through my article.


  1. sudip mondal November 3, 2014
  2. manu December 2, 2014
    • Chandrakant April 2, 2016
  3. nelson chimwala December 13, 2014
  4. melaku asefa December 22, 2014
  5. VISAKH VL December 24, 2014
  6. Antoneraj January 29, 2015
  7. anil January 30, 2015
  8. arunrx February 24, 2015
  9. arunrx March 4, 2015
  10. arunrx March 4, 2015
  11. EDISON RODRIGUEZ March 26, 2015
  12. arunkumar March 28, 2015
  13. arunkumar March 28, 2015
  14. Arun kumar March 30, 2015
  15. arun kumar April 9, 2015
  16. Cass April 12, 2015
  17. anhvu April 12, 2015
  18. keltoum April 13, 2015
  19. Antoine Freyfer April 15, 2015
  20. Urmila Dhavale June 17, 2015
  21. Mytheen Muhammed July 28, 2015
  22. Anas Mohana August 27, 2015
  23. Biswajit Kundu August 31, 2015
  24. Kj January 2, 2016
  25. Shiji Sebastian January 24, 2016
  26. Timothy February 19, 2016
  27. Timothy. February 19, 2016
  28. castromabs August 12, 2016
  29. Jose Mimbela Cubillas September 10, 2016
  30. thrishool December 16, 2016

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 719 other subscribers