Tuesday, November 23, 2010

Smart Voltage Stabilizer Using PIC16F877A

Voltage stabilizers are used for many appliances in homes, offices and industries. The mains supply suffers from large voltage drops due to losses on the distribution lines en route. A voltage stabilizer maintains the voltage to the appliance at the nominal value of around 220 volts even if the input mains fluctuates over a wide range.

Here is the circuit of an automatic voltage stabilizer that can be adapted to any power rating. Its intelligence lies in the program on PIC16F877A—a low-cost microcontroller that is readily available. The circuit, when used with any appliance, will maintain the voltage at around 220V even if the input mains voltage varies between 180V and 250V.

Here the circuit is shown for a 5A stabilizer. It acts within 100ms to produce a smoothly varying output whenever input mains voltage changes. (Servo stabilizers move a variable contact on a toroidal auto transformer to adjust the output when input goes up and down, which takes seconds.)

The PIC16F877A is an RISC (reduced instruction set computer) microcontroller with 35 instructions, and hence program development with it is rather tough. But, there are good support programs.

Circuit description:
The circuit is divided into two sections as it is easy to test them separately: voltage stabilizer controller and voltage stabilizer buck-boost. The sections can be joined easily.

Voltage stabilizer controller section:
This part of the circuit is built around the PIC microcontroller (see Fig. 1). The 5V supply for the microcontroller is derived from a small iron-core mains step-down transformer having 9-0-9V, 300mA rating, two diodes (1N4007) and a 1000μF capacitor followed by the 7805 regulator.

 The ADC input channel 0 at port-A pin 2 of IC2 is used as shown in Fig. 1. Here potentiometer VR1 is connected to +5V and ground through a jumper connection. For the purpose of testing, you can vary VR1 to adjust the voltage from 0 to 5V. The reset circuitry at pin 1 (MCLR) has capacitor C1 and resistor R1. Pin 30 (port-D bit 7) gets a signal (marked as ‘D’) derived from the mains supply. Pins 17 and 16 (CCP1 and CCP2) provide the actual pulse output signal that helps in stabilizing the mains power. The signal is a set of equally spaced pulses at about 8 kHz for a 12MHz crystal.

The pulses from pins 16 and 17 are buffered using a pair of inverter gates of high current
driver IC ULN2003. Note that the gates in this chip need pull-up resistors Fig. 1: Circuit of voltage stabilizer controller section at the output pins. So at points marked ‘A’ and ‘B’ we get two pulse trains from the microcontroller. Synchronization with the mains supply is achieved by the square wave (50Hz mains derived) on port-D bit 7 (pin 30).

Transistor T3 (BC547) produces a rectangular pulse from the half-wave rectified low voltage (9V) from the transformer (9V-0-9V, 300mA). Using 50 Hz as reference for positive and negative half cycles of the mains supply, it produces the pulses at A and B points in turn. These pulses change in width and are hence called pulse width modulated. The width varies in accordance with the voltage to be produced for compensating the voltage from mains supply.

After wiring the circuit, program the chip with the given Assembly program. Insert the chip into the board and apply power supply. The chip has two PWM pins, 16 and 17. Adjust the shaft of pot-meter VR1 (10-kilo-ohm) to the bottom position for zero voltage. Also, ground pin D. The PWM pulse is now available from pin 17 of IC2, while pin 16 is low. If the shaft of the potmeter is moved to the top position when ‘D’ is connected to ground, pulses will be available from pin 16. Taking pin D to 5V reverses the above sequence. After checking this part of the circuit, the circuit shown in Fig. 2 may be tested.

In manual position of input selection switch S1 (Fig. 1), the analogue input voltage from pot-meter VR1 is used. In this position, the circuit functions as a variac that varies the output voltage from 180V to 250V as the pot-meter is varied.

In auto position of S1, the circuit acts as a stabilizer. For this, transformer rectified supply derived from the mains provides a proportional voltage to the ADC of the chip.

Point E in the voltage stabilizer controller circuit gives a voltage that varies with mains voltage. At exactly 220V mains, the 9V transformer (X1) gives a peak voltage of 9√2 =12.7V and subtracting 10V using zener diode ZD1 gives 2.7V at point E. It increases to 5V when the mains voltage rises to 259V and drops to zero when mains drops to 172V in effect, giving 0 to 5V over this range.

Using this voltage at point E, you can assess variation in the mains voltage and thereby control the PWM based sine voltage for adding (boost) or subtracting (buck) from mains. Point E is connected to the ADC input pin (point C) of the PIC in auto position (Fig. 1).

