Pure sine wave inverter using PIC16F76

Published by MKDas on

In this article, we are going to learn how to make a Pure sine wave inverter using PIC16F76. Sine wave inverter is one of the most wanted projects in the power electronics field. Making a pure sine wave inverter is a very pleasurable job too. You can make a pure sine wave inverter for yourself and use it, even you can make a bigger machine with this after learning this project carefully. So stay connected and keep reading…

Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept as open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guide feel free to comment below, the author will try to help you. Thanks.

Sine wave inverter mechanism:

In most of the cases in sine wave inverters, an H-bridge driving configuration is used. There are four sets of switches that are connected like an H like this image:

H-Bridge configuration

Here there are 4 switches S1 to S4. By switching in the sequential way we can generate an alternating current flow through the load. It is like this:

H-Bridge driving way

An alternating current flow generates an alternating voltage across the load. And if this load is a step-up transformer, then we can get an alternating voltage across the secondary of the transformer which will be higher than the supply voltage. This is the basic configuration of the inverter.

Now if you drive this H-bridge with an SPWM signal, an SPWM can be found on the higher voltage end of the transformer. After filtering that, we can easily get a pure sine wave. This is the concept of a sine wave inverter.

SPWM generation:

Before we start, you should read my previous article to understand how we can use the PIC16F76 microcontroller to generate an SPWM signal.

Now our main job is to generate SPWM using a PIC micro-controller. Then we can use this SPWM signal to drive our H-bridge. But to drive the H-bridge, we need two push-pull types SPWM and two push-pull signals like this:

SPWM signal for the inverter
SPWM signal

Here there are two signals at 50Hz in a push-pull configuration and the other two are SPWM signals (20KHz) which is followed by the previous 50Hz signals. These 4 signals are used to drive the H-bridge. The switches can be MOSFET, IGBT, or transistors. We have to choose a suitable one. For inverters, we can use MOSFET as our switching device.

The upper arms of the bridge are switched by a 50Hz signal. And the lower arms are switched by 20KHz modulated signals. We can not use these signals directly to switch the MOSFETs. We need gate driving circuits to convert the signal from micro-controller to switch MOSFET. These gate driving circuits simply convert the MCU’s 5V signal into a 10V+ signal which can be used to switch MOSFETs.

Circuit diagram:

So the circuit diagram for our Pure sine wave inverter using PIC16F76:

Sine wave inverter circuit diagram

Here you can see that all the MOSFETs are driven with the help of TLP250 gate driver IC and associated circuitry. The control MCU is PIC16F76 which is generating 4 sets of signals and these signals are triggering the MOSFETs with the help of gate driver IC. The MOSFETs are driving our main power transformer TR1. Which is actually a UPS transformer rated 7.5v:250v. At the secondary terminal, a high voltage capacitor C9 is working as our filter (C), and the transformer itself working as an inductor (L). So an L-C filter is formed in our circuit.

There is another option to add feedback in our inverter which is designed with a transformer. Although it can be made of electronic circuit only but to keep it simple to understand I kept transformer TR2.

Also to measure the battery voltage, I used a voltage divider circuit with R18 & R19+C14. And to turn the inverter on/off, a switch is used which is connected to the RA4 pin of our MCU.

Before we start the coding, you should read my previous article to understand how we can use the PIC16F76 microcontroller to generate an SPWM signal.

Coding:

Here is our code to drive the H-bridge from where our expectation is to get a 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

Here I used a sine table “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};” to generate the SPWM and Timer1 to make the 50Hz signal as well as 4 push-pull type signals (2 pairs).

In the timer interrupt section, this timing and signal generation is done. Here as we need to regulate the output as a feedback system so a feedback variable is used named ‘fb_constant’. This floating-point variable is used to reduce or increase the SPWM duty inside the timer interrupt. This makes a smooth feedback system for sine wave inverter.

We are using a switch to turn the inverter on/off which is controlled by the variable ‘inv_mask’ inside the interrupt routine.

Download the hex file from here for free!

PCB:

I use Sprint-Layout to design PCB for most of the small projects. Here is the PCB design for our inverter.

PCB for sine wave inverter

Result:

After completing the PCB I tested this project. The result was awesome. Project image:

Pure sine wave inverter

Waveshape:

Output wave shape

My oscilloscope’s voltage sensing is not actual. I measured with multi-meter, I got 210V AC output which was good. As a small transformer used here, loading over 100Watts significantly reduces output voltage.

For more useful articles, don’t forget to subscribe. Thanks.

Don’t forget to subscribe for the next update.

Loading

JLCPCB – Only $2 for PCB Prototype (Any Color)

24 Hours fast turnaround, Excellent quality & Unbeatable prices

$18 Welcome Bonus for new registrations Now!!! https://jlcpcb.com


Check this out: 5 coolest multimeters you can buTop 5 Digital Multimeters for beginners


MKDas

I'm Mithun K. Das; B.Sc. in EEE from KUET, Bangladesh. Blog: https://labprojectsbd.com. "First, electronics was my passion, then it was my education, and finally, electronics is now my profession." I run my own electronics lab, M's Lab (https://mlabsbd.com). Where I work with the creation of new products from ideas to something in real life. Besides this is my personal blog where I write for hobbyists and newcomers in the electronics arena. I also have a YouTube channel where I publish other helpful videos, you can find the link inside the articles. I always try to keep it simple so that it becomes easy to understand. I hope these will help them to learn electronics and apply the knowledge in their real life.

23 Comments

  • Asimi · July 26, 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 · July 27, 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 · October 20, 2020 at 12:07 pm

    a million thanks for this tutorial I love your tutorials

      Mithun K. Das · October 20, 2020 at 2:12 pm

      Thank you too

        Emman · April 11, 2021 at 10:52 am

        Thank you for this article.
        How can we add battery charging code here?

          MKDas · April 11, 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 · January 1, 2021 at 11:05 pm

    excelente, como todos sus trabajos ,desde Colombia gracias

      Godwin · February 26, 2021 at 9:02 pm

      Please,hex file I didn’t have the compiler

        Mithun K. Das · February 27, 2021 at 5:10 am

        Install the compiler. Arrange your code. If it shows demo limit, email the complete project file to mithun060@gmai.com

          Ehi · August 11, 2021 at 1:55 am

          The drop in voltage when more than 100w is appliedhow can it be corrected

            MKDas · August 11, 2021 at 11:39 am

            If it is redesigned with feedback then it will work.

    manh · May 8, 2021 at 6:38 am

    Please help,code file I didn’t have the compiler

    Antonio Franca · July 7, 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 · July 7, 2021 at 5:08 am

      I don’t know the pin configuration of the easy pic. Kindly change it yourself, please.

        Antonio França · July 7, 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 · July 7, 2021 at 11:48 am

          the diagram is given in the article. Kindly check.

    Franca · July 7, 2021 at 1:22 pm

    Which Article?

      MKDas · July 7, 2021 at 2:40 pm

      This article. Kindly check again.

        Antonio França · July 7, 2021 at 10:06 pm

        Dear MKDas,

        Unfortunately I didn’t see in your schematic any connection of the PIC16F73 with any LCD

          MKDas · July 8, 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 · August 17, 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 · August 17, 2021 at 8:25 pm

      First, make this basic one. Then you can add features easily if you understand this.

    Leave a Reply

    Avatar placeholder

    Your email address will not be published. Required fields are marked *