POWER CONTROL USING TRIAC AND ARDUINO

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » POWER CONTROL USING TRIAC AND ARDUINO

As an Amazon Associate we earn from qualifying purchases.

This project controls the power delivered to an AC bulb with an Arduino and a triac. An MCT2E opto-coupler detects the zero crossing of the mains voltage, and a MOC3021 opto-coupler triac driver fires a BT136 triac at a chosen point in each half cycle. It works like the LED fading example that ships with the Arduino IDE, except that the load is an AC lamp instead of a DC LED.

Warning: this circuit is connected to mains voltage. Only build it if you know how to work safely with AC. Keep everything in an insulated enclosure, never touch the circuit while it is plugged in, and do not connect a USB cable to your PC while the Arduino side is live unless the circuit is properly isolated.

How AC power control with a triac works

A triac is a semiconductor switch that conducts in both directions, so it suits AC loads. Once a short pulse on its gate turns it on, it stays on until the current falls to zero. For mains, that happens at the end of each half cycle.

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

Because of this, you control power by choosing when in each half cycle the gate pulse arrives. This is called phase angle control:

  • A pulse right after the zero crossing lets almost the whole half cycle through, so the lamp is bright.
  • A pulse late in the half cycle lets only a small part through, so the lamp is dim.

To do this, the Arduino needs to know where each zero crossing is. It then waits a set time and sends the gate pulse. The same idea is used in the power control with a triac and diac approach, except there the timing comes from an analog RC network instead of a microcontroller.

Components and what each one does

  • Arduino board: reads the zero crossing signal on pin 4 and drives the triac driver from pin 10.
  • MCT2E opto-coupler: gives the Arduino an isolated pulse at each zero crossing of the mains. You can read more in this zero crossing detector using an opto-coupler project.
  • MOC3021 opto-coupler triac: isolates the Arduino from the mains and supplies the gate current that fires the main triac.
  • BT136 triac: switches the bulb on the mains side.
  • AC bulb: an ordinary incandescent lamp is the easiest load for this test.
See also
SCADA WITH ARDUINO OR ENERGIA

The MOC3021 is a random-phase driver, which is what you need for phase control. A zero-cross type driver would only switch on at the zero crossing and could not dim the lamp. For the driver side in more detail, see this triac gate driver with an opto-coupler article.

Demonstration video

Zero crossing signal

The image below shows the output of the zero crossing opto-coupler (yellow) next to the input sine wave. The Arduino uses this pulse as its timing reference for every half cycle.

Zero crossing opto-coupler output waveform and input sine wave

Output waveform at the zero crossing opto-coupler (yellow) and the input sine wave.

Circuit

The circuit has two isolated parts. The MCT2E senses the mains and feeds the zero crossing pulse to Arduino pin 4. On the output side, Arduino pin 10 drives the MOC3021, which triggers the gate of the BT136 in series with the bulb. Check the resistor values and ratings in the diagram against the datasheets of your parts before building.

TRIAC power control circuit with MCT2E, MOC3021 and BT136

Program


int a,i;
void setup() 
{ 
pinMode(4,INPUT);
pinMode(10,OUTPUT);
}
void loop() 
{
  for(i=0;i<10;i++)
  {
    for(int j=0;j<10;j++)
    { 
    waitfornextzerocrossing ();delay(i);   digitalWrite(10, HIGH); delayMicroseconds(200);   digitalWrite(10, LOW); 
    }
  }
    for(i=9;i>0;i--)
    {
      for(int j=0;j<10;j++)
      { 
      waitfornextzerocrossing ();delay(i);   digitalWrite(10, HIGH); delayMicroseconds(200);   digitalWrite(10, LOW); 
      }
    }
}
void waitfornextzerocrossing (void)
{
  do
    { 
      a =digitalRead(4); 
    }
  while(!a); 
}

How the sketch works

The sketch repeats the same three steps on every half cycle:

  1. Wait for the zero crossing. The function waitfornextzerocrossing() reads pin 4 in a loop until it goes HIGH.
  2. Delay. delay(i) waits i milliseconds after the crossing. A longer delay means the triac fires later and the lamp gets less power.
  3. Fire the triac. Pin 10 goes HIGH for 200 microseconds and then LOW. This short pulse is enough to latch the triac on until the next zero crossing.

The outer loops change the delay. In the first loop, i goes from 0 to 9, and each value is held for 10 half cycles. The lamp starts at full brightness and fades down. The second loop counts from 9 back to 1, so the lamp fades up again. The result is a continuous fade in and out on AC, much like the fading LED example on DC.

See also
THERMAL PRINTER INTERFACING WITH ARDUINO

Things to watch for

Mains frequency

A half cycle lasts 10 ms on 50 Hz mains and about 8.3 ms on 60 Hz. The sketch uses delays from 0 to 9 ms, which fits 50 Hz. On 60 Hz, keep the maximum delay below about 8 ms, otherwise the pulse may fall outside the half cycle and the lamp will flicker or go out.

Zero crossing detection

The function returns as soon as it reads pin 4 HIGH. This works when the pulse is short compared with the delay. If you see flicker, make the code wait for the pin to go LOW first, or use an external interrupt on pin 2 or 3 for steadier timing.

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

Choice of load

  • Use an incandescent lamp or another resistive load for testing.
  • Most LED and CFL lamps are not designed for phase-cut dimming and may flicker or buzz.
  • Motors and other inductive loads usually need a snubber network across the triac, and the gate timing may need adjusting.
  • Check the current rating of the BT136 against your load, and add a heatsink if it will run warm.

Where to go next

Once the fade works, you can replace the fixed loops with a potentiometer or serial input to set the delay. A similar idea is used in triac based speed control of a household fan. If you use a different microcontroller, the timing logic is the same, as in this example of blinking an AC lamp with an MSP430.

FAQ

Why do I need a zero crossing detector?

The Arduino has to fire the triac at a known point in each half cycle. Without a zero crossing reference, the delay would not be tied to the mains waveform and the brightness would be unstable.

Can I use a zero-cross optotriac like the MOC3041 instead?

Not for dimming. Zero-cross drivers only turn on near the zero crossing, so you cannot delay the firing. Use a random-phase type such as the MOC3021 for phase angle control.

See also
ARDUINO AND SERVO MOTOR INTERFACING SIMULATION IN PROTEUS

Why is the trigger pulse only 200 microseconds?

A triac only needs a short gate pulse to latch on, and it stays on until the current drops to zero. A short pulse also keeps the gate and driver dissipation low.

Can I use this to dim an LED bulb?

Usually not. Only LED bulbs marked as dimmable work with phase-cut control, and they may still need a minimum load or different timing. Test with an incandescent lamp first.

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