Wednesday

Arduino SPWM Generator Circuit

In this post we learn how to generate sine wave pulse-width-modulation or SPWM through Arduino, which can be used for making a pure sine wave inverter circuit or similar gadgets.


The Arduino code is developed by me, and it is my first Arduino code, ...and it looks pretty good :)

What is SPWM


I have already explained how to generate SPWM using opamps in one of my earlier articles, you could go through it for understanding how it can be created using discrete components and regarding its importance.

Basically, SPWM which stands for sine wave pulse width modulation, is a type of pulse modulation where the pulses are modulated to simulate a sinusoidal waveform, so that the modulation is able to attain properties of a pure sine wave.

To implement a SPWM the pulses are modulated with an initial narrower widths which gradually get broader at the center of the cycle, and finally end being narrower at the end to finish the cycle.

To be more precise, the pulses begin with narrowest widths which gradually get broader with each subsequent pulses, and gets broadest at the center pulse, after this, the sequence continues on but with an opposite modulation, that is the pulses now gradually begin getting narrower until the cycle finishes.



This constitutes one SPWM cycle, and this repeats throughout at a particular rate as determined by the application frequency (usually 50Hz or 60Hz). Typically, SPWM is used for driving power devices such as mosfets or BJTs in inverters or converters.

This special modulation pattern ensures that the frequency cycles are executed with a  gradually changing average voltage value (also called the RMS value) , instead of throwing sudden Hi/low voltage spikes as normally witnessed in flat square wave cycles.

This gradually modifying PWMs in a SPWM is purposely enforced so that it closely replicates the exponentially rising/falling pattern of a standard sinewaves or sinusoidal waveform, hence the name sinewave PWM or SPWM.


Generating SPWM with Arduino


The above explained SPWM can be easily implemented using a few discrete parts, and also using Arduino which will probably enable you to get more accuracy with the waveform periods.

The following Arduino code can be used for implementing the intended SPWM for a given application.

Gosh!! that looks awfully big, if you know how to shorten it, you may certainly feel free to do it at your end.

// By Swagatam (my first Arduino Code)
 
void setup(){
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop(){

  digitalWrite(8, HIGH);
  delayMicroseconds(500);
  digitalWrite(8, LOW);
  delayMicroseconds(500);
  digitalWrite(8, HIGH);
  delayMicroseconds(750);
  digitalWrite(8, LOW);
  delayMicroseconds(500);
  digitalWrite(8, HIGH);
  delayMicroseconds(1250);  
  digitalWrite(8, LOW);
  delayMicroseconds(500); 
  digitalWrite(8, HIGH);
  delayMicroseconds(2000);
  digitalWrite(8, LOW);
  delayMicroseconds(500);
  digitalWrite(8, HIGH);
  delayMicroseconds(1250);
  digitalWrite(8, LOW);
  delayMicroseconds(500);
  digitalWrite(8, HIGH);
  delayMicroseconds(750);
  digitalWrite(8, LOW);
  delayMicroseconds(500);
  digitalWrite(8, HIGH);
  delayMicroseconds(500);
  digitalWrite(8, LOW);
  
 //...... 
    
digitalWrite(9, HIGH);
  delayMicroseconds(500);
  digitalWrite(9, LOW);
  delayMicroseconds(500);
  digitalWrite(9, HIGH);
  delayMicroseconds(750);
  digitalWrite(9, LOW);
  delayMicroseconds(500);
  digitalWrite(9, HIGH);
  delayMicroseconds(1250);  
  digitalWrite(9, LOW);
  delayMicroseconds(500); 
  digitalWrite(9, HIGH);
  delayMicroseconds(2000);
  digitalWrite(9, LOW);
  delayMicroseconds(500);
  digitalWrite(9, HIGH);
  delayMicroseconds(1250);
  digitalWrite(9, LOW);
  delayMicroseconds(500);
  digitalWrite(9, HIGH);
  delayMicroseconds(750);
  digitalWrite(9, LOW);
  delayMicroseconds(500);
  digitalWrite(9, HIGH);
  delayMicroseconds(500);
  digitalWrite(9, LOW);
  
}
//-------------------------------------//
In the next post I'll explain how to use the above Arduino based SPWM generator to make a pure sinewave inverter circuit....keep reading!


Please Share this Post:


2 comments:

  1. Hi sir,

    Its good to see you involving in arduino projects, I hope you will soon master in this field too :)

    I would like to point some errors in the code:

    1) we must not use pin 13 in arduino, unless we run out of pins, since it is always connected with LED, it will deviate our results. we can use other pins say 8 and 9.

    2) we cannot use delay for less than 1ms and we can't use decimal place in delay function like delay(1.25); . Instead use delayMicroseconds(); for instance delayMicroseconds(500); for 500us and delayMicroseconds(1250); for 1.25ms. You may replace all delay with this function.

    The above errors won't show up while compiling but, it affects our end result.

    Regards


    ReplyDelete
    Replies
    1. Thank you Girish,

      Although I hardly find time, I did this just to have some fun ... :)

      I will surely correct the above code as per your suggestions, as soon as possible..

      I appreciate your help very much and I am sure the readers will also get an opportunity to learn from this discussion.

      Thanks again, and keep up the good work!

      Delete

Please Note: If you find any DEAD link starting with http//:homemadecircuitsandschematics.blogspot.com, please replace it with http://www.homemade-circuits.com/ for making it visible and alive again.