The buck-boost principle:
Voltage stabilizers buck (subtract) the mains voltage if it is higher than 220V and boost (add to) the mains voltage if it is lower than 220V. For this purpose, you need to produce a small voltage to do addition or subtraction. In Fig. 3, the mains voltage waveform is shown in the top left corner, with two voltages of smaller amplitude (about 30V) shown below it. One of these two voltages is in the same phase as the mains voltage, while the other is out of phase. By adding any of the two voltages, you can boost or buck the mains voltage.

For this purpose, ordinary voltage stabilizers generate a small voltage using a transformer with one or more taps. They connect the small voltage in series with the mains supply so as to add or subtract from it. A changeover relay is used to switch to buck/boost, while another relay selects between voltages from the two taps.

This method does not produce a smooth voltage change due to relay switching and the voltage from tap produces a fixed value (instead of a finely-variable voltage). In this project, the additional voltage of about 30V in phase or out of phase with the mains voltage is finely variable because of PWM. So it produces a smoothly varying output.

A typical PWM concept is shown in Fig. 4. The microcontroller produces pulse-widths, as required, for generating the voltage to be added or subtracted from the mains. The pulses from points A and B (refer Fig. 1) are fed to the transformer shown in Fig. 2. The secondary winding of this transformer gives the adding voltage. In this case, there is no relay switching; the buck or boost is done smoothly by changing the phase of the adding signal instantly. So it is a continuous voltage stabilizer. Depending on how much the input varies from 220V, pulse width is generated so as to adjust the output voltage by adding or subtracting from it. This is a feed-forward control.

Points marked ‘com’ common points in Figs 1 and 2 are not the ground and should not be connected to the neutral line.

Take care while checking the buck/boost circuit, as all the points are ‘hot’ and will give electric shock if touched, and also when interconnecting the voltage stabilizer control and buck-boost circuits.

Pulse-drive circuit and the transformer:
Fig. 2 shows the circuit to buck/boost the mains voltage using a buck-and-boost transformer. The iron core transformer used here is the same as used in voltage stabilizers. There is no tap on the secondary winding and the primary winding is center-tapped. 

As with most transformers, the stampings used for this transformer are made of 4mm thick silicon steel. These are E-I type Stalloy/CRGO stampings. The size of the stampings depends on the rating. A toroidal winding transformer gives better performance and is smaller in size.

Here, we have used a 250V-0-250V, 500mA primary to 50V, 5A secondary transformer. The windings’ number of turns depends on the core size used.

Pulses from A and B of the voltage stabilizer controller circuit are fed to the gate pins of MOSFET power transistors (IRF840) via 10k series resistors. There are also 100-kilo-ohm grounding resistors connected to the transistors’ gates. The drains (D) are connected to the winding ends of transformer X2. The center tap of the primary winding is connected to the rectified DC supply from the mains. (This rectified voltage is not filtered; it is just unfiltered, rectified sine wave at point P.)

The power transistors (IRF840) switch the rectified sine voltage supply at the PWM frequency produced by the microcontroller. To smooth out the pulse switching, a 2.5μF, 400V AC fan capacitor is connected across the primary winding of transformer X2. The voltage induced in the secondary winding is a sine wave whose amplitude depends on the width of pulses at points A and B. The program changes the PWM width, and thus the amplitude of the sine wave, to adjust the mains voltage to 220V level.

We thus get a sine wave of the mains frequency. By serially adding the secondary voltage to the input mains voltage we get the stabilized voltage at the output of the unit.

On alternate half cycles, pulses at points A and B arrive to make either of the two transistors T1 and T2 conduct and allow the current to flow through the primary winding. A 2.5μF, 400V AC capacitor is required across the primary winding of transformer X2. Otherwise, only the pulses from A will pass through and buck and boost cannot be obtained.

In manual position of the switch, when potmeter VR1 is varied from bottom to top (0 to 5V),  the voltage across the secondary decreases, crosses zero and then increases again. This means the secondary voltage varies with the potmeter position. Check the variation in secondary voltage by using a voltmeter, or a multimeter set to 50V AC range. The voltage should increase on either side of the mid-point of VR1. In auto position, combining the secondary output voltage of transformer X2 with the mains voltage gives you the stabilized output.

  • First, test the controller circuit (Fig. 1) for pulse width modulated signals at points A and B. Check changeover from A to B by applying 0V and 5V at point D.

  • Check the circuit for a square wave of 5V amplitude at point D during positive half cycles of AC mains. This square wave is generated by the transistor fed with the unfiltered low voltage DC from transformer X1.
  • Vary the pot-meter in manual position of the switch. Using a CRO, you can see variation in the pulse-width (see Fig. 4).
  • As VR1 is adjusted beyond the mid position, pulses at points A and B toggle.
