You are on page 1of 21

PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

1

AUTOMATIC VOLTAGE STABILIZER (AC AC)
USING THE PIC16F873A

SYED TAHMID MAHBUB
www.tahmidmc.blogspot.com


PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

2

INTRODUCTION
My first semester at Cornell University ended late December (2013). I went back home to Dhaka,
Bangladesh for my winter break. During this period of time (late December to mid January), there was a
lot of political turmoil in the country due to which I could not leave the house a lot to spend time with
friends and family. So I ended up spending a lot of time in the house with electronics specifically on
two things: making some small projects with the PIC32MX250F128B (Microchip PIC32 series), and,
making an automatic voltage stabilizer circuit.

Ill talk about the automatic voltage stabilizer here. First Ill give a short introduction as to the motivation
behind me working on it before I go on to talk about the operating mechanism of the voltage stabilizer
and then the circuit diagram and source file.

At the end of the article, youll find the links to download all the files. Also do check out the Youtube
videos where I demonstrate the voltage stabilizer and its operating mechanism.

MOTIVATION
My dad knows a man named Kamruzzaman who worked under my dad (in electronics) for a very short
amount of time, doing stuff like soldering boards, etc. A few days after I went back to Dhaka,
Kamruzzaman called my dad and mentioned that he wanted to talk to my dad about something. We
invited him home, where he showed us a nice Chinese-made automatic voltage stabilizer circuit he was
trying to replicate albeit unsuccessfully. At the same time, he mentioned about his financial hardship
and asked for our help with designing the automatic voltage stabilizer so that he could have some good
financial support from this product.

In Bangladesh, the automatic voltage stabilizer (AC-AC) is a ubiquitous little piece of hardware that is
used to somewhat compensate for the varying line voltages (which while being advertised as 220V, can
on a given day vary between 170V and 240V in Dhaka and can vary over a larger range in other parts of
the country, due to the unreliable electrical grid).

This was a good learning opportunity, a great opportunity to gain some experience and most
importantly, a great way to help someone in need through doing something I truly love.

PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

3

And I got to it. I spent about somewhere between a day and a half, and two days thinking about how
best to go about designing this voltage stabilizer circuit, while maximizing performance and minimizing
build hassle. Then I built the test prototype on verroboard and tested it out. Kamruzzaman and I tested
the entire product through a long eight-hour testing process where I kept on refining and improving the
circuit until I achieved what I wanted a blend of the right amount of performance and a minimal
amount of build effort/hassle. After that, I designed the PCB for the board; it was a long night designing
the PCB fuelled by coffee (=P), I started at around 12 AM and finished at around 9.30AM after which
Kamruzzaman got the PCB made (that very day) and we performed the final testing of the product that
night. The circuit worked as expected and the project was complete.

SPECIFICATIONS
Now, lets go on to talk about the technical part of the project. For this automatic voltage stabilizer, the
parameters were decided initially:
Output voltage must lie between 200V and 240V for all input voltages above 150V and upto
260V.
Input voltage range must be 150V to 260V, preferably wider.
Output frequency and waveform should be unchanged from the input frequency and waveform.
The voltage stabilizer must be inexpensive.
There should be no variable resistors in the final finished product. This was something
recommended by Kamruzzaman, as he said that sometimes, some of the variable resistors he
uses tend to drift in resistance slightly and this causes the circuit to become less reliable over
time. Although this seemed quite challenging (due to resistor tolerances in the voltage sense
section, tolerances in the diode forward voltages in the AC-DC rectification section, etc), I quite
liked the idea.
Based on the above initial design decisions, the final parameters/specifications are as follows:
Input voltage: 125V/135V (Ill explain this later) to 270V
Output voltage: >=200V and <= 240V for all input lying between 140V and 270V
Input and output frequency are the same
High cut feature at 270V
Low cut feature at 125V/135V
Input voltage is displayed (to the nearest voltage, 1V) on a 3-digit seven segment display
There are no variable resistors in the final finished product. However, this does not mean that
there is no variable resistor at all. A variable resistor is used to initially calibrate the circuit
before it can be removed from the circuit more on this later.
4 relays are used
The auto-transformer has a 0V/neutral connection and 4 additional tappings 165V, 190V, 215V
and 240V (notice that the tapping voltage ratings are in 25V increments)
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

4


