AUTOMATIC VOLTAGE STABILIZER USING MSP430

Photo of author

By Jackson Taylor

Home » Electronics Topics » Msp430 » AUTOMATIC VOLTAGE STABILIZER USING MSP430

As an Amazon Associate we earn from qualifying purchases.

In this project I converted an old manual voltage stabilizer into an automatic one. An MSP430G2553 microcontroller measures the mains voltage, a ULN driver IC switches relays, and the relays select the correct transformer tap. No one has to turn a knob when the supply drops or rises.

Project image

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Safety note: this project works directly with mains voltage, which can cause serious injury or fire. Only build it if you are experienced with mains wiring. Keep the low-voltage electronics isolated from the mains side, use an enclosure, and test with a bulb or a current-limited setup first.

Why make a stabilizer automatic?

A manual stabilizer usually has a switch or a knob that picks one of several taps on a transformer. Each tap gives a different boost or cut to the output. It works, but only if someone watches the supply and changes the setting. If the voltage sags in the evening and nobody notices, the connected appliance runs on low voltage for hours.

Automating the tap selection solves this. The controller watches the input, decides which tap suits the present voltage, and switches to it through a relay. The transformer and the wiring stay the same, so the conversion is cheap and simple.

How the system works

The design has four parts:

  • Voltage sensing: a scaled-down, rectified sample of the mains is fed to analog pin A0 of the microcontroller.
  • MSP430G2553: reads the sample with its ADC and decides which tap should be active.
  • ULN driver IC: the microcontroller pins cannot drive relay coils directly, so the ULN driver provides the extra current.
  • Relays: three relays connect the lower, middle or upper tap of the transformer to the output.

Only one relay is on at a time. The sketch is written for the Energia environment, which is why you see familiar setup() and loop() functions and pin names such as P1_3 and P2_3.

See also
SENSORS

Choosing the right tap

The program keeps a state variable, f, that records which tap is active: 1 for the lower tap, 2 for the middle tap and 3 for the upper tap. It compares the ADC reading with a few thresholds:

  • A reading of 211 or more (and below 250) selects the lower tap, which is used when the input is high enough to need little or no boost.
  • A reading above 185 and up to 210 selects the middle tap.
  • A reading of 186 or less selects the upper tap, which gives the most boost when the input is low.

The comments at the top of the sketch mention 210, 235 and 265 volt taps, and note the sensed voltage at the 210 V switching point (about 0.68 V on hold and 0.73 V on release). The ADC numbers depend on your own sensing divider and reference, so you will need to calibrate them for your build. Printing the reading over the serial port (the commented-out Serial.println(a) line) makes this easy.

Hysteresis and switching delay

Two details in the code stop the relays from chattering.

First, after switching to the middle or upper tap, the program stays in a small while loop and keeps reading the input until it moves clearly past the threshold (220 for the middle tap, 193 for the upper tap). This gap between the “switch on” and “release” points is hysteresis. Without it, a voltage hovering near a threshold would make the relays click on and off repeatedly.

Second, every change turns all relays off, waits two seconds, and only then switches on the new tap. This avoids two taps being connected at once and gives the load a short pause rather than a sudden jump. The cost is a brief output interruption when the tap changes, which is usually fine for most appliances.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

CIRCUIT

Circuit diagram

The circuit connects the sensing input to A0, and three output pins (P2_3, P1_3 and P1_4) to the ULN inputs. Each ULN output drives one relay coil. Use relays whose contacts are rated comfortably above the current of the load you plan to connect, and keep mains tracks and wiring well separated from the low-voltage section.

See also
MSP430-Digital Input

PROGRAM

//210-235-265 --SELECTED TAPS WHEN INPUT IS AT 210V AC
//210--0.68V -210 --- ON HOLD
//210--0.73V ON RELEASE
//210-235-265
//210--0.68V -210 --- ON HOLD
//210--0.73V ON RELEASE
#define lowertap P2_3
#define middletap P1_3
#define uppertap P1_4
int f=1,a;
void setup()
{
    Serial.begin(9600);
    initoutputpins ();
    offallrelay();
    digitalWrite(lowertap,HIGH);delay(2000);  
    f=1;  
}
void loop()
{
    a=analogRead(A0);   delay(10);    //Serial.println(a); delay(100); 
    if(a>=211 &&   a<250 && (f==2 || f==3) ) {   offallrelay();delay(2000);    digitalWrite(lowertap,HIGH); delay(2000);   f=1;   }
    else if(a>185 && a<=210 &&(f==1 || f==3)   ) { offallrelay();delay(2000);   digitalWrite(middletap,HIGH); delay(2000); f=2;
            while( a<220 && a>186   ){a=analogRead(A0);   delay(10);}
    }
    else if (a<=186 &&( f==1 || f==2) ) { offallrelay(); delay(2000);   digitalWrite(uppertap,HIGH); f=3; delay(2000); 
                while( a<193 ){a=analogRead(A0);   delay(10);}
    }
}
void initoutputpins (void)
{
    pinMode(lowertap,OUTPUT); pinMode(middletap,OUTPUT); pinMode(uppertap,OUTPUT);
}
void offallrelay(void)
{
    digitalWrite(lowertap,LOW);digitalWrite(middletap,LOW);digitalWrite(uppertap,LOW);  
}

PCB LAYOUT

PCB layout

The PCB holds the microcontroller, the ULN driver, the relays and the sensing circuit. When you build it, keep the relay contact tracks wide, leave clear spacing around the mains connections, and mount everything in an insulated box.

Testing and tuning

  1. Power the control board from a safe low-voltage supply first and confirm that the lower tap relay turns on at start-up.
  2. Feed the sensing input from a variable source or a variable transformer and watch the ADC value on the serial monitor.
  3. Adjust the threshold numbers in the code until each relay switches at the mains voltage you want.
  4. Connect the transformer with a bulb as a load, then check each tap while varying the input.
  5. Only after that, test with a real appliance at the rated load.

Ideas for improvement

  • Add a display or LEDs to show the input voltage and active tap.
  • Add an over-voltage cut-off that disconnects the output if the input is too high or too low.
  • Use the same relay-switching approach from the AC lamp blinking using MSP430 project if you prefer solid-state switching with a triac, bearing in mind its different heating and isolation needs.
  • Log the voltage over time with a real time clock to see when your supply tends to dip.

If you enjoy projects that deal with higher voltages, our high voltage buck converter build and the iron box automatic turn off system are worth a look.

See also
TEMPERATURE INDICATOR AND FAN CONTROLLER

FAQ

Why use relays instead of a manual switch?

Relays let the microcontroller change the transformer tap by itself. They also keep the low-voltage control circuit electrically separate from the mains side.

Why is a ULN IC needed?

The MSP430 output pins can supply only a small current, which is not enough for a relay coil. The ULN driver takes the small signal from the pin and switches the larger coil current.

Do I need to change the thresholds in the code?

Most likely, yes. The ADC values depend on your sensing divider, reference voltage and transformer tap voltages, so calibrate them against a known source before connecting a real load.

Can I use another microcontroller?

Yes. The logic is simple: read an analog input, compare with thresholds and drive three outputs. It can be ported to Arduino or another board with an ADC, though the pin names and ADC range will change.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works