Note that the transistors are ‘hot’ and ‘live.’ Energize the voltage stabilizer controller circuit first but only in manual position of switch S1. Join points C and E and then switch on mains power for the buck and boost circuit.

Measure the AC voltage across the secondary output of transformer X2. Vary VR1 in the
controller section and check whether the voltage output at the secondary of X2 varies.
A CRO can be used to observe this secondary voltage. It should be 50Hz sine wave, but if
it has a break, it means the half cycles are not synchronized.

In Fig. 1, at point E, we have included a lag circuit comprising variable resistor VR2 (5 Kilo-ohm) and capacitor C5 (10μF). Adjust preset VR2 until the waveform is a smooth sine wave.

There may be small ripples in the first half of each cycle, but these do not matter and will
anyway be present due to PWM switching. Capacitor C7 (2.5μF) across the primary winding of transformer X2 filters them out.

The secondary voltage of transformer X2 should decrease and then increase as VR1 is raised from 0V position. Then check voltage regulation after changing over to auto position in Fig. 1. Adjust VR1 to the center position precisely. In the center position, there will be no pulse and therefore no adding voltage in the secondary winding. So the value of the zener diode used in the rectifier circuit should be changed in order to get 0V for 220V input. A variac is useful for varying the input voltage and checking the output.
  • Activate the buck-and-boost circuit by closing stabilizer switch S2 (Fig. 2).
Using a variac, the voltage can be varied and the stabilizer output observed on a voltmeter. If the voltage boosts up instead of bucking, reverse the secondary winding terminals connected in series to the mains.

Capacitor C6 (0.1μF) at the output terminals of X2 removes minor ripples, if any, in the waveform.

The optional input voltage display circuit consists of three common-anode, seven-segment LEDs (each LTS542) shown in Fig. 5. The seven-segment LED displays are driven from port-B of the chip in a multiplexed manner. The anode selections are made through bits 0 through 2 from port-D via transistors T4 through T6, respectively. 

DownloadCode files

Fig 1 PIC16F877 based Stabilizer

Fig. 2: Circuit of voltage stabiliser buck-boost section

