Project

Control Your AC Mains with a Microcontroller

November 09, 2015 by Patrick Lloyd

Learn how to put together your own toaster oven controller with a handful of inexpensive, through-hole parts. The emphasis is on safety, low price, and minimal modification of the toaster oven.

Introduction

The world is getting smaller and so are electronics designs. Through-hole components 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. 

Oooh shiny... But at what cost!?

 

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.

What You Need

A Few Notes on Safety

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:

  1. Look good.
  2. Don't die.
  3. If you have to die, look good doing it.

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:

  • 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.

  • 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.

  • 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'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 resistive loads only, 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.

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.

The solid state relay. Bang-bang, baby!

 

Not as flashy as the SSR but our TRIAC does some super cool stuff

 

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.

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 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.

 

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. On the toaster side, 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 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 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.

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.

 

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:

A satisfyingly symmetrical circuit

 

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!):

TRIAC Driver and Isolated Driver Circuit

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.

The schematic looks like this:

 

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. The MOC310 driver board circuit looks like this:

 

The heatsink, TRIAC, varistor, filter cap, and screw terminals all live on a main board separate from the driver. Once everything is 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 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.

Video

Next Steps

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? 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. KiCad schematic files are also available below:

Code

triac-dimmer.zip

Give this project a try for yourself! Get the BOM.

10 Comments
  • JBaczuk December 17, 2015

    Great tutorial, thanks!  To anyone ordering parts, be careful ordering rectifier diodes, don’t order the 1N4148 that isn’t rated for 120V!  Had to redo my rectifier…

    Like. Reply
    • onion March 03, 2016
      Hey, this is the author here. You are totally right -- sorry about that error. Those diodes are from the 1N400x family of general purpose rectifier diodes. I don't have the circuit in front of me but I think they are 1N4004. Any of those diodes from 1N4004 to 1N4007 will work, but the higher the value, the larger the diode. I'll correct this in the article.
      Like. Reply
  • P
    patman March 02, 2016

    For anyone reading along with the source article for the Zero-Crossing Detector, this post switches C1 and C2 designations relative to the original. Had me stumped while reading through the full functional description.

    Like. Reply