Microcontrollers are a ton of fun. Once I got hooked, there was no
turning back. Initially playing with sensors and LCDs, I quickly discovered the limits to what a microcontroller could control. A microcontroller’s GPIO (general purpose input/output) pins cannot handle higher power requirements. An LED was easy enough, but large power items such as light bulbs, toaster ovens, and blenders required more sneaky circuitry. Something sneaky called a relay:
In this tutorial we will discuss a small relay board to control the power to a normal AC outlet using 5VDC control.
A relay is a large mechanical switch. That switch is toggled on or off by energizing a coil.
The other half the relay is called the coil. This is basically a small electro-magnet. If you send current through the coil, a magnetic force is created, which pulls on the steel paddle causing it to move (flip) and touch the copper paddle – as if you flipped a light switch. The coil requires a small amount of power (5VDC @ 80mA). So you see, controlling the low-power coil allows us to actually control quite a lot of power!
It is important to note the coil is physically isolated from the paddles. If you have 120VAC running through the paddles, you don’t have to worry about that 120VAC sneaking back into and vaporizing your microcontroller (connected to the coil).
The paddles are capable of carrying very large currents. Both AC or DC – the paddles don’t care. A relay can be used to control a DC motor, or an AC lamp.
The relay that we will be working with, in this tutorial, in my opinion. It can handle a lot of power – 30A at 220VAC. What happens if you violate this limit? I have thankfully never been in that situation. I have heard reports that the relay will begin to heat up. When the voltage/current becomes large enough, there will be sparks inside the relay as you switch the paddles. If these sparks get large enough, you can actually spot weld the movable paddle to stationary paddle causing the relay to fail, potentially in the ‘on’ position. Obviously this would be very bad on many levels.
Like we do with capacitors, we under-rate the relay so that we mitigate the risk of relay failure. If you need 10A@120VAC, don’t use a relay rated for 10A@120VAC, instead use a bigger one (such as 30A@120VAC). Remember, power = current * voltage so a 30A@220V relay can handle up to a 6,000W device (two hair dryers).
When the ‘RELAY’ pin (aka CTRL) goes high, the NPN transistor connects to ground sending current through the coil (activating the relay) and through the LED (turning the activation LED on). R1 pulls the ‘RELAY’ pin to ground so if anything goes haywire the relay will remain in the safe, off position.
Note: The 1N4148 diode is connected in a odd fashion for a reason. This is placed between power and ground in a reverse fashion. When the coil of the relay is de-activated, it acts like an inductor, trying to suppress current change. This can cause some havoc on the 5V power rail. When this happens, the 1N4148 will forward bias causing the current stored in the coil to flow happily back to the 5V rail protecting the power supply and the near-by parts.
Take that beautiful extension cord and cut off the female connector about 6″ from the female end.
Note: A two-wire extension cord will not work correctly. Notice we are using thick, three-wire circular extension cord. This extra wire is the ground return and allows the GFCI to operate correctly.
Using a meter set to continuity, check that the ground pin (the round one) is indeed connected to the green ground wire. I’ve seen a few extension cords with non-standard colors.
Conversely, when you send 5V to the coil, the paddle flips from the ‘off’ state to the ‘on’ state, connecting the two pieces of black wire (on the left side of the picture above), power is delivered to the outlet and your project is powered.
Now for the moment of truth. Attach the three control wires (5V, GND, and CTRL) to some sort of system. In the above picture, I have a fairly dirty breadboard. All that I am actually using on the bread board is 5V and GND – ignore all the other parts as they are not doing anything. I then manually toggled the control wire from GND (off) to 5V (on). You can do the same thing by plugging into the 5V and GND pins on an Arduino board.
Tying the CTRL line to 5V I heard a very friendly click as the relay kicked over. This indicated (along with the LED on the control board) that the relay was actuated to the ‘on’ position. Removing CTRL from the 5V rail (called floating because the CTRL line is neither connected to 5V or GND), the relay released. This is good! If CTRL is left floating or tied to ground, the outlet is turned off.
You can also use a meter in continuity mode to check that the relay is working properly before youconnect to 120VAC. When the relay’s open, one of the fins of the plug and one of the rectangular holes of the outlet will not have continuity, and when it’s closed, they will. The other fin and rectangular hole will always have continuity, as will the ground pin and the funny hole. I always do this check before plugging into the 120VAC, because I am, you know, paranoid.
The next step is to plug the extension cord into the wall and test again. If anything goes wrong the GFCI should activate and cut off. Be sure to unplug the outlet anytime you are working on it. Please don’t get zapped!
You should now have an outlet that is fully controllable over 5V logic. When you plug a device into the outlet, it will by default be off. When you expose 5V to the CTRL line, the relay will activate turning on power to the device plugged into the outlet.