Component Required:
IC1,                                              7805 voltage regulator
IC2,                                              PIC16F877A microcontroller
IC3,                                              ULN2003 buffer
D1-D8,                                         1N4007 diode
T1,T2,                                          IRF840 MOSFET
T3,                                                BC547 npn bipolar transistor
ZD1,                                             10V zener diode
R1, R3-R8, R10, R12, R13,       10 K ohm
R2,                                                4.7-kilo-ohm
R9, R11,                                       100-kilo-ohm
VR1                                              10-kilo-ohm potmeter
VR2,                                              5-kilo-ohm preset
C1,                                                 100nF ceramic
C2,                                                 1000μF, 25V electrolytic
C3, C4,                                          22pF ceramic
C5,                                                 10μF, 25V electrolytic
C6,                                                 0.1μF, 400V AC
C7,                                                 2.5μF, 400V AC
X1, - 230V AC primary to 9V-0-9V, 300mA secondary transformer
X2, - 250V-0-250V, 500mA primary to 50V, 5A secondary transformer
Xtal, - 4MHz crystal oscillator
S1, - SPDT changeover switch
S2, - On/off power switch (5A)


  1. Please software for smart stabilizar useing pic16f877a

  2. Code files are located at download label (above schematic). Download from there.

  3. we have some problem
    1. where can i connect servo motor connection
    2. manual mode function not drw inthe cantrol card.

    provide over or under voltage protection, amp. current protection or timer in smart stabilizer card

  4. Servo motor in stabilizer? Servo stabilizer is one kind of stabilizers.
    For manual mode use switch 1, in fig. 1.

    Sugesations are welcome.

  5. sir,
    please suggest were can i connect servo motor connection gor more information regard servo motor pls visit


  6. are the codes working correctly
    pls help me

  7. this cantrol is suitable for ac servo motor & where connect the servo motor connection to control the output of stabilizer. how can i show the input/output voltage in the display section

  8. Load should be connected at X2 (fig. 2). Output of stabilizer is only depend on the variation in ac input voltage. To display input voltage refer to the fig.5.

  9. Sorry for late response. This stabilizer is not the servo stabilizer that uses servo motors. This is an intelligent stabilizer that uses PWM technique to adjust the output without any mechanical movement as used in servo stabilizer.

  10. u have any micro controller circuit for servo stabilizer
    for any more clarification for servo stabilize r please visit www.servostabilizer.in

  11. Nop; i don't have any micro-controller based servo stabilizer.

  12. Leading digital voltage stabilizers manufacturers of India deals in AC wall mounted voltage stabilizer, refrigerator servo stabilizers, digital automatic stabilizers, servo voltage controller sabilizers and more.

  13. Kaku Enterprises is one of the leading high quality stabilizer manufacturers, suppliers and exporters in Delhi, India. We are exporter of AC Wall Mountable,Air Conditioner Stabilizers and Digital Refrigerators Stabilizers.

  14. can i used this circuit for a 15 Amp circuit. if possible what changes i have to make in this

  15. No. For 15 amp you have to use high power transformer, X2, and transistors, T1 and T2, that can easily handle this much current.

  16. so i can use this same circuit for that. so the only thing i want to change is the transformer. the thing is that im planing to use this at the output of a generator of 35oo watts(230 v). so can i use this circuit for that

  17. Yse; but this circuit is not tested for this much power, use at your own risk.

  18. hello! i work on a project with a microcontroller 16F877A and i would like to have your help, getting your sources code of this program.. but when i try to enter in the link you post, nothing happened.. is it normal?

  19. Try it again. The code is uploaded on 4shared.

  20. Leading digital voltage stabilizers manufacturers of India deals in AC wall mounted voltage stabilizer, refrigerator servo stabilizers, digital automatic stabilizers, servo voltage controller sabilizers and more.

  21. i tried this circuit ....bt its not working...when i m trying to vary the input using variac, my output is also varying with input and i/p value = o/p value in my case what to do please help me....please

  22. Please read the above mentioned testing procedure. It says:

    "If the voltage boosts up instead of bucking, reverse the secondary winding terminals connected in series to the mains."

  23. What i have done is i slightly change the capacitor with different values ....this will effect the circuit...and i have to use the same capacitors of the given values..???

  24. If your o/p is ok then it doesn't matter. if not then use cap of same value and do testing "according to the mention procedure in this article"

  25. Dear Malik
    What changes will occur in the circuit if I want to use this circuit for mains input range 90V to 280V.

    D Raj

    1. i want to use this circuit for mains 90-280v input another can iuse other non programming ic problem its programming reply thanks

    2. set the voltage at 'E' to 0 for 90v and 5v for 280v. for this you may have to change the zener diode.

  26. Dear Sir,
    Shall i get micro controller for servo stabilisers ac motor design contact Maria, nagasdolphin@gmail.com

  27. Dear Zohaib,
    where you from? My request is Plz plz send me your email addres at this

    I'll be awaiting your reply Thanks......

  28. how can i use the code...in which software???? plz help me...

  29. use the .hex file and burn it into the pic. check the "pic programmer" in labels for info.

  30. plz post c programing for automatic voltage regulator

  31. how to burn this code to pic

  32. Do you have a circuit for using this design with an isolating transformer?. I need to isolate harmonics and noise.

  33. Please help, What will be the ratings of the trasformers, in case I, need only 1Amps at 230Volts.

    1. as total power is Pout + Pin. Primary VA is 500V*500mA=250
      and for 1 amps secondary VA=230*1=230. So total =470VA.

    2. sorry correction in above: for 1 amp secondary VA=50*1=50VA. So total VA=250+50=300VA.

  34. Hello sir.
    I try to program hex file with pickitII i have 2 problems sir.
    1.code hex file with no option display when i import to pickitII it warning No configuration words in hex file in mplab (ver 7.5) i need to know config word for
    16f877a and try to build new code hex file to pickitII
    2.code hex file with option display it ok for pickitII but when i testing with no 220Vac input. no display show at 7 led but when i move power off it show a few second how to test display work sir.

    1. bits are: Debug disable, LVP disable, WDT disable, and XT oscillator.

  35. A good working
    Have pic basic code
    How did you choose as well as the frequency of the PWM

  36. Can you give access to e-mail address

  37. friends! Do you have this circuit running
    If you are running,
    can contact me

    Thank you for your interest

  38. Sir wat is the exact output of the above circuit? I am confused totally as in fig 2 , steady output is written and secondary voltage is of 50V,5A? Then how to use it for the 230 V,1A??
    Kindly reply.

    1. this is 5amps stabilizer, 50v is in series with the i/p voltage.this ll add up or subtract according to the i/p voltage level.

  39. Dear Zoahaib,

    I have few questions
    1. Is it safe to run Air-Conditioners on PWM sine wave?
    2. For 4KVA application (Air-Conditioners) what would be the primary and secondary current?


  40. Sir To use this circuit for 230V/1A current output, what modification i need to do in this circuit? I need to make the stabillizer with same design but not for 5amp current kindly reply?

    1. just use lower va transformer; 250-0-250v 100ma and 50v 1amps.

  41. Sir i am having query is that, why to use irf 830 power transistor? Is it for isolation? Another query is that the use of AC fan capacitor??

    Thanking you.

    1. we are using pwms to control the xformer voltage; for this we are using mosfets.its only to operate the xformer for desired o/p voltage.
      ac capacitor is used to remove the glitches occurring during switching of mosfet.

  42. Hi

    good project,
    how to modified for 120v?
    circuit and code?

  43. can i simulate this circuit in proteus 7, and can you post c code for this circuit and please tell me what's compiler you used for this asm file.

  44. Hello Zohaib,

    i am having one confusion with the position of switch S1 as now it seems the switch is in effect regardless of manual and auto position, and even in auto position, i am able to manipulate the voltage and the zener voltage will further reduce before entering the microcontroller. but if the switch is placed in the 5V supply line, then only it can work separately in manual mode. Kindly advise me if i am wrong.

  45. yes sir,
    can i simulate this using proteus.how can i use the source code in mplab.kindly reply.Tq

  46. Dear Friend i want to use a as ac stablizer input 80vac output 220v ac so which transformer size and input winding and output winding i use.waiting for ur reply

  47. thankyou!!!!!!!!

  48. can i know what software you use for simulation? please..answer me.

  49. dear all,
    few questions;

    1.do you have the c programming code
    2.what do you mean by 'com'
    3.friends! Do you have this circuit running
    If you are running,can contact me

    Thank you for your interest

  50. sir,my controller circuit is running perfectly...but the following problems are arising in the buck boost section:-

    1.the transformer is producing sound when connected to the circuit board.it is heating up very fast.
    2.in auto mode the 7 segment display doesn't show any variation in voltage.

    sir i want to know the purpose of mosfets and full wave bridge rectifier here and why the centre tap of buck boost transformer is connected to one o/p of full wave diode rectifier.and lastly what voltage and current is the primary of the buck boost transformer receiving...pls explain the buck boost segment in a more detailed and easy language..
    thanking in anticipation

  51. Sir some question for this circuit.1-drive pulses is change as voltage increase&decrease but as s2 as on mosfet blown. 2- according your circuit diagram lf s2 kept on main supply directly go to output as you show stabilized output .how work the circuit

  52. Excellent project

    Thank you

  53. anyone can help me on issue that my control circuit is working fine but mosfet IRF840 blown up when transformer is connected it.

  54. How can I do it work on 120 v 60 hz?

  55. Fig4 says 12 Mhz is the actual frequency, component list and the circuit says 4Mhz crystel. config word says XT (<=4Mhz) pls carify. thank you.

  56. any one can help,at which frequency X2 transformer design?

  57. Sir some question for this circuit.1-drive pulses is change as voltage increase&decrease but as s2 as on mosfet blown. 2- according your circuit diagram lf s2 kept on main supply directly go to output as you show stabilized output .how work the circuit

  58. Can anyone tell me that whether the codes given are correct and which software to use to run the codes?

  59. Where are the calculations for Voltage regulation?
    Or any other calculation regarding voltage balance

  60. The Automatic Voltage Stabilizer Manufacturers are known to be the best options for conferring protection to the electrnonic devices. Thus these devices are considered to be vital for enhancing the endurance of the devices. Servo Stabilizer Manufacturers, Isolation Transformer Manufacturers


  62. IFR840 blown, I can see others experiencing the same issue, was there an answer to this issue. Works upto 60V ac via variac on the X2 side of the circuit, then the IFR840 gets destroyed when you go beyond this.

  63. Voltage stabilizers are used for many appliances in homes, offices and industries. The mains supply suffers from large voltage drops due to losses on the distribution lines en route.voltage stabilizer manufacturers in delhi

  64. anyone can help me on issue that my control circuit is working fine but mosfet IRF840 blown up when transformer is connected it.

  65. This comment has been removed by the author.

    1. Suspect, the Transformer is getting saturated. Have given up on this project. In simple terms idea is brilliant but does not work in practice,. Absolute tragedy..

  66. Dear,
    what changes I have to make to change Trx 9-09 from 50-0-50