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.

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.

can you explain the programme in details pls

sir, I wana to make 3 phase inverter , which voltage and frequency should vary as per the v/f ratio, 0-50hz.

I wana to make 3 spwm each have 180 degree apart.

can I use aurdino (atmega328), can I generate 3 spwm by hardware,

pls help me

if you have gotten the answer then please help me doing this….

Very nice blog u got here

But this means I cannot use the microcontroller for other purposes such as PID control calculations while generating SPWM, right?

thank you sir .thank you very much…………..

can i use this spwm for controlling single phase ac induction motor control.

can u explain the program in detail

i have studied this topic from various books and researching it on internet for 2-3 years to get the complete understanding and its implementation .Today my search ends .your one page article gave more information and in very simple way compared to 2 or 3 prescribed UG engineering books.

Thanks a lot.

Mikro c(pic16f877a)

sir how generate 12 gating signals…

sir hw to generate 12 gating signals….

I would like to see a picture of what is in the mosfet because much of what you have to prove to me it does not generate a pure square wave and I would like to know how often you calculate the lc filter

sir can the explain the code in detail plz..

sir can u explain the code in detail plz..

sir,can u explain the code in detail…

Sir,I hv connect the ckt in breadboard.I hv downloaded program in pic IC. But output at pin no 34 n 17 it not cmg.I hv given 5v dc supply .plz Help me…

Hi

I have sin very good projects in here and would like some help from you….

I got a project to design and implement…. the project is

12v DC /220v AC micro controller base to produce PWM…..

500w output power to use for sensitive electronic devices

the problem is I run out of time for the project….

And now I am really struggling ho to make it… the design to determine the right components

can you help me please?

hi! I i trying to design a Sine Wave Inverter but i have some broblem. i cant see signal output on RB1.it nothing. can you give me code it ?. iguess i fail when i copy.

my email: chimchichclick@gmail.com

hi sir,Could you please give us the programme of this PWM in protus ISIS because I will make a three phase inverter plz help me

Dir Sir,

I appreciate so much your work …

Could you please explain to me please how we get a pulse time periode of 100us for a 20khz frequency unless we got a frequency pf 10khz instead. Am i right.

Thank you in advance.

Sincerly

I want to generate R-y-B 3 phase sine wave using PWM technique in PI18F. how should i calculate the value of look up table?

Can I have get the code in CCS C compiler.

i can’t open this page 404 error PLz can support me in this problem

“Complete circuit diagram of pure sine wave inveter and coding”

Can you Generate SPWM without CCP. Only using GPIO Pin.

Hello,me I am having a problem of using this IR2110 for driving 6 IGBT s making up a three phase IGBT bridge for three phase induction motor speed control,could anyone help me to handle this issue?I am using PIC16F1936 for generating the six signals but I am not succeeding please help.

What kind of problem you facing in driver.?

Comment Text* what calculation did you do, that resulted in getting the period of each pulse as 100us. I am a bit confused about it.

Good job sir but there is a little confusion, which is ; what calculation did u do ,that resulted to getting the period of each pulse as 100us ? I would be so grateful if u can answer as soon as possible.

sir please i need a complete pure sine wave inverter circuit using any of the pic 16 series micro controller with the source code

Dear Bilal Malik:

I read your tutorial on using PIC 16F877A SPWM Implementation microcontroller and is a very good help for this purpose.

In its description you say, referring to a sine wave of 50 Hz “But the question is what Should be the time period of each press? Time period of each press depend on frequency of PWM. For example We Have Chosen to PWM of frequency 20 kHz. HENCE Time period of each press is equal 100us. So we can number of pulses used to make SPWM is equal to

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

Question ? If the PWM frequency is 20KHz we use, its period is 1 / 20,000 = 50us and if we consider only half cycle, the number of pulses is:

Number of pulses = 10ms / 50us = 200 pulses

Please clarify my doubt answering my mail.

Thank you

what did you multiplied 255 to instantaneous sine values???

8 bit timer is cable of producing 0-255 duty cycles (8 bit 2^8=255)

sir can you explain code please

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.

How did you get this 100us

THANKS A LOT ,THIS PAGE IS MORE THAN BOOK,

Very useful, specially in calculating duty cycle of each pulse for sine PWM

very useful article…

but can we calculate the amplitude modulation of SPWM?

much appreciated 🙂