The automatic voltage stabilizer is controlled by an inexpensive PIC 16F873A microcontroller. The
voltage conversion and control are done by using one autotransformer along with four relays (all control
signals are obviously generated by the microcontroller). The microcontroller senses the input voltage
and turns the relays on/off as required to provide an output voltage between 200V and 240V at all input
voltages between 140V and 270V. These relays work in conjunction with the auto-transformer to step
up or down the input voltage to provide the required output. Two of the four relays are used to switch
the input voltage connection between the 165V, 190V and 240V tappings, while a third relay is used to
switch the output voltage connection between the 215V and 240V tappings. The fourth relay is a
master on/off control relay this relay is always on when the automatic voltage stabilizer is operating
normally (this ensures that there is an output), but is turned off in the low-cut and high-cut modes to
disconnect the output.

INPUT VOLTAGE SENSING SECTION
The input AC voltage is first rectified to DC using a bridge rectifier. This is then filtered with a relatively
large high voltage capacitor to reduce/minimize the DC voltage ripple to obtain a constant smooth DC
voltage. This high voltage DC is then stepped down to a low-voltage DC level (that is within bounds
acceptable by the microcontroller). This is done using a simple resistive voltage divider circuit.

Initially, while I was testing the voltage stabilizer, I noticed that the input voltage sensing section was not
working satisfactorily. While the output low voltage DC was directly (linearly) proportional to the input
AC voltage for most input voltages, this (linear) proportionality was being lost at higher voltages. I
calculated that the power dissipation across the upper resistor (initially selected as 100k) was about
1.5W at high input voltages and had thus used a 2W resistor. However, the resistor heated up
excessively at the high voltages. This caused its resistance to drift and thus the sensing circuit was thus
not working properly. Later, the single 2W resistor was replaced with multiple lower power resistors in
series to decrease the power dissipation per resistor and thus the heat dissipation per resistor, in order
to ensure that the resistors did not heat up ensuring that the resistors had a constant resistance while
operating. This worked nicely. I further modified the voltage divider so that the resistances were no
longer 100k:1k (as initially selected) but (47k*6):3.3k. While the resistance ratio of both circuits is
approximately the same, the latter configuration further reduces the power dissipation, promising
better performance.

At the output of the voltage divider, two diodes were used to form a clamp circuit. In the event of
overvoltage presence at the voltage divider output, one of the diodes would become forward biased
and thus clamp the voltage to VDD + one diode forward drop. This would be about 5.7V for our circuit.
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

5

In case of undervoltage (too low negative voltages) presence at the voltage divider output, the other
diode would become forward biased and thus clamp the voltage to VSS one diode forward drop. This
would be about -0.7V for our circuit. While +5.7V and -0.7V inputs to the ADC are not ideal, these are
definitely better than the presence of high positive or negative voltages at the ADC input (which would
immediately destroy that portion of the microcontroller). Regular rectifier diodes were used in the
circuit, which is why I assumed the forward voltage drop to be +0.7V. To improve the clamping, schottky
diodes could be used instead of regular rectifier diodes. At the very small current level present, it is
reasonable to expect a diode forward voltage of +0.3V or perhaps even lower, depending on the diodes
being used.

While this is all good and well, there are two things here that could potentially disrupt proper operation
of the circuit: the input filter capacitance and the input impedance for the PIC ADC (the voltage divider
circuit).

If too large an input filter capacitance is selected, it will discharge slower and give poorer response to
quick voltage drops. Thus, a value of the capacitance should be used such that the voltage ripple is low
but the response to quick voltage drops does not suffer too much. Capacitances of 10F, 22F and 33F
were tested and all gave good results. 22F seems to be the match here providing a good compromise
between response to quick input voltage drops and DC voltage ripple.

To ensure that the ADC measured the low-voltage DC level properly, a capacitor was placed at the
output of the voltage divider section such that this would act as a parallel capacitance to the internal
one (of the ADC). Furthermore, the ADC sampling time was chosen to not be too quick so that more
accurate results can be obtained. The default settings of the mikroC PRO for PIC ADC library support this
requirement.

CALIBRATION
There is a switch in the circuit for calibration. When this switch is shorted and the microcontroller is
reset, upon startup the microcontroller enters calibration mode. I have mentioned above that there is
no variable resistor in the final circuit but that one would be needed for calibration. The reason a
variable resistor would be needed in the first place, is that the output of the voltage divider will not
always be the same ie, from circuit to circuit, due to variations in component values and parameters,
the output voltage will be the same. The main reasons for this are the tolerances in the resistances, the
inconsistencies in diode forward drop voltages and the discrepancies from part to part. To compensate
for this, traditionally, a variable resistor is used as part of the voltage divider. The resistance is altered to
compensate for the different errors and discrepancies and thus provide the expected output.
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

6


Now, sometimes, the value of the variable resistance may not remain constant even when the wiper
position is unchanged. Thus, in this circuit, where reliable and consistent output over long periods of
time is a necessity, it was decided that a variable resistance will not be used in the final product at
least not one on which the circuit depends while running.

So, in this circuit, I have provided the calibration mode. Upon entering the calibration mode, the
microcontroller displays what it thinks is the input voltage. The real input voltage is measured with a
voltmeter. Then, the variable resistance is changed and accordingly, the microcontroller displays a
changed voltage. In the software, I have done some floating point mathematics where the ADC result is
converted to an AC voltage level. In this calculation, there is a constant with which the entire expression
is multiplied. Upon changing the resistance of the variable resistor in calibration mode, the value of
the constant is changed as well, and this is reflected in the voltage displayed on the three digit seven
segment display. When the calibration switch is opened, the microcontroller exits calibration mode
and proceeds to save the value of this constant in its internal memory in the EEPROM. Since a floating
point value cannot be saved in the EEPROM, the floating point number is multiplied by 10000 to obtain a
value that is smaller than 2
16
, meaning that this value can then be saved in two memory locations the
high byte in one and the low byte in the other. Once the microcontroller exits calibration mode it
cannot reenter calibration mode unless it is reset, upon which calibration may again be performed.

Every time the microcontroller starts up, it checks to see if it has been calibrated. This is understood
from the value written to a specific EEPROM location this value is written when the calibration
constant is saved onto the EEPROM. Thus, if calibration has already been done and the calibration
switch is not pressed, the microcontroller retrieves two bytes of data from two EEPROM locations and
puts them into one 16-bit value. Now, when this value is divided by 10000, the corresponding floating
point value is the original required calibration constant. This is used by the microcontroller in all further
voltage calculations and interpretations.

When the microcontroller starts up for the first time, it waits for the calibration switch to be pressed.
Once the calibration switch is pressed and calibration is done, the calibration switch is opened and the
microcontroller saves the calibration constant in the EEPROM and proceeds to carry out its required
operations.

Once proper calibration has been completed, the variable resistor and the calibrate switch may be
removed from the circuit, if desired. This will not affect the performance of the circuit, unless of course
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

7

the user wants to recalibrate at any time. This is what I initially meant when I mentioned that the final
product has no variable resistor in it.
RELAY AND TRANSFORMER CONFIGURATION, AND SWITCHING


The input switches between the 165V, 190V and 240V transformer tappings while the output
switches between 240V and 215V tappings.

The transformer is a simple autotransformer with the turns ratio 165V: 190V: 215V: 240V along
with an auxiliary winding for powering the circuitry.

PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

8

REST OF CIRCUIT
The microcontroller runs off of a 4MHz external crystal oscillator. An external crystal oscillator has been
used since the PIC 16F873A lacks an internal oscillator, which would have been sufficient since there is
no precise time-critical aspect to the automatic voltage stabilizer.

The microcontroller is powered off of a regulated 5V DC supply. The autotransformer has a 12.5V
auxiliary winding. The voltage at this winding will remain around 12.5V and not vary too much with the
input voltage variation due to the switching of the relays and the output voltage regulation which acts to
regulate the voltage across this winding too. This low voltage AC is rectified to DC using a bridge rectifier
and then filtered with a bulk capacitance. You will also find that decoupling/bypass capacitors have also
been used. This filtered DC is fed to the input of a 7805 linear voltage regulator. Since the current draw
is not too high, a linear regulator such as the dirt-cheap ubiquitous 7805 is sufficient and no fancy
switching regulator is required (I still do recommend switching regulators, cost permitting, especially
with large current outputs and/or large input-output voltage differences). It is critical to use at least one
decoupling capacitor (which should be placed as close to the microcontroller as possible) and you can
see that it has been used.

The regulated filtered DC voltage that is fed to the 7805 input is also used to power the relays. However,
this voltage is not directly provided as the voltage is a tee-bit higher than what the 12V rated relays
would probably like. Thus, the voltage is dropped by approximately 2.8V by passing this input voltage
through four regular rectifier diodes in series.

