How to Control AC Mains with a Microcontroller?
Published on 1/22/2017 5:58:44 PM
Description
<style>.e_editor{font:14px/24px Arial,'microsoft yahei','Times New Roman','b8bf53';}.e_editor div,e_editor p,e_editor td,e_editor th,e_editor li{font-weight:inherit;font-size:inherit;font-style:inherit;font-family:inherit;}.e_editor ul{margin:-10px 0 20px;}.e_editor li{padding:5px 0;}.e_table{width:96%;border-collapse:collapse;empty-cells:show;}.e_table th,.e_table td{padding:5px;border:2px solid #eee;}.e_img{padding:10px 0;text-align:center;}.e_p{line-height:20px;padding:0 0 20px;text-indent:0em;}</style> <div class="e_editor"> <div class="e_p"> Introduction </div> <div class="e_p"> The world is getting smaller and so are electronics designs.<a href="https://www.allpcb.com/soldering_thru-hole_component.html" target="_blank"> Through-hole components</a> are less profitable for parts manufacturers, and as a result, new products are oftentimes only available in surface mount packages. Being able to solder surface mount parts by hand is a very valuable skill and can come in handy for parts like chip resistors and caps, small outline packages (SOIC/TSSOP), and quad flat packages (QFP). Still, what if the perfect ARM core for your quadcopter controller only comes as a quad-flat no-leads (QFN)? What if that FPGA you need to do vision processing with is only in a ball-grid array (BGA) style? You can take your chances with a hot air gun or you can use a solder reflow oven. </div> <div class="e_p"> Solder reflow ovens are nothing new, nor is making one for yourself out of a standard toaster like this one. The issue is that commercial options are generally very expensive (the one above may cost upwards of $2,000 USD) and many home-built options often require dismantling and modding the toaster, which can be error-prone and require special tools (my own Black & Decker Toasr-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 even AAC author Robert Keim has some tutorials on the basic concepts of oven controlling, such as 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. </div> <div class="e_p"> What You Need </div> <div class="e_p"> <table> <tbody> <tr> <th> Qty </th> <th> Part </th> <th> Price (USD) </th> </tr> <tr> <td> 1 </td> <td> Plastic enclosure </td> <td> $1.24 </td> </tr> <tr> <td> 1 </td> <td> IEC 10A plug with fuse holder and switch </td> <td> $3.99 </td> </tr> <tr> <td> 2 </td> <td> 10A glass fuses </td> <td> $0.44 </td> </tr> <tr> <td> 1 </td> <td> 15-Amp Tamper Resistant Single Outlet </td> <td> $2.99 </td> </tr> <tr> <td> 2 </td> <td> Six-position screw terminal block </td> <td> $3.50 </td> </tr> <tr> <td> 1 </td> <td> 47R 1/4 watt resistor </td> <td> $0.10 </td> </tr> <tr> <td> 1 </td> <td> 180R 1/4 watt resistor </td> <td> $0.10 </td> </tr> <tr> <td> 2 </td> <td> 1K 1/4 watt resistor </td> <td> $0.20 </td> </tr> <tr> <td> 1 </td> <td> 4.7K 1/4 watt resistor </td> <td> $0.10 </td> </tr> <tr> <td> 1 </td> <td> 22K 1/4 watt resistor </td> <td> $0.10 </td> </tr> <tr> <td> 2 </td> <td> 220K 1/4 watt resistor </td> <td> $0.20 </td> </tr> <tr> <td> 1 </td> <td> 100nF 275V X-class film capacitor </td> <td> $0.65 </td> </tr> <tr> <td> 1 </td> <td> 1nF 1kV ceramic capacitor </td> <td> $0.25 </td> </tr> <tr> <td> 1 </td> <td> 10uF 35V aluminum capacitor </td> <td> $0.15 </td> </tr> <tr> <td> 5 </td> <td> 1N4148 (or equivalent) diode </td> <td> $0.55 </td> </tr> <tr> <td> 2 </td> <td> General purpose NPN BJT (2N3904 or 2N5551) </td> <td> $0.40 </td> </tr> <tr> <td> 1 </td> <td> 4N35 opto-isolator </td> <td> $0.48 </td> </tr> <tr> <td> 1 </td> <td> MOC310 opto-isolator </td> <td> $0.72 </td> </tr> <tr> <td> 1 </td> <td> BTA312-600B TRIAC </td> <td> $0.99 </td> </tr> <tr> <td> 1 </td> <td> TO-220 heatsink </td> <td> $1.60 </td> </tr> <tr> <td> 1 </td> <td> T0-220 mounting hardware </td> <td> $2.07 </td> </tr> <tr> <td> 1 </td> <td> 185V varistor </td> <td> $0.47 </td> </tr> <tr> <td> 1 </td> <td> Misc mounting hardware </td> <td> ~ </td> </tr> <tr> <td> 1 </td> <td> Misc hookup wire </td> <td> ~ </td> </tr> <tr> <td> <strong>TOTAL</strong> </td> <td> </td> <td> ~$22.00 </td> </tr> </tbody> </table> </div> <div class="e_p"> A Few Notes on Safety </div> <div class="e_p"> A rock climbing instructor jokingly told me once that there are three rules to heed when it comes to safety. In order of priority, they are: </div> <div class="e_p"> <ol> <li> Look good. </li> <li> Don't die. </li> <li> If you have to die, look good doing it. </li> </ol> </div> <div class="e_p"> I promise you that you will be in violation of all three 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: </div> <ul> <li> <div class="e_p"> Don't connect high voltages to a breadboard. The risk of wires coming loose or accidentally touching / plugging into the wrong hole on a breadboard is not worth it. Soldering components to perfboard should be OK for prototyping though. </div> </li> <li> <div class="e_p"> Absolutely 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, use an incandescent bulb as a test load and remotely turned on the device with a surge protector switch. </div> </li> <li> <div class="e_p"> 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. </div> </li> </ul> System-Level Design <div class="e_p"> We're only going to be making the AC waveform dimmer here. Note that this is an ad-hoc device in that it is designed to controlling <strong>resistive loads only</strong>, like most toaster ovens. Capacitive and inductive loads require some slight modifications (the addition of snubber components) which won't be covered here 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. </div> <div class="e_p"> A very common way of implementing 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. Inside most solid state relays, however, is a device called a TRIAC which can be ordered as a standalone device. Like Robert mentions in his article, it's essentially a bidirectional extension of the thyristor, or can be though of as a solid-state switch that conducts current in both directions. </div> <div class="e_p"> Not as flashy as the SSR but our TRIAC does some super cool stuff </div> <div class="e_p"> The whole idea of this oven controller is to use the TRIAC to implement something called AC phase control. If you wait for the zero-crossing of the AC waveform and turn the TRIAC on at some known time later, you are 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, effectively dimming it. Other methods of dimming exist like wave packet control (a sort of synchronous BANG-BANG paradigm; sorry no EN WikiPedia) but they are beyond the scope of this project. </div> <div class="e_p"> Plugs, Terminals, and Enclosure </div> <div class="e_p"> Safe and cheap is the name of the game here, so the first order of business in making this was to select 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 the plugs can be cut or drilled in the side but care needs to be taken to keep the material from cracking. </div> <div class="e_p"> 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: </div> <div class="e_p"> 10A IEC plug with integrated fuse holder and switch </div> <div class="e_p"> It takes care of over-current protection and allows me to turn the whole thing off without having to unplug the cables. On the toaster side, I just browsed through my local hardware store and came upon this: </div> <div class="e_p"> Single tamper-resistant outlet </div> <div class="e_p"> 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 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. </div> <div class="e_p"> Isolated Zero-Crossing Detector </div> <div class="e_p"> When controlling or measuring high voltage circuits with low voltage devices, it's always a good idea to employ some kind of galvanic isolation between the two sides; this can be accomplished either inductively, optically, or capacitively. There are a few other isolation methods but these are the big ones. </div> <div class="e_p"> Robert's TRIAC controller and zero-crossing detector use the transformer in a wall wart to step down the mains to a safer 12V before interacting with it. This application uses opto-isolators to separate the high and low voltages which has 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. </div> <div class="e_p"> This circuit was lovingly borrowed from here. The author does an excellent job explaining the circuit in detail but a quick rundown goes like this: the mains waveform is first filtered and rectified. It's voltage is divided which then charges the 10uF cap. When the divided voltage drops below the voltage on the capacitor, the comparator transistor turns on, activating the opto-isolator. The output has an open collector which means you can operate it at any VCC your microcontroller supports. My perfboard circuit looks like this: </div> <div class="e_p"> A satisfyingly symmetrical circuit </div> <div class="e_p"> I tested this circuit in isolation from the rest of the board with a modified power cable and a surge protector. 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. For the love of God don't hook your mains to your scope!): </div> <div class="e_p"> TRIAC Driver and Isolated Driver Circuit </div> <div class="e_p"> Next up is the TRIAC and the isolated driver circuit. I mentioned earlier Andy Brown's tutorial. I adapted his TRIAC protection and driver circuit to work on 120VAC here in the States and followed his thermal considerations for heatsink selection. The TRIAC we're using is the BTA312. We use another opto-isolator to drive the TRIAC called the MOC310M which requires between 30 and 60mA to turn on. Most microcontrollers aren't comfortable sourcing this kind of current so we use a general purpose NPN transistor to provide it. </div> <div class="e_p"> VR1 is a varistor. It serves as over-voltage protection in case there's a spike in the AC line. C3 is a 275VAC film cap for emission suppression. That one could be considered optional. </div> <div class="e_p"> The heatsink, TRIAC, varistor, filter cap, and screw terminals all live on a main board separate from the driver. </div> <div class="e_p"> I used the mounting holes at the corners of the main board to attach it to the enclosure. </div> <div class="e_p"> Now you should be ready to roll! Connecting the wires appropriately (this page was helpful), connect up VCC and GND to a bread board power supply, and flip the power switch. If you apply 3.3V to the TRIAC_ACTIVE line, you should get 100% power on the other end. </div> <div class="e_p"> Next Steps </div> <div class="e_p"> Okay, I admit it: turning on a light bulb with a 3.3V 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? <strong>Absolutely not!</strong> 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. </div> </div>
402
comment
All comments
Unknown
2881
0
402
Rules about cashback: 1. Valid time: ALLPCB cashback activity will end on April 1st. 2. Capped amount: The capped amount of cashback for each account is $5,000. Each order can get a maximum of $2,000 cashback. That means every author can get $5,000 max. 3. Cashback range: The cashback activity only covers the corresponding PCB order. The order amount for other combined payment products will be invalid. 4. Clicking your own promotional link will be invalid. The same email address, shipping address, contact information, and phone number are all recognized as the same account. 5. ALLPCB has the final interpretation right of the cashback activity.
ALLPCB will donate 2% to the author for this promotion link.