Want to build a pure sine wave inverter? In this article, we design and build one with a PIC16F76 microcontroller. Pure sine wave inverters power motors, fans and electronics more cleanly than modified sine wave ones. By the end, you will understand how the inverter works, and you will have a working circuit, code and PCB.
- How a sine wave inverter works
- Generating SPWM with a PIC16F76
- Circuit diagram and code
- PCB and measured output waveform
⚠️Disclaimer:
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
Sine wave inverter mechanism
Most sine wave inverters use an H-bridge. It has four switches arranged in the shape of the letter H, as in this image:

The four switches are S1 to S4. By turning them on in pairs, one pair after the other, we make the current flow through the load first in one direction and then in the other:

This alternating current creates an alternating voltage across the load. Now, if the load is the primary of a step-up transformer, the secondary gives a much higher AC voltage than the battery. This is the basic idea of every inverter.
Next, if we drive the H-bridge with an SPWM (sinusoidal pulse-width modulation) signal, the transformer output is also an SPWM waveform. After filtering, it becomes a clean sine wave. In short, that is how a pure sine wave inverter works.
- IPS / inverter size calculator – pick the right IPS VA rating and battery for your load
- Voltage divider & ADC scaling calculator – scale a voltage into your MCU ADC range
SPWM generation for sine wave
Before we start, you should read my previous article to understand how we can use the PIC16F76 microcontroller to generate an SPWM signal.
Our main job, then, is to generate SPWM with the PIC microcontroller and use it to drive the H-bridge. For this, we need four signals: two 50 Hz square waves and two SPWM signals, like this:

The two 50 Hz signals are complementary, and each SPWM signal (20 kHz) is active only while its matching 50 Hz signal is high. Together, these four signals drive the H-bridge. The switches can be MOSFETs, IGBTs or bipolar transistors. For a low-voltage battery inverter, MOSFETs are the best choice.
In this design, the 50 Hz signals switch the upper MOSFETs, and the 20 kHz SPWM signals switch the lower ones. However, the microcontroller cannot drive the MOSFETs directly. Instead, gate driver circuits turn its 5 V signals into gate voltages of 10 V or more, which fully switch the MOSFETs on.



Circuit diagram
Here is the circuit diagram of our pure sine wave inverter with the PIC16F76:
All the MOSFETs are driven through TLP250 gate driver ICs. The PIC16F76 generates the four control signals, and the gate drivers switch the MOSFETs. The MOSFETs, in turn, drive the main power transformer, TR1, a 7.5 V : 250 V UPS transformer. On the secondary side, high-voltage capacitor C9 and the leakage inductance of the transformer form an LC filter. This filter removes the 20 kHz switching frequency and leaves a sine wave.
For output voltage feedback, we use a second small transformer, TR2. You could also sense the output with an electronic circuit, but the transformer keeps the design simple and isolated.
In addition, resistors R18 and R19 with capacitor C14 form a divider to measure the battery voltage. Finally, a switch on pin RA4 turns the inverter on and off.
Before we look at the code, read my previous article on how to generate an SPWM signal with the PIC16F76.
Coding
Here is the code that drives the H-bridge and produces the pure sine wave:
/*******************************************************************************
* Program for sine wave generation using PIC16F73 *
* Program Written by_ Engr. Mithun K. Das *
* MCU:PIC16F76; X-Tal:20MHz; mikroC pro for PIC v7.6.0 *
* Date:18-04-2020 *
*******************************************************************************/
unsigned char sin_table[32]={0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250, 249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25};
int duty=0;
float fb_constant=0.8;
bit alt;
bit inv_mask;
void Interrupt() iv 0x0004 ics ICS_AUTO
{
if (TMR2IF_bit == 1)
{
duty++;
if(duty>=32)
{
duty=0;
alt=~alt;
CCPR1L = 0;//inverter shutdown
CCPR2L = 0;
RC0_bit=0;
RC3_bit=0;
asm nop;
asm nop;
}
if(inv_mask)
{
if(alt)
{
CCPR1L = sin_table[duty]*fb_constant;
CCPR2L=0;
RC0_bit=1;
RC3_bit=0;
}
else
{
CCPR2L = sin_table[duty]*fb_constant;
CCPR1L=0;
RC0_bit=0;
RC3_bit=1;
}
}
else
{
CCPR1L = 0;//inverter shutdown
CCPR2L = 0;
RC0_bit=0;
RC3_bit=0;
}
TMR2IF_bit = 0;
}
}
int k=0;
unsigned int battery=0,output=0;
bit lcd_clr;
char msg[]="000";
void main()
{
TRISA=0xFF;//all input
TRISC = 0x00;//all output
PORTC=0x00;
RC4_bit = 1;//for LCD backlight
ADC_Init();
PR2 = 249;
CCP1CON = 0x4C;
CCP2CON = 0x4C;
TMR2IF_bit = 0;
T2CON = 0x2C;
TMR2IF_bit = 0;
TRISC = 0;
TMR2IE_bit = 1;
GIE_bit = 1;
PEIE_bit = 1;
UART1_Init(9600);
while(1)
{
RC4_bit = 1;
//switch operation
if(!RA4_bit)
{
inv_mask=1;
lcd_clr=1;
}
else
{
inv_mask=0;
if(lcd_clr)
{
lcd_clr=0;
Delay_ms(1000);
}
}
//read output
output=0;
for(k=0;k<20;k++)
{
output+=ADC_Get_Sample(3);
Delay_ms(1);
}
output/=20;
if(output<100)
{
if(fb_constant<0.7)fb_constant+=0.1;
}
if(output>115)
{
if(fb_constant>0.5)fb_constant-=0.1;
}
}
}// end
Now, let us go through the code step by step.
Sine wave generation (interrupt routine)
sin_table[32]: first, this array stores 32 pre-calculated values for one half-cycle of the sine wave. Each value sets the PWM duty cycle for one step.- Timer2 interrupt: Timer2 sets the 20 kHz PWM frequency, and its interrupt moves to the next sine value.
duty: the index intosin_table. It steps through the sine values one by one.alt: toggles each timedutyreaches 32, at the end of every half-cycle. It selects which half of the H-bridge (CCP1 or CCP2) is active.inv_mask: enables or disables the output. When it is 1, the inverter runs; when it is 0, all outputs stop.fb_constant: scales the sine table to set the output amplitude. The feedback loop inmain()adjusts it.- PWM generation (CCP1 and CCP2): in the interrupt, the CCP modules output PWM with a duty cycle equal to the sine table value multiplied by
fb_constant. - H-bridge control (RC0 and RC3): these pins drive the 50 Hz upper switches. The
altbit makes sure only one diagonal of the bridge is active at a time, which creates the alternating output. - Shutdown: at the end of each half-cycle, or when
inv_maskis 0, the PWM outputs go to zero.



Main function
- Initialization:
- First, it sets up the I/O pins (PORTA as inputs, PORTC as outputs).
- Next, it initialises the ADC.
- Then it configures Timer2 for the PWM frequency.
- After that, it puts CCP1 and CCP2 into PWM mode.
- It also enables the Timer2 interrupt and global interrupts.
- Finally, it initialises UART1 for serial monitoring.
- Main Loop:
- On/off switch (RA4): reads RA4 and sets
inv_mask, so the switch turns the inverter on or off. - Output voltage (ADC channel 3): reads the feedback transformer output.
- Feedback control: if the reading is below 100, the code increases
fb_constant; if it is above 115, it decreases it. In this way, the output voltage stays inside a band. - LCD and UART output: the full project file also shows readings on an LCD and sends them over the UART.
- On/off switch (RA4): reads RA4 and sets
How to improve this design
- Better regulation: the band feedback is simple. A PI controller would hold the output voltage much more steadily as the load changes.
- Dead time: add a short blanking time when the 50 Hz signals change over, so the upper and lower MOSFETs of one leg are never on together.
- Higher resolution: a larger sine table (64 or 128 steps) gives a smoother waveform with lower harmonics.
- Output filter: for a cleaner output under heavy loads, add a dedicated output inductor in addition to the transformer leakage inductance.
- Protection: add over-current, short-circuit, low-battery and over-temperature shutdown before you use it with real loads.
- Integer maths: replace the floating-point multiply in the interrupt with scaled integer maths, which runs much faster on an 8-bit PIC.
In summary, this code shows the core of a working pure sine wave inverter. With better feedback, dead time and protection, it can grow into a robust product.
Learn PIC programming step by step:
- Learning PIC Microcontrollers Programming in C: Chapter 1
- Learning PIC Microcontrollers Programming in C: Chapter 2
- Learning PIC Microcontrollers Programming in C: Chapter 3
- Learning PIC Microcontrollers Programming in C: Chapter 4
- Improve your code learning PIC MCU in C: Chapter 5
Request this file on WhatsApp for free!
PCB
I use Sprint-Layout to design PCBs for most small projects. Here is the PCB design for this inverter:
Result
After building the PCB, I tested the inverter, and the result was excellent. Here is the finished project:
The waveshape of our sine wave inverter:
My oscilloscope’s voltage scale is not accurate, so I measured the output with a multimeter: a clean 210 V AC. However, because the transformer is small, the output voltage drops noticeably above about 100 W of load.
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
Read more:
- Learning PIC MCU Chapter 7: How to interface buttons with microcontrollers
- Learning PIC MCU Chapter 6: Interfacing Passive Components with Microcontrollers
- How to reduce noise from DC motor
- Interfacing External EEPROM with PIC microcontroller
Some of our inverters:



Featured products:


Related on LabProjectsBD
Planning an inverter, UPS or power converter product? We design power electronics, embedded firmware and production-ready PCBs for clients. Message us on WhatsApp or use the contact form.





