Previously, I wrote a note on how to build an SPWM inverter with Arduino. Here I have modified the same circuit to include new components and I have also used new code for the Arduino. This diy inverter circuit is supposed to a pure sine wave inverter but I am not sure so I look forward for comment on this.
Hardware and Circuit for Arduino Inverter
The modified Arduino-based inverter design introduces several key improvements over the earlier version(see SPWM inverter with Arduino) to enhance performance, reliability, and output waveform quality. By adding 10 kΩ pull-down resistors to the MOSFET gates, the circuit ensures that the IRFL44N MOSFETs remain fully off when not driven, preventing accidental turn-on due to gate capacitance or floating signals—something the previous design with IRF540N lacked, potentially causing unwanted switching or heat buildup. The switch from IRF540N to IRFL44N, a logic-level MOSFET, ensures better compatibility with the 2N2222A driver stage, allowing the gates to turn on fully even with lower drive voltages. Additionally, incorporating a Pi-network low-pass filter (L = 2.8 H, C = 5.7 µF × 2) after the transformer greatly improves the output waveform by removing high-frequency switching noise from the SPWM signals, resulting in a smoother and more sinusoidal AC output. These enhancements collectively lead to greater efficiency, reduced electromagnetic interference (EMI), improved transformer performance, and a more stable and cleaner inverter output suitable for powering sensitive AC devices.
I also like to add that a 12V to 5V buck converter is used here to power the Arduino. I have seen circuits like where a linear regulator IC like LM7805 is used to get 5V for Arduino from 12V. This is not efficient way of using the available energy because there will loss of power from the linear regulator LM7805 in form of heat. Heat sinks will be required. So it is better use the buck converter.
Arduino Programming for Inverter
The code for Arduino to control the switches for inverter operation is below.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
const int SpwmArryValues = 256;
uint8_t SpwmArry[SpwmArryValues];
volatile int sineIndex = 0;
bool togglePhase = false;
void setup() {
// Generate 256-point full sine wave table (0–254)
for (int i = 0; i < SpwmArryValues; i++) {
SpwmArry[i] = 127 + int(127 * sin(2 * PI * i / SpwmArryValues));
}
// Set PWM output pins
pinMode(9, OUTPUT); // OC1A (D9)
pinMode(10, OUTPUT); // OC1B (D10)
// Timer1 - Fast PWM 8-bit, Non-inverting mode on OC1A and OC1B
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10); // Fast PWM 8-bit
TCCR1B = _BV(WGM12) | _BV(CS10); // No prescaler (~31.25kHz)
// Timer2 - CTC mode, generates interrupt at ~2kHz (for 50Hz SPWM cycle)
cli(); // Disable global interrupts
TCCR2A = _BV(WGM21); // CTC mode
TCCR2B = _BV(CS22) | _BV(CS20); // Prescaler = 128
OCR2A = 124; // 16MHz / 128 / (124+1) ˜ 1kHz
TIMSK2 = _BV(OCIE2A); // Enable Timer2 Compare Match A interrupt
sei(); // Enable global interrupts
}
ISR(TIMER2_COMPA_vect) {
// Alternate phase every 128 steps (10ms for 50Hz)
if (togglePhase) {
OCR1A = SpwmArry[sineIndex]; // D9 active
OCR1B = 0;
} else {
OCR1A = 0;
OCR1B = SpwmArry[sineIndex]; // D10 active
}
sineIndex++;
if (sineIndex >= SpwmArryValues / 2) {
sineIndex = 0;
togglePhase = !togglePhase;
}
}
void loop() {
// Nothing needed here. All handled in ISR.
}
This Arduino code generates a pure sine wave approximation using Sinusoidal PWM (SPWM) by leveraging Timer1 in 8-bit Fast PWM mode to output high-frequency PWM signals on pins D9 and D10, which are alternately activated every half sine cycle (~10ms) to drive a push-pull transformer configuration suitable for inverter applications. A 256-point sine table modulates the PWM duty cycle smoothly from 0% to 100% and back, simulating a 50 Hz sine wave across the transformer windings. Timer2 is configured in CTC mode to trigger interrupts at a steady rate (~2 kHz) to step through the sine table. This approach enables high-frequency PWM generation for efficient switching and smoother transformer output, ideal for building low-cost inverters. For more details on how Fast PWM is configured using Timer1, see Programming Arduino Timer1 in Fast PWM Mode. To learn about Timer2’s configuration, refer to How to Use Arduino Timer2 in Fast PWM Mode. For background on how PWM controls power devices like motors or MOSFETs, check out Speed Control of DC Motor using PWM with Arduino.
Video Demonstration
The following video shows simulation of the the Arduino based pure sine wave inverter circuit.