Each relay switching is controlled by the microcontroller. However, since the microcontroller cannot
provide sufficient current to drive the relay coils, transistors are used to amplify the current and drive
the relays from the required signals provided by the microcontrollers. The configuration is the simple
common emitter mode. Each relay coil also has in parallel with it an anti-parallel diode that is used to
catch or rather bypass the inductive kickback that occurs whenever the current flow through the
relay coil is stopped, ie when the driving transistor is turned off.

Now lets move on to the seven segment display. As you may have already guessed (and it should be
quite apparent, given that Im using a 3-digit segment), the decimal points in the display are not used.
Thus that leaves us with seven LED segments (conventionally referenced as segments A through G) that
needed to be driven. Additionally, to minimize the number of pins required to drive the seven segment
display, the three digits are turned on one after the other. However, this is done so quickly that to our
eyes, it seems that the three digits are always turned on. I have chosen to use a 167Hz refresh rate
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

9

meaning that the entire display is refreshed 167 times a second once every 6 milliseconds. Each digit is
turned on, kept on for 2 milliseconds and then turned off before the next display is turned on and so on.

Since the microcontroller output drive current is limited and we want optimum brightness (and thus
drive current) of the seven segment display, seven transistors were used in the common collector (also
known as emitter follower) mode to drive the seven LED segments in the display. Additionally, three
transistors were used to provide or disconnect the supply to the individual digits, as required for
continuous subsequent switching between the digits.

Upon start-up, the microcontroller enters delay mode. This is when, for a specified amount of time
(that is pre-programmed), there is no output. There is a switch that is used to select between short
delay (default mode, when switch is open) and long delay (when switch is closed/pressed). These delay
times are pre-programmed, and I have chosen to use 2 seconds for the short delay and 3 minutes for the
long delay. These, as far as I know, are the standard times present in the voltage stabilizers available in
the market. The delays are set by simple software loops that do nothing such delay functions are
provided in the mikroC PRO for PIC library.

There are three LEDs in the circuit that are used to provide visual feedback, besides that already
provided by the seven segment display. These LEDs are used to indicate:
1. When the delay mode is on
2. When the microcontroller is operating in low-cut or high-cut mode
3. When the microcontroller is operating in normal mode



PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

10

PICTURES (because nothing is complete without pictures):
(The circuits on verroboard are from the test stage. You can see the PCB at the end.)

PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

11






PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

12






PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

13







PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

14







PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

15


Output
voltage is
218V for
an input
voltage
of 175V
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

16


Output
voltage is
220V for
an input
voltage
of 154V
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

17








You can see the PICKIT3 and my laptop. This really is the entire test bench for this project.
Output voltage is 218V for an input voltage of 153V
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

18


PCB with the display board mounted. Output voltage is 201V for an input voltage of 138V
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

19



Closer
look at
the PCB
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

20









Thats me calibrating the PCB you can see my dad and Kamruzzaman as well
PIC16F873(A) based automatic voltage stabilizer Syed Tahmid Mahbub

21



ALL FILES
You can download all the files related to the voltage stabilizer:
Code: https://drive.google.com/file/d/0B4SoPFPRNziHbWtkVUc1VUM1REU/edit?usp=sharing
HEX: https://drive.google.com/file/d/0B4SoPFPRNziHbWtkVUc1VUM1REU/edit?usp=sharing
PCB: https://drive.google.com/file/d/0B4SoPFPRNziHdkE0SzI4SDQ5V3M/edit?usp=sharing

PCB images:
https://drive.google.com/file/d/0B4SoPFPRNziHNEtSQlVtX2x4VWc/edit?usp=sharing
https://drive.google.com/file/d/0B4SoPFPRNziHQXl2eWtydmh3WkU/edit?usp=sharing
https://drive.google.com/file/d/0B4SoPFPRNziHTktlc3dMamlFTjA/edit?usp=sharing
https://drive.google.com/file/d/0B4SoPFPRNziHdktCQ29kVkNJd1k/edit?usp=sharing


Conclusion:
Making the automatic voltage stabilizer was a great experience it was fun making it and it was
also a good learning experience. Ive shared all the files in hopes that itll help you make a
voltage stabilizer yourself. Do let me know what you think! Leave your comments and feedback
in the comments section. Dont forget to look at my Youtube videos where I demonstrate the
voltage stabilizer.