How to write SPWM program for 5 level inverter in arduino

I am making an 5 level inverter using arduino so for that i have written simple arduino program for switching the mosfet but problem i am facing in this is it has low switching frequency so i want to write program using spwm technique now i dont know where to start can any one help me out.

This is the simple code which i had made. Untitled (M3j3iQV0o2) - PasteCode.io

OP's code:

float f=50;
float t=(1/f)*1000000;
float a1=17.9178;
float a2=57.4096;
float a1p1=(a1*t)/360;
float a1p2=(a2*t)/360;
float d=5;

int pin1=9;
int pin2=2;
int pin3=3;
int pin4=4;
int pin5=5;
int pin6=6;
int pin7=7;
int pin8=8;

void setup(){
  Serial.begin(9600);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(pin8, OUTPUT);
}

void loop(){
 
  
  digitalWrite(pin1,HIGH);
  digitalWrite(pin2,HIGH);
  digitalWrite(pin5,HIGH);
  digitalWrite(pin6,HIGH);
  delayMicroseconds(a1p1-d);                                                                                                                                                                                                                                   

  digitalWrite(pin2, LOW);
  delayMicroseconds(d);
  digitalWrite(pin4,HIGH);
  delayMicroseconds(a1p2-a1p1-d);

  digitalWrite(pin6,LOW);
  delayMicroseconds(d);
  digitalWrite(pin8,HIGH);
  delayMicroseconds(t/2-(2*a1p2)-d);

  digitalWrite(pin5,LOW);
  delayMicroseconds(d);
  digitalWrite(pin7,HIGH);
  delayMicroseconds(a1p2-a1p1-d);

   digitalWrite(pin1,LOW);
   delayMicroseconds(d);
   digitalWrite(pin3,HIGH);
   delayMicroseconds((2*a1p1)-d);

   digitalWrite(pin4,LOW);
   delayMicroseconds(d);
   digitalWrite(pin2,HIGH);
   delayMicroseconds(a1p2-a1p1-d);

   digitalWrite(pin8,LOW);
   delayMicroseconds(d);
   digitalWrite(pin6,HIGH);
   delayMicroseconds(t/2-(2*a1p2)-d);

   digitalWrite(pin7,LOW);
   delayMicroseconds(d);
   digitalWrite(pin5,HIGH);
   delayMicroseconds(a1p2-a1p1-d);

   digitalWrite(pin3,LOW);
   delayMicroseconds(d);
   digitalWrite(pin1,HIGH);
   delayMicroseconds(a1p1);
}

I would suggest replace the digialWrites with direct port Manupulation, use a lookup table for the port IO values and one of the builtin timers in place of delay.

hope that helps....

Something like this maybe:
[compiles, NOT tested]

/*arduino UNO*/
byte switching_sequence[10] = {0b00011100,
                               0b00011100,
                               0b00111000,
                               0b01010100,
                               0b00111000,
                               0b00011100,
                               0b00011100,
                               0b11000100,
                               0b10101000,
                               0b11000100,
                              };
int step = 0;

ISR(TIMER0_COMPA_vect) {   //This is the interrupt request
  PORTD = (PORTD & 0b00000011) | switching_sequence[step++];
  if (step == 10) step = 0;
}
void setup() {
  // set pins 2 to 7 as outputs
  DDRD = DDRD | 0b11111100;

  //activate Timer0 interrupt request every 1ms
  TCCR0A |= (1 << WGM01); //Set the CTC mode
  OCR0A = 0xF9;          //Set the value for 1ms
  TIMSK0 |= (1 << OCIE0A); //Set the interrupt request
  sei();                 //Enable interrupt
  TCCR0B |= (1 << CS01); //Set the prescale 1/64 clock
  TCCR0B |= (1 << CS00);
}

void loop() {


}

The above sequence is based on this:

hope that helps....

Thanks for the reply Sherzad

I have tried your code in Proteus but i did not got the expected output.

I never guaranteed that me code would work! :wink:
However, looking at the schematic your posted, the pin-to-MOSFET assignments are not correct. For the code I provided, the assigments for your schematic IMHO should be

Q1 - pin7
Q2 - pin4
Q3 - pin6
Q4 - pin3
Q5 - pin5
Q6 - pin2

hope that helps....

hi sherzaad

I have tried this by changing the pins but still i did not get the output.

khalifa_007:
hi sherzaad

I have tried this by changing the pins but still i did not get the output.

Fair enough...

On my side, the output pattern is right, at least on paper! :wink:

Guess it's time for YOU to figure out the ACTUAL sequence the MOSFETs need to turn ON at each step in your circuit and mod values in the 'switching_sequence' array accordingly. The issue most likely lies there as the pattern is 'almost' there! :slight_smile:

I hope you took the trouble to try and understand how the sample code I proposed works. If not, time for you to get on it!

I cannot test it, so the troubleshooting part is ALL up to you!

Good luck!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.