68 Comments
Asimi · 26/07/2020 at 2:57 am
Good day Mr Mithun. I have been writing to severally but the message didn’t deliver and I decided to try it again today. Pls I really appreciate all your efforts in all the post created to help individual who want to learn. May God Almighty reward and give you more knowledge. However, I have been facing a lot of challenges in the sine inverter you have posted. (1) how did you get CCP1CON= CCP2CON= 0x4C; = 0b01001100; from the table, in datasheet. I have spent almost completely full day, I couldn’t get it. Pls show me how you got it. (2) how did you get your PR2 = 251, when using “Fosc = 20MHz and Fpwm = 20Kz respectively. When calculated, I got 249 (I e PR2= 249 not 251). (3) pls, explain to me, how did i develop small code to charge the inverter via mains ac sensing. Is it to keep the 2 of ccp1 and ccp2 active and deactivate the low frequency (50Hz) signal or keep low frequency (50Hz) actively and deactivate ccp1 and ccp2. pls, guide me
Mithun K. Das · 27/07/2020 at 4:37 am
Yes it was 249 if calculated. I was testing tuning a little bit which gave me good result for my circuit and transformer. You can check on your side.
djalltra · 20/10/2020 at 12:07 pm
a million thanks for this tutorial I love your tutorials
Mithun K. Das · 20/10/2020 at 2:12 pm
Thank you too
Emman · 11/04/2021 at 10:52 am
Thank you for this article.
How can we add battery charging code here?
MKDas · 11/04/2021 at 11:18 am
This one is a basic one. If it is utilized and reformated in flat coding, all standard options can be added. I’ll try to do something with this later. Stay with us & subscribe. Maybe in future, you’ll get more useful posts.
enrique · 01/01/2021 at 11:05 pm
excelente, como todos sus trabajos ,desde Colombia gracias
Godwin · 26/02/2021 at 9:02 pm
Please,hex file I didn’t have the compiler
Mithun K. Das · 27/02/2021 at 5:10 am
Install the compiler. Arrange your code. If it shows demo limit, email the complete project file to [email protected]
Ehi · 11/08/2021 at 1:55 am
The drop in voltage when more than 100w is appliedhow can it be corrected
MKDas · 11/08/2021 at 11:39 am
If it is redesigned with feedback then it will work.
manh · 08/05/2021 at 6:38 am
Please help,code file I didn’t have the compiler
MKDas · 08/05/2021 at 7:42 am
Updated
Antonio Franca · 07/07/2021 at 12:58 am
Mithun, thank you very much for your prompt attention. I kindly ask if it is possible for you to change and send me the lines of code referring to the LCD.
Sincerely,
Antonio France
MKDas · 07/07/2021 at 5:08 am
I don’t know the pin configuration of the easy pic. Kindly change it yourself, please.
Antonio França · 07/07/2021 at 11:46 am
Hello Mithun, good morning. Here in Brazil it is 8:45 am
I don’t know which country you reside in.
Returning to the question of your project, I would like if possible to make available to everyone the schematic diagram with the connections with the LCD.
thanks
Antonio França
MKDas · 07/07/2021 at 11:48 am
the diagram is given in the article. Kindly check.
Franca · 07/07/2021 at 1:22 pm
Which Article?
MKDas · 07/07/2021 at 2:40 pm
This article. Kindly check again.
Antonio França · 07/07/2021 at 10:06 pm
Dear MKDas,
Unfortunately I didn’t see in your schematic any connection of the PIC16F73 with any LCD
MKDas · 08/07/2021 at 1:02 am
Oh sorry, I did not check that this one was LED-based. There is another one with LCD based. I’ll make the hardware ready soon, then write on that.
MUNIKO · 17/08/2021 at 4:54 pm
hi,i want to build your circuit but help me to have protections, battery low and overvoltage,short circuit and display,
MKDas · 17/08/2021 at 8:25 pm
First, make this basic one. Then you can add features easily if you understand this.
Muniko · 19/10/2021 at 10:04 pm
Is there a feeback in this circuit?
MKDas · 21/10/2021 at 11:47 am
no
Saddam · 01/11/2021 at 2:06 am
Dear sir aponi ki pic16f73 ic er ips er jonno peogrrame kore dite parben…….
Lab Projects BD · 01/11/2021 at 10:21 am
The same program, Just change the IC in the compiler.
Chimezie · 09/11/2021 at 3:38 pm
Hi please which compiler did you make use of.. and please when are you adding feedback to the design thanks
Lab Projects BD · 09/11/2021 at 9:17 pm
I mentioned in the article. Please read carefully.
Paul · 09/11/2021 at 5:02 pm
hi nice work please i need your assistance to add charging mode to this great project thanks [email protected]
Lab Projects BD · 09/11/2021 at 9:17 pm
In that case, I’ll suggest you use dsPIC.
Prasanjith · 25/03/2022 at 4:07 pm
In your schematic Where is the Low pass filter after generating PWM signals?
MKDas · 26/03/2022 at 12:11 pm
The transformer itself is a big inductor. And just add a capacitor with the output in parallel. So there is a LC filter….
prestley johnson · 10/04/2022 at 1:17 am
hi MKDas, kindly upload video for the spwm inverter.
MKDas · 10/04/2022 at 7:20 pm
I’ll, but not this one. With dsPIC one. 🙂
Achira · 14/04/2022 at 12:30 pm
Dear MKdas,
I would like to make the inverter project. Let me have the PCB pattern as a PDF so that I will be able to make it with the toner transfer method. Also a crystal shown in your assembled photo but its not indicated in the circuit diagram ?
Thank you,
Achira
[email protected]
MKDas · 14/04/2022 at 4:36 pm
Crystal is not shown in the proteus circuit diagram but in real, you have to use one.
Eniola · 21/05/2022 at 5:34 pm
The greatest blog ever,love 💕💕💕 to be in this blog .may take Lord continue to increase your wisdom.love you sir.sir,I want protection for this circuit or can I sense overload for the some port use as input? which port can I sense with overload and short circuit? looking forward to your quick response.i have subscribed to this blog
MKDas · 21/05/2022 at 8:57 pm
You can do anything you want if you understand the working principle and know how to add codes.
Love your blog and May the Lord bless you for helping people like me. Love you sir. Sir, how can I add lcd display to display battery sense, inverter on and off · 07/06/2022 at 10:55 pm
Love your blog and May the Lord bless you for helping people like me. Love you sir. Sir, how can I add lcd display to display battery sense, inverter on and off
MKDas · 08/06/2022 at 9:51 am
Thank you. Yes you can add the LCD display. Simply use the LCD functions from the library. But only one thing you need to keep in mind about the microcontroller’s range of capacity.
Caleb · 17/07/2022 at 4:56 pm
I love your blog keep up the good work. I will update the code and add feedback, short circuit, overload battery low and charging and will upload foe everyone
Mr.PP · 22/07/2022 at 8:22 am
I want the system Soft Start Amplitude from 0-220v how to do? Please advise.
MKDas · 22/07/2022 at 12:34 pm
gradually increase the PWM
Abul Hassan · 09/08/2022 at 8:54 pm
Can this project work in pic16f72 ic?
MKDas · 09/08/2022 at 9:04 pm
No.
Faizan · 06/09/2022 at 11:05 pm
Great
Faizan · 06/09/2022 at 11:06 pm
Can this project work in pic16f873 ic?
MKDas · 07/09/2022 at 4:09 pm
yes, but configuration changes are required.
EMERITUS · 17/01/2023 at 1:18 am
I finally finished the project have completed the feedback section and can power any heavy load with no output drop, added temperature sensor for shutdown while inverting and charging, added charging section to it can charge upto 15A current and doesnt select transformer, added battery low shutdown, mainsdetect and charges commence after 10secs, trickle charger, overload protection, and shut circuit protection all in the pic16f73. you can contact me for the full c-file written in mikroc for pic
MKDas · 17/01/2023 at 10:27 am
Fantastic
Olly · 13/05/2023 at 1:26 pm
Hi, good work and more grace to your elbow. Please can you help with your developed design? Thanks in advance.
MKDas · 13/05/2023 at 3:02 pm
This one is for learning purpose and its open for all. But for professional ones, I recommend dsPIC. And sorry, professional ones are not open source.
Olly · 03/08/2023 at 12:44 pm
Hi Mr Emeritus, you have done a great job to compliment the author of this post.
Please assist me with your design circuit and code just as the author of this post has done. My email is [email protected]. Thanks for the good job.
MKDas · 12/08/2023 at 6:15 pm
plz write what you are asking for.
paul · 14/10/2023 at 5:35 pm
EMERITUS plese i need it
Snehrish · 13/11/2023 at 2:00 am
EMERITUS, please can you email so we can talk about it
[email protected]
MKDas · 13/11/2023 at 11:45 am
if you need to discuss anything, WhatsApp me.
CDTES · 23/06/2024 at 3:09 am
Can I get your whatsapp number, I need the professional one, what will it cost me
MKDas · 23/06/2024 at 1:21 pm
given in the subscription section.
kuldip · 28/03/2023 at 2:28 pm
Sir, PWM signal is not generating from the microcontroller as you have shown here, i have simulated the circuit in proteus but didn’t work, what to do? Please help, sir
MKDas · 29/03/2023 at 11:56 am
check project settings
Alfrever · 25/06/2023 at 9:53 pm
Hello, your design looks very interesting and thanks for sharing it. I am a beginner in programming, what parts of the code would I have to modify to change the frequency to 60Hz?
MKDas · 02/07/2023 at 7:28 pm
Thanks. Edit timer interrupt settings.
paulo obregon · 28/08/2023 at 8:11 pm
ola gostaria de operar em 60 hz ajude-me
MKDas · 04/09/2023 at 8:29 pm
edit timing
Arif · 13/05/2024 at 4:45 pm
Sir, is dead time pwm possible with this IC. For Puspool Transformer ??
MKDas · 16/05/2024 at 1:54 pm
not possible directly. but with code not impossible.