Sine Wave inverter using PIC16F76

Published by MKDas on

In this article, we are going to learn how to make a sine wave inverter with PIC16F76 micro-controller. 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. Skill required to work with electricity. Do work with your own risk. Author will not be responsible for any misuse or harmful act. This website contents is also copyright protected. Anything copied and directly posted in your website and claiming it as yours is prohibited and nonsensical. Author published all the articles as opensource to help you making your project and learning purpose only. Learn and make one for yourself. If need any help feel free to ask the author. Author will be helpful to you. Thanks.

Sine wave inverter mechanism:

Most of the cases in sine wave inverter, a H-bridge driving configuration is used. There are four set of switches which is connected like a H like this image:

H-Bridge configuration

Here there are 4 switches S1 to S4. By switching in 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 inverter.

Now if you drive this H-bridge with SPWM signal, a 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 PIC16F76 micro-controller to generate SPWM signal.

Now our main job is to generate SPWM using 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 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 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 10V+ signal which can be used to switch MOSFETs.

Circuit diagram:

So the circuit diagram for our inverter will be this:

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 set 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 transformer itself working as an inductor (L). So a L-C filter is formed in our circuit.

There are another option to add a 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.

Coding:

Here is our code to drive the H-bridge from where our expectation is to get 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, these 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 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 variable ‘inv_mask’ inside interrupt routine.

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

You can download the PCB files to make your PCB.

Result:

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

Pure sine wave inverter

Wave shape:

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.


MKDas

Mithun K. Das; B.Sc. in EEE from KUET. Blog: https://labprojectsbd.com

7 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

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

Leave a Reply

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