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.

Page Contents

**SPWM (Sinusoidal pulse width modulation)**

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.

There are two ways to generate SPWM:

- 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.

- Y = sin (18) = .3090
- Y= sin (36) = .5877
- Y = sin (54) = .8090
- Y = sin (72) = .9511
- Y = sin (90) =1
- Y = sin (90) =1
- Y = sin (72) = .9511
- Y = sin (54) = .8090
- Y= sin (36) = .5877
- 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.

- Y’ = sin (18) = .3090 * 250= 77
- Y’= sin (36) = .5877 *250=147
- Y’ = sin (54) = .8090 *250=202
- Y’ = sin (72) = .9511 *250=238
- Y’ = sin (90) = 1 *250=250
- Y’ = sin (90) = 1 *250=250
- Y’ = sin (72) = .9511 *250=238
- Y’ = sin (54) = .8090 * 250=202
- Y’= sin (36) = .5877 *250 =147
- 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 :**

**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.

Buy SPWM code in 39$[/button-brown]

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.

First of all I would like to thank you for your useful explanations.

But, Would you please express more “How did you calculate Time period for 20KHz”?

(Confusion about 100uSec , Because Tpulse = 1/20KHz = 50uSec not 100uSec)