Site icon Homemade Circuit Projects

Arduino SPWM Generator Circuit – Code Details and Diagram

waveform 2

waveform 2

In this post I have explained 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.

waveform 2

Video Demo

Arduino SPWM Generator Circuit

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);
}
//-------------------------------------//

How the Code Works

So here in this Arduino program, we are making SPWM which is sinusoidal pulse width modulation. We do this to create a kind of waveform that looks like a sine wave but is made using square pulses of different widths.

We start with the setup part like this:

void setup(){

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

}

This above code is just saying to Arduino that pin 8 and pin 9 both will be output pins. We will send HIGH and LOW signals from them. This setup runs only one time when Arduino starts.

Then we move to the loop part. Loop means everything inside it will run again and again forever.

void loop(){

Now inside this loop, first we start giving SPWM pulses from pin 8. The idea is to start with small pulses, increase them step by step to peak value and then reduce again to small.

We do this:

digitalWrite(8, HIGH);

delayMicroseconds(500);

digitalWrite(8, LOW);

delayMicroseconds(500);

This gives a thin pulse of 500 microsecond ON and 500 microsecond OFF. It is like sine wave just starting from zero.

Then:

digitalWrite(8, HIGH);

delayMicroseconds(750);

digitalWrite(8, LOW);

delayMicroseconds(500);

This one is fatter than before meaning sine wave is going higher.

Then:

digitalWrite(8, HIGH);

delayMicroseconds(1250);

digitalWrite(8, LOW);

delayMicroseconds(500);

Now pulse is even more fat. So sine wave is now rising more.

Then this:

digitalWrite(8, HIGH);

delayMicroseconds(2000);

digitalWrite(8, LOW);

delayMicroseconds(500);

This is the maximum peak pulse. So we are at top of sine wave.

After this we begin going down.

digitalWrite(8, HIGH);

delayMicroseconds(1250);

digitalWrite(8, LOW);

delayMicroseconds(500);

Then:

digitalWrite(8, HIGH);

delayMicroseconds(750);

digitalWrite(8, LOW);

delayMicroseconds(500);

And finally:

digitalWrite(8, HIGH);

delayMicroseconds(500);

digitalWrite(8, LOW);

So this above series of HIGH and LOW signals from pin 8 makes one full sine cycle using pulse widths. This is one full SPWM cycle for pin 8.

Now we do the exact same thing for pin 9, like this:

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);

}

So finally, this whole thing is a simple way to generate sine wave shaped signals using Arduino and square wave pulses of changing width.

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!

The above SPWM code was further improved by Mr Atton for enhancing its performance, as given below:

/*
This code was based on Swagatam SPWM code with changes made to remove errors. Use this code as you would use any other Swagatam’s works.
Atton Risk 2017
*/
const int sPWMArray[] = {500,500,750,500,1250,500,2000,500,1250,500,750,500,500}; // This is the array with the SPWM values change them at will
const int sPWMArrayValues = 13; // You need this since C doesn’t give you the length of an Array
// The pins
const int sPWMpin1 = 10;
const int sPWMpin2 = 9;
// The pin switches
bool sPWMpin1Status = true;
bool sPWMpin2Status = true;
void setup()
{
pinMode(sPWMpin1, OUTPUT);
pinMode(sPWMpin2, OUTPUT);
}
void loop()
{
// Loop for pin 1
for(int i(0); i != sPWMArrayValues; i++)
{
if(sPWMpin1Status)
{
digitalWrite(sPWMpin1, HIGH);
delayMicroseconds(sPWMArray[i]);
sPWMpin1Status = false;
}
else
{
digitalWrite(sPWMpin1, LOW);
delayMicroseconds(sPWMArray[i]);
sPWMpin1Status = true;
}
}
// Loop for pin 2
for(int i(0); i != sPWMArrayValues; i++)
{
if(sPWMpin2Status)
{
digitalWrite(sPWMpin2, HIGH);
delayMicroseconds(sPWMArray[i]);
sPWMpin2Status = false;
}
else
{
digitalWrite(sPWMpin2, LOW);
delayMicroseconds(sPWMArray[i]);
sPWMpin2Status = true;
}
}
}

Exit mobile version