Automatic Power Factor Controller using Microcontroller

Automatic Power Factor Controller using Microcontroller

The thirst for new sources of energy is unquenchable, but we seldom realize that we are wasting a part of the electrical energy every day due to the lagging power factor in the inductive loads we use. Hence there is an urgent need to avoid this wastage of energy.

Before getting into the details of Power factor correction, lets just brush our knowledge about the term “power factor”. In simple words power factor basically states how far the energy provided has been utilized. The maximum value of power factor is unity. So closer the value of P.F to unity, better is the utility of energy or lesser is the wastage. In electrical terms Power factor is basically defined as the ratio of the active power to reactive power or it is the phase difference between voltage and current. Active power performs useful work while Reactive power does no useful work but is used for developing the magnetic field required by the device.

Most of the devices we use have power factor less than unity. Hence there is a requirement to bring this power factor close to unity. Here we are presenting a prototype for automatic power factor correction using PIC Microcontroller.

Circuit Diagram

Automatic Power Factor Controller using PIC Microcontroller Circuit Diagram
Automatic Power Factor Controller using PIC Microcontroller Circuit Diagram

Working

Comparator Section

The 230 V, 50 Hz is step downed using voltage transformer and current transformer is used to extract the waveforms of current. The output of the voltage transformer is proportional to the voltage across the load and output of current transformer is proportional to the current through the load. These waveforms are fed to Voltage Comparators constructed using LM358 op-amp. Since it is a zero crossing detector, its output changes during zero crossing of the current and voltage waveforms. These outputs are fed to the PIC which does the further power factor calculations.


Microcontroller Section

PIC 16F877A microcontroller is the heart of this Automatic Power Factor Controller, it find, displays and controls the Power Factor.. To correct power factor, first we need to find the current power factor. It can be find by taking tangent of ratio of time between zero crossing of current and voltage waveforms and two successive zero crossing of voltage waveform. Then it displays the calculated power factor in the 16×2 LCD Display and switches ON the capacitors if required.

Correction Section

When load is connected the power factor is calculated by the PIC microcontroller. If the calculated power factor is less than 0.9 then the relay switches on the capacitor. The relays are switched using ULN2003 which is basically a driver IC. ULN2003 consists of seven DARLINGTON PAIRS.
The current lead in capacitor compensates the corresponding current lag which is usually present in loads. Hence the phase difference between the current and voltage will be reduced.

Power Factor Correcting capacitor connected parallel to load through relay, if the relay is energized by microcontroller it will connect  the capacitor parallel with load, if relay deenergized it will remove the capacitor from the load. When the resistive load is on the power factor will be near to unity so the microcontroller doesn’t energize the relay coil. When the inductive load is on the power factor decrease now the microcontroller energize the relay coil in order to compensate the excessive reactive power. Hence according to the load the power factor is corrected.

MikroC Program

//LCD Module Connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//End LCD Module Connections
int powerFactor()
{
  int a=0,b=0,t=0,x=0;
  float tm,pf;
  TMR1L=0;
  TMR1H=0;
  do
  {
    if(PORTA.F0 == 1)
    T1CON.F0 = 1;
    else if(PORTA.F0 == 0 && T1CON.F0 == 1)
    {
      T1CON.F0 = 0;
      break;
    }
  }while(1);
  a = (TMR1L | (TMR1H<<8)) * 2;
  TMR1L=0;
  TMR1H=0;
  do
  {
    if(PORTA.F0 == 1)
    {
      T1CON.F0=1;
      if(PORTA.F1==1)
      {
        T1CON.F0=0;
        break;
      }
    }
  }while(1);

  b = TMR1L | (TMR1H<<8);
  tm = (float)b/a;
  pf = cos(tm*2*3.14);
  x=abs(ceil(pf*100));

  return x;
}

void main()
{
  char c[]="0.00";
  int a,b,d,x,f,e;
  float tm,pf;

  Lcd_Init();
  Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

  ADCON1 = 0x08; // To configure PORTA pins as digital
  TRISA.F0 = 1; // Makes First pin of PORTA as input
  TRISA.F1 = 1; //Makes Second pin of PORTA as input
  TRISD.F0 = 0; //Makes Fist pin of PORTD as output
  TRISD.F1 = 0; //Makes Second pin of PORTD as output

  while(1)
  {
    a = powerFactor();
    Delay_us(50);
    b = powerFactor();
    Delay_us(50);
    d = powerFactor();
    Delay_us(50);
    e = powerFactor();
    Delay_us(50);
    f = powerFactor();

    x = (a+b+d+f+e)/5;
    c[3]=x%10 + 0x30;
    x=x/10;
    c[2]=x%10 + 0x30;
    x=x/10;
    c[0]=x%10 + 0x30;

    Lcd_Out(1,1,"Power Factor");
    Lcd_Out(2,1,c);

    if(x<90)
    {
      PORTD.F0 = 1;
      PORTD.F0 = 1;
      Delay_ms(2000);
    }
    else
    {
      PORTD.F0 = 0;
      PORTD.F0 = 0;
    }
    Delay_ms(250);
  }
}

The function powerFactor() will find the current power factor by using the Timer 1 module of PIC 16F877a. Power Factor may be fluctuating, so to avoid it we will find power factor more than one time and its average is taken.

Note : This is only a prototype of Automatic Power Factor Controller, for practical implementation you may need to make some modifications in the program and circuit.

Share this post

  • Can the owner of this code contact me so that he can explain how to make this code run

  • Hi ..It compiled with out any error for me. But i don’t know how to put a load in proteus to make low pf in supply
    pls help

  • I am planning to simulate this in proteus as i don’t require to do this in real hardware. Is the pf correction section now correct? and how to insert a load to make pf low in proteus?

  • We are getting error for abs and cos tetms in mikroC complier…wt to do??

    Is this the correct code?

  • Hi, Divya shamka I am working in NFL , I want ask you that the above programm for automatic power controller by pic , can be written in assembly language , if yes please write it in assembly.
    Thanks with regards.

  • What is the rating of CT and PT, what inductance should i use on primary and secondary side transformer?

  • This is only a prototype of Automatic Power Factor Controller, for practical implementation you may need to make some modifications in the program and circuit.

  • Ligo please help me, when i running this code in simulation it give
    an error of “stack underflow” please correct this error or send me the
    correct code its not working and how to improve power factor correction using pic 8051

  • Ligo please help me, when i running this code in simulation it give
    an error of “stack underflow” please correct this error or send me the
    correct code its not working.

  • i don’ t know why this program did not compile. can i know what exact version of mikroC it has written for. thank you?

  • Hey man thanks for the code, got it kicking on Proteus 8. Have had an exciting past day since i couldn’t use microc (2k limit). So I translated your code into Hitech C. And just a pointer,,,,, ADCON1 = 0x06 if you want to make all of PORTA pins digital input. Again, thanks for sharing, really helpful you guys.

  • We are not providing the hex file as the project is not complete. Its power factor measuring section is correct, but correction is not correct.

  • iam not able to generate hex file because my compiler is a demo version …so plz send me the hex file or suggest wat to do to generate hex file ….if u tel me to buy original version of micro c pro …its costlier..i can’t afford tat much moneyy so kindly give me some other option
    mail id: [email protected]

  • You need to select proper power factor selection capacitors depending on your load requirements.
    You need to improve power factor correction mechanism of the program.

  • how i made corrections or improve that sections.

    if u dont mine please give me an idea.

  • Hey can u tell the rating of ct for above circuit . I just need a reference as to by what value current should be reduced???

  • The secondary winding of the transformer is connected to LM358. transformer out is AC is this affect LM358.Or it required any rectifer.please tell me.

  • The program is only showing black boxes on the LCD when done on hardware..what to do? I checked all my connections

  • sir whenever i tried to built the programme its showing demo limit in micro c pro …so iam not able to generate hex file because my compiler is a demo version …so plz send me the hex file or suggest wat to do to generate hex file ….if u tel me to buy original version of micro c pro …its costlier..i can’t afford tat much moneyy so kindly give me some other option
    mail id: [email protected]

  • when i Compiled it . error as
    error: 0 434 Demo Limit Demo Limit
    error: 0 102 Finished (with errors): 23 Dec 2014, 01:25:07 Automatic Power Factor Controller.mcppi

    its means generated hex file limit exceed to 2k .
    so pl post it hi-tech or xc8 ver. code

  • Hello,
    The above code is correct, it will work…. only problems is that power factor correction section needs improvement… There is no errors..

    I think, it is not good to simulate projects like this…. Try in real hardware..

  • Ligo please help me, when i running this code in simulation it give an error of “stack underflow” please correct this error or send me the correct code its not working.
    please i have to submit my project at the end of this month.

  • hi sir
    i dont know how to create a hex file
    i am doing the project in protecus???
    is this good on going for that or whether i need to change the software ???
    i dont how miroC works?????? can we build this same circut over there

  • alright thank you so much, please guide me can i use normal step down 12v, 2A transformer in series for current measuring in lm358 ?

  • For voltage transformer you can use a 230/3V, 500mA transformer available in the market…
    For current transformer.. you can wind depending on your requirements.. provide a secondary voltage about 3V.

  • what rating you use of current transformer and voltage transformer please reply. i m waiting for your concideration.

  • If the clock frequencies are correct in mikroc project settings and pic properties in proteus..
    Check the PIC used in mikroc project settings and proteus are same.. PIC 16F877A..

    Note : 16F877 and 16F877A are different microcontrollers..

  • I have only the above code.. It is written for MikroC Pro compiler..
    It is working code.. only thing is it need improvement in the power factor correction section..

  • bro,,,i worked with the pf calculation part only,,,& the code compiles fine,,,,,,,but when i tried it in proteus it gives error & Lcd is blank…

  • hi im doing the project with atmega 16 . i have made the required changes in this code to be compatible with atmega. the code compiles without error but nothing happens on proteus. kindly help me out with this. i have to submit my project very soon

  • what type of current transformer did you used?i mean the name of the transformer or no or something else that i can find and buy it

  • i don’ t know why this program did not compile. can i know what exact version of mikroC it has written for. thank you?
    Please send me the corrected code because when i load his one on proteus nothing happens although it compiles without errors
    my email [email protected]

  • Did Some one practically implemented this? i tried but the display value changes randomly for the same load…

  • May be you could have longer delays.250ms is not enough at all. Since the power factor does not change quickly you could have delay at least 10seconds. Or give the user a configuration option to configure it.

  • There should be some header files. See your code does not include any header files , compiler crazy about where does that Lcd_init(); or Lcd_write() came from?

  • Hi , I am working on a similar project but for a 3 phase system. I would like to know how did you manage the problem of linearity and phase displacement of the CT in particula?

  • Sorry for the late replay..
    Currently we aren’t providing any training.. I am ready to provide training personally… But I am not good in speaking English.

  • tm = (float)b/a;
    pf = cos(tm*2*3.14);
    x=abs(ceil(pf*100));
    can you elaborate this function i.e. what (a) and (b) shows

  • A few of modification of circuit and using ccs c compiler ccp(capture) example, i got a power factor based on comparison between two signal using proteus simulation. Hope you don’t mind that i did some modification on your circuit. I won’t share my email to public, so thank you for giving an idea about measuring power factor. 🙂

  • Would you mind to share or send to email the hex file perhaps? My mikroc pro for pic 6.0 seems giving an hex file size 1kb only after I compile the code. Thank you.

  • I have an Arduino micro-controller & I creating a Power factor meter. So i need some help my brothers..

  • I think.. no modification is needed in the circuit… but you should modify the program…… It is my logic… I have tested it only once… so you should modify the program by conducting some practical tests… and calibrating this project..

  • how to modify this circuit in 3 phase supply.? in 3phase supply compare
    all the phase current and all phase voltage .. how to change this
    circuit..

  • Yes you can replace it with current sensor…….
    voltage transformer is an ordinary transformer….
    It is better to use transformer… as it isolates our low power circuits from high power ac….
    I think the current sensors have internal transformer…

  • Current Transformer and voltage transformer can be replaced with any other electronic circuit its possible ..

  • The above is an academic project made by ECE students… and it is working……… for demonstration…
    but for practical implementation, I think you need to make some more changes…

  • sir which compiler shuold i use for compiling currently i use mikroc pro for pic v6.0.0 but they get an error

  • Hi sir I understand the source code,can you give me the sample current ratio or specification detail of current transformer

  • Sorry, 🙁 it is also a mistake..
    Actually we done this project by connecting current transformer inputs to PORTD, but changed to PORTA while drawing circuit for making the circuit diagram simple..
    Thanks…
    Also the power factor correcting section of this code is not good… We checked this project by adding a switch to capacitor, such that change in power factor can be seen on the LCD by ON and OFF the switch…

  • please share your hex and coff file
    i not understand:

    TRISD.F0 = 0; //Makes Fist pin of PORTD as output
    TRISD.F1 = 0; //Makes Second pin of PORTD as output

    So PORTD is output

    i simulate in proteus and program remain in endless loop:

    do
    {
    if(PORTD.F0 == 1)
    T1CON.F0 = 1;
    else if(PORTD.F0 == 0 && T1CON.F0 == 1)
    {
    T1CON.F0 = 0;
    break;
    }
    }while(1);

    how to trigger the timer1 if the inputs are PORTA0 and 1 ???

  • first error: missing line “sbit LCD_EN at RB5_bit;”
    compiler (mikroC PRO for PIC 6.0.1) says:

    59 404 ‘c’ Identifier redefined automatic-power-factor-controller-using-microcontroller.c
    77 318 Assigning to non-lvalue ” automatic-power-factor-controller-using-microcontroller.c
    81 324 Undeclared identifier ‘e’ in expression automatic-power-factor-controller-using-microcontroller.c
    83 324 Undeclared identifier ‘e’ in expression automatic-power-factor-controller-using-microcontroller.c
    83 317 Operator ” is not applicable to these operands ” automatic-power-factor-controller-using-microcontroller.c
    83 317 Operator ” is not applicable to these operands ” automatic-power-factor-controller-using-microcontroller.c
    60 1163 Variable ‘tm’ has been declared, but not used automatic-power-factor-controller-using-microcontroller.c
    60 1163 Variable ‘pf’ has been declared, but not used automatic-power-factor-controller-using-microcontroller.c
    0 102 Finished (with errors): 17 mai. 2013, 12:50:37 automatic-power-factor-controller-using-microcontroller.mcppi

  • The code is correct.& we made this project with above code.. ,for the practical implementation you need to modify the power factor correcting section of the code…. as this is only a prototype…

  • i don’ t know why this program did not compile. can i know what exact version of mikroC it has written for. thank you?


  • >