Use a Microcontroller to Control Your AC Mains
2017-08-28 | By All About Circuits
License: See Original Project
Courtesy of All About Circuits
As electronic designs get smaller, through-hole components become less common in newer products in favor of surface mount packages. Being able to solder surface mount yourself by hand is a very valuable skill and comes in handy for parts like chip resistors and caps, small outline packages (SOIC/TSSOP), and quad flat packages (QFP). What do you do if the perfect ARM core for your quadcopter controller only comes as a QFN (quad-flat no-leads)? What if that FPGA you need for vision processing with is only in a BGA (ball-grid array) style? You can take your chances and roll the dice with a hot air gun or you can use a solder reflow oven.
Oooh shiny... But at what cost!?
Solder reflow ovens aren’t new, neither is making one yourself out of a standard toaster like this one. The issue is that commercial options are usually expensive (the one probably costs around $2,000 USD) and many options built at home often require dismantling and modding the toaster, which can be error-prone and often require special tools (my own Black & Decker Toast-R-Oven has security Torx screws in it...).
Sparkfun published a tutorial back in 2006, Andy Brown created a beautiful design on his blog, and Robert Keim from All About Circuits has some tutorials on the basic oven controlling concepts, like zero-cross detection and controlling a TRIAC. This set of tutorials aims to flesh out some of the concepts and provide a different hardware and (eventual) software approach to this application. It's another “recipe in the cookbook,” if you will.
Required Materials
- Plastic enclosure
- IEC 10 A plug with fuse holder and switch
- 10 A glass fuses X2
- 15-A Tamper Resistant Single Outlet
- Six-position screw terminal block X2
- 47 R 1/4 watt resistor
- 180 R 1/4 watt resistor
- 1 K 1/4 watt resistor X2
- 4.7 K 1/4 watt resistor
- 22 K 1/4 watt resistor
- 220 K 1/4 watt resistor X2
- 100 nF 275 V X-class film capacitor
- 1 nF 1 kV ceramic capacitor
- 10 uF 35 V aluminum capacitor
- 1N4004 (or equivalent) diode X5
- General purpose NPN BJT (2N3904 or 2N5551) X2
- 4N35 optoisolator
- MOC310 optoisolator
- BTA312-600B TRIAC
- TO-220 heatsink
- T0-220 mounting hardware
- 185 V varistor
- Misc mounting hardware
- Misc hookup wire
A Few Notes on Safety
A rock climbing instructor once told me jokingly that there are only three rules to heed when it comes to safety. In order of priority, they are:
- Look good.
- Don't die.
- If you have to die, look good doing it.
I can promise that you’ll be in violation of all three rules if you don't respect how dangerous mains voltages can be. There are a few things to keep in mind when dealing with high voltages:
- Don't connect high voltages to a breadboard. The risk of wires coming loose or accidentally touching or plugging into the wrong hole on a breadboard is not worth it. Soldering components to perfboards is OK for prototyping though.
- Keep your hands off whenever mains are connected. If you have to measure high voltages with your multimeter, don't hold the probes to the board by hand; attach alligator clips and operate the device remotely. Better yet, you can use an incandescent bulb as a test load and remotely turn on the device with a surge protector switch.
- Above all else, be smart about this. If you don't know what you are doing or don't feel comfortable doing it, ask for help and find someone who does. The AAC forums and local hackerspaces are great resources to leverage.
System-Level Design
A full oven controller consists of several parts:
Toaster oven system flowchart
We will only be making the AC waveform dimmer here. This is an ad-hoc device in that it is designed, like most toaster ovens, only for controlling resistive loads. Capacitive and inductive loads require some slight modifications (the addition of snubber components) which we won’t be covering in this project, but information is available online and in the component datasheets. Compact fluorescent lamps (CFLs) have a fairly complicated electronic ballast circuit inside their housing that is not compatible with the AC dimmer at all.
A common way of to implement an AC controller is with a solid-state relay. These allow the oven to be full ON or full OFF and the signal can be pulsed to get an approximate temperature (known colloquially as BANG-BANG controlling). A huge swath of the world's control systems run perfectly fine on BANG-BANG controllers but they are neither elegant nor super interesting to implement. However, inside most solid state relays, there’s a device called a TRIAC which can be ordered as a standalone device. Like Robert mentions in his article, a TRIAC is essentially a bi-directional extension of the thyristor, or can be thought of as a solid-state switch that conducts current in both directions.
The solid state relay. Bang-bang, baby!
While it isn’t flashy like the SSR, our TRIAC does some super cool stuff
The whole idea behind this oven controller is using the TRIAC to implement something called AC phase control. If you wait for the AC waveform’s zero-crossing and turn the TRIAC on at some known time later, you will be left with an output waveform that retains the same frequency and magnitude of the original waveform for the time that the TRIAC is active. This limits the amount of power to the end device, which effectively dims it. There are other methods of dimming like wave packet control (a sort of synchronous BANG-BANG paradigm; sorry no EN Wikipedia)
Example of AC phase control from Andy's Workshop
Plugs, Terminals, and Enclosure
Safe and cheap is the name of the game here, so the first order of business in making this was selecting a reasonable enclosure. I got an inexpensive plastic bin with a lid and wrote a nice and scary warning label for myself and anyone around. Holes for the plugs can be cut or drilled in the side but care needs to be taken to keep the material from cracking.
I opted for a plug and socket interface to the dimmer. On the AC mains side, I used a 10A IEC plug with an integrated fuse holder (the fuses I had to source myself) and a power switch, much like this one:
10A IEC plug with integrated fuse holder and switch
It takes care of over-current protection and allows me to turn the whole thing off without having to unplug the cables. For my toaster’s outlet, I just browsed through my local hardware store and came upon this:
Single tamper-resistant outlet
It's a single tamper-resistant outlet that screws into the enclosure. I didn't want to solder the high voltage wires directly to my board so I got a six-position screw terminal block. The positions on the terminal block are for MAINS_L, MAINS_N, OVEN_L, OVEN_N, and two mains ground wires. I also used one of these terminal blocks for the four microcontroller wires leading outside of the box. A bit overkill but it's what I had on hand.
Isolated Zero-Crossing Detector
When using low voltage devices to measure or control high circuits, it's always a good idea to employ some kind of galvanic isolation between the two sides. Galvanic isolation can be accomplished either inductively, optically, or capacitively. There are a few other isolation methods but these three are the big ones.
The TRIAC controller and zero-crossing detector projects that Robert made use the transformer in a wall wart to step down the mains to a safer 12 V before interacting with it. This application uses optoisolators to separate the high and low voltages which have the advantage of being far lighter and more compact than a bulky transformer. They're slow to react to fast signal changes compared to some of the other methods but at sub-kilohertz speeds like our application, it doesn't really matter.
The author did a great job explaining the circuit in detail, but here’s a quick rundown of their circuit: the mains waveform is first filtered and rectified. Its voltage is divided which then charges the 10 µF capacitor. When the divided voltage drops below the voltage on the capacitor, the comparator transistor turns on, activating the optoisolator. The output has an open collector which means you can operate it at any VCC your microcontroller supports. My perfboard circuit looks like this:
A satisfyingly symmetrical circuit
I used a modified power cable and a surge protector to test this circuit in isolation from the rest of the board. The zero-crossing detector waveform superimposed on an AC sinusoid should look something like this (I used a step-down transformer to get the shot. Also, DO NOT hook your mains to your scope!)
TRIAC Driver and Isolated Driver Circuit
The next order of business is the TRIAC driver and the isolated driver circuit. I mentioned Andy Brown's tutorial earlier. I adapted his TRIAC protection and driver circuit to work on 120 VAC here in the States and followed his thermal considerations to select my heatsink. The TRIAC we're using is the BTA312. We used a different optoisolator called the MOC310M to drive the TRIAC. The MOC310M requires between 30 mA and 60 mA to turn on. Most microcontrollers aren't comfortable sourcing this kind of current so we use a general purpose NPN transistor to provide it.
The schematic looks like this:
VR1 is a varistor that serves as over-voltage protection in case there's a spike in the AC line. C3 is a 275 VAC film cap for emission suppression. That one could be considered optional. The MOC310 driver board circuit looks like this:
The TRIAC, heatsink, filter cap, varistor, and screw terminals all live on the main board separate from the driver. Once we have everything attached to the board through headers, it should look something like this:
I used the mounting holes at the corners of the main board to attach it to the enclosure. Once everything is put together you get this:
Now we should be ready to roll! After connecting the wires appropriately (this page was helpful), connect VCC and GND to a breadboard power supply, and flip the power switch. If you apply 3.3 V to the TRIAC_ACTIVE line, you should get 100% power on the other end.
Next Steps
Okay, I’ll admit it: turning on a light bulb with a 3.3 V switch isn't that impressive. In fact, it's pretty much just a BANG-BANG controller at that point. What we need next is a controller that can measure the zero-cross signal, dim the line accordingly, and read input from a temperature sensor. That will all be covered in the next installment. What's that you ask? Will I use an Arduino? Absolutely not! It may look like that on the outside but we're going to be playing fast and loose with bare-metal C on the Atmega328P. Until next time, happy hacking.
All project files and documentation can be found on my repository on GitHub -- feel free to contribute.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum