How to make inverter with arduino and mosfet

Hi all,

Would love your help with a project I am working on. I'm working on an inverter project using irfz44n and bc547.My code, proteus design and output is down below. What can i do to get better output?

/*
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 = 3;
const int sPWMpin2 = 5;
// 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;
}
}
}



const int ArraySize = 13; // You need this since C doesn’t give you the length of an Array
const int sPWMArray[ArraySize] = {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 sPWMpin1 = 3;
const int sPWMpin2 = 5;

bool sPWMpin1Status = true;
#define makePinHIGH(b) (b)<13?(b)<8?PORTD|=1<<(b):PORTB|=1<<(b-8):(PORTC|=(1<<(b-13)))
#define makePinLOW(b) (b)<13?(b)<8?PORTD&=~(1<<(b)):PORTB&=~(1<<(b-8)):PORTC&=~(1<<(b-13))

void setup(){
  pinMode(sPWMpin1, OUTPUT);
  pinMode(sPWMpin2, OUTPUT);
}

void loop(){
  for (int i = 0; i < ArraySize; i++)  {
    if (sPWMpin1Status)    {
      makePinLOW(sPWMpin2);
      makePinHIGH(sPWMpin1);
      sPWMpin1Status--;
    }
    else    {
      makePinLOW(sPWMpin1);
      makePinHIGH(sPWMpin2);
      sPWMpin1Status++;
    }
    delayMicroseconds(sPWMArray[i]);
  }
}
1 Like


got this output after your code. cant i get better sine wave without using mosfet driver

look

You need to use higher rate PWM output. The original article is a bit of a joke to be honest: Arduino Pure Sine Wave Inverter Circuit with Full Program Code | Homemade Circuit Projects I guess the very crude output might sorta work for a light bulb, but otherwise seems rather useless.

I would suggest a PWM rate of at least 8kHz would be more appropriate.

ETA: Also your simulation lacks a capacitor or two across the output.

so do i have to use mosfet driver for that or not even do that with arduino

Actually I don't think you need better PWM, or even an Arduino. The Arduino is just being used as a simple oscillator which is overkill.

However, the output capacitors are definitely required.

Should I put capacitors on the AC output and what should their value be?

Assuming the circuit you are following is this one:


Then it appears to be 0.68uF 400V.

The point of having a simulation is that you can safely try different designs? If you actually build a circuit, please try not to electrocute yourself.

thx for respond i will try to use output capacitor. And yes simulation is for try safely then im planing to try it on breadboard

An inverter like that can simply be bitbanged at 50 Hz (or 60 Hz).
No need for sine wave.
The transformer will filter off the edges. The result will not be an exact sine, but close enough for most applications.

1 Like

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