As an Amazon Associate we earn from qualifying purchases.
Blinking an LED from a microcontroller is easy. Blinking a mains lamp is the same idea, but you need a safe way to switch AC power with a 3.3 V logic pin. This project does that with an MSP430 LaunchPad, an opto-isolated driver and a triac. The video above shows the project in action, and the sections below explain how it works and how to build it.
Warning: this circuit connects to mains voltage, which can cause serious injury or death. If you are not experienced with mains wiring, build and test the logic side first with a low-voltage lamp or just the LED inside the optocoupler.
How the circuit works
The MSP430 cannot drive an AC lamp directly. Its pins only supply a few milliamps at 3.3 V, and the lamp runs on alternating mains. The circuit therefore has three stages:
- MSP430 output pin: toggles high and low at the blink rate you choose.
- Optocoupler with a triac output: the MSP430 lights a small LED inside the optocoupler. Light, not a wire, triggers the output side, so the low-voltage and mains sides stay electrically isolated.
- Power triac: the optocoupler triggers the gate of the main triac, which then conducts and lets current flow through the lamp.
A triac is a bidirectional switch. Once its gate is triggered, it stays on until the current through it falls to nearly zero. On 50 Hz or 60 Hz mains that happens every half cycle, so the gate has to be triggered again each half cycle. In a simple on/off circuit, you just keep the optocoupler LED on for as long as you want the lamp lit.
Parts you need
- MSP430 LaunchPad (a MSP430G2553 or similar board works)
- Optocoupler with a triac output, such as the MOC3021 family
- A power triac rated well above your lamp’s current and voltage, for example a BT136 triac for small loads
- Resistors: one to limit the optocoupler LED current, and one from the optocoupler output to the triac gate (check the optocoupler and triac datasheets for values)
- An incandescent lamp as the load
- A lamp holder, an insulated enclosure, a fuse and a properly rated mains cable
- Jumper wires and a breadboard for the low-voltage side only
Use a plain incandescent lamp for your first test. It is a purely resistive load, so it is the easiest for a triac to switch cleanly.
Wiring the circuit
Low-voltage side
Connect a digital output pin of the MSP430 to the optocoupler’s input LED through a current-limiting resistor. Connect the other end of the LED to ground. The resistor value depends on the LED forward current in the datasheet and on the 3.3 V supply. Stay within what the pin can safely supply.
You can also use the LaunchPad’s on-board LED pin as a visual check. If the board LED blinks, the code is running, and the optocoupler LED should follow the same pin.
Mains side
The mains side is wired in series with the lamp:
- Take the live wire through a fuse.
- Connect it to the lamp, then to main terminal 2 (MT2) of the triac.
- Connect main terminal 1 (MT1) of the triac to neutral.
- Connect the optocoupler’s output through its gate resistor to the triac gate, with the other output pin going to MT2.
Check the pinout of your exact triac, because the pin order differs between packages and manufacturers. For more on this stage, see our guide to a triac gate driver with an optocoupler.
Keep the low-voltage wiring physically away from mains tracks and wires. Mount everything in an insulated box, and never touch the circuit while it is plugged in.
The program logic
The software side is the same as a normal blink program. You set one pin as an output and toggle it with a delay in between. If you have never done this on a microcontroller, our Arduino LED blinking project covers the basic idea, and it carries over directly to the MSP430.
On the MSP430 you can write it in Code Composer Studio or in Energia, which uses Arduino-style functions. The steps are:
- Stop the watchdog timer (in CCS), so the chip does not reset itself.
- Set the chosen pin as an output.
- In an endless loop, set the pin high, wait, set it low, wait.
A delay of half a second to one second on each state gives a clear, visible blink. Longer delays are easier on incandescent lamps, because their filaments heat up and cool down relatively slowly, so very fast blinking looks like a dim glow instead of flashing.
Testing safely
- With the mains disconnected, upload the program and confirm that the optocoupler LED blinks (you can use a multimeter or a spare LED in its place).
- Double-check the triac pinout and all mains connections, then close the enclosure.
- Connect mains power using a fused supply, ideally via a residual-current device (RCD/GFCI).
- Watch the lamp. It should turn on and off in step with the MSP430 pin.
- Unplug the circuit before making any changes.
Troubleshooting
- Lamp never turns on: check the triac pinout, the gate resistor and the optocoupler orientation. Also confirm the MSP430 pin is actually toggling.
- Lamp stays on all the time: the triac may have failed shorted, or the gate may be picking up unwanted current. Disconnect and test the parts.
- Lamp flickers or glows faintly when it should be off: triacs can leak a little current. This is more noticeable with low-power loads. A snubber network across the triac is often added to reduce false triggering, particularly with inductive loads.
- Works with a filament lamp but not an LED or CFL bulb: a triac needs a minimum load current to stay on (its holding current). Very low-power bulbs may not meet it, and many LED bulbs have electronics that do not like triac switching.
- Triac gets hot: check that its current rating suits the lamp, and add a heatsink if you switch larger loads.
Where to go next
Once on/off switching works, the next step is controlling how much power reaches the lamp. That needs a zero-crossing detector and a firing delay in each half cycle, a technique called phase control. Our article on AC power control using triac and diac explains the principle behind dimming. You can then use the MSP430’s timers to trigger the gate at precise moments instead of delays in a loop.
FAQ
Why do I need an optocoupler between the MSP430 and the triac?
It isolates the low-voltage microcontroller from the mains. If something fails on the AC side, the isolation helps stop mains voltage from reaching your LaunchPad, your computer’s USB port and you.
Can I use a relay instead of a triac?
Yes, for simple on/off blinking a relay module works. A triac has no moving parts, switches silently and can be used for dimming through phase control. Relays wear out with frequent switching, so a triac is better for fast blinking.
Does this work with other MSP430 boards?
The circuit is the same for any MSP430 board. Only the pin name and the code setup change. Make sure the pin can supply enough current for your optocoupler LED, or add a transistor.
Can I blink an LED bulb this way?
Sometimes, but results vary. Many LED bulbs draw too little current for the triac to latch reliably, and some flicker or glow when off. A resistive incandescent lamp is the most predictable load for testing.