Warning!!! This project deals with AC electricity which is dangerous if you don’t know how to treat it safely. You must treat electricity with caution. There are many books and websites about electrical safety procedures and if you’re not sure how to be safe you should read that information. The most basic advice I can give is always assume any exposed wires are live and touching them will hurt a lot at best and kill at worst.

Microcontrollers are good at controlling small devices, but frequently we DIY-ers want to use them to control things that aren’t so micro. In this post I’ll talk about how to turn on household lights with the Arduino microcontroller. Actually this technique isn’t limited to lights, it works for anything that gets plugged into the wall like a table saw or a small rail gun.

The first thing you need is a cheap extension core that you are willing to cut in half. After cutting and stripping the wires you need to solder in a relay. A relay is just like a light switch only instead of using your finger to flip the switch you use a small amount of voltage. Any 5 or 12 volt relay would work, but I already had some OJE-SH-105DM relays sitting around. These relays handle 5Amps at 240V AC. This means they can safely handle 5 Amps at 120V like you find in the US power grid. To get a feel for how much power that is I used Ohm’s Law which states amps*volts=power or in our case 5A * 120V yields 600 watts. That’s enough wattage for me, but if you want to blow up the moon with a giant laser then you just need to use a bigger relay.

I spliced the relay into the black wire on my power cord. For safety reasons you must splice the relay into the live wire. The standard coloring convention is that the black wire is live and the white wire is neutral. If you splice the relay into the wrong wire even when the relay is off the light would still have power to it and you could get electrocuted. I used a DMM to verify which wires were active, neutral, and ground. You can see the schematic for details. Once the relay is wired into the cable I verified that when I applied the trigger voltage (+5V for my 105DM) to the relay that I could hear it flip positions and also verified it was acting as a switch for the extension cord by using my trusty sidekick Mr. DMM again. At this point I knew everything was working as expected so I finished wiring the extension cord together and wrapped it with electrical tape in a safe manor. This needs to be done before you ever plug the extension cord into the wall. Remember you are working with 120V of AC power which is dangerous if not handled properly. If you aren’t sure that you’re being safe then you should find one of the many other websites or books dealing with AC wiring safety. Sorry for sounding like your mother, but I want to make it clear that if you mess this up and shock yourself or burn down your house it’s not my fault. At this point you have a magical extension cord that can power things on and off simply by applying voltage to the relay. Ok maybe it’s not so magical. Apply voltage to the cable seems to be more work than just plugging it in, but trust me in a little while it will be awesome! Test it with your light bulb and a power source to make sure everything is working.

The last step and the one that makes this project useful is getting the microcontroller to control this relay. To do that you can use the following circuit. Most motors and relays shouldn’t be connected directly to a microcontroller because they are inductive and require more current that a microcontroller can safely supply. If you are using a low current 5 volt relay you may be able avoid this circuit (you’d still need to clamp the relay with a diode), but using this circuit should work fine with these small relays so if in doubt use this circuit.

In this circuit the transistor acts as a switch and it allows you to turn on the relay. This circuit works for relays using 5, 9, or 12 volts (the common trigger voltages for relays). I picked a 5 volt relay because that lets me use the Arduino board’s 5V volt output thus eliminating the need for another power source. You also need to protect the microprocessor from back EMF current and that is what the diode is doing.

The cost to build the circuit below should be under $15 dollars and half of that is for a cheap extension cord. RadioShack or online electronic stores will have all the other components.

The code to run this was amazingly simple since the transistor and relay make turning on the extension cord as simple as turning on or off an LED. Here is a program that lets you toggle on and off the relay with the space bar.

// Maurice Ribble // 4-6-2008 // http://www.glacialwanderer.com/hobbyrobotics // This code just lets you turn a digital out pin on and off. That's // all that is needed to verify a relay curcuit is working. // Press the space bar to toggle the relay on and off. #define RELAY_PIN 3 void setup() { pinMode(RELAY_PIN, OUTPUT); Serial.begin(9600); // open serial Serial.println("Press the spacebar to toggle relay on/off"); } void loop() { static int relayVal = 0; int cmd; while (Serial.available() > 0) { cmd = Serial.read(); switch (cmd) { case ' ': { relayVal ^= 1; // xor current value with 1 (causes value to toggle) if (relayVal) Serial.println("Relay on"); else Serial.println("Relay off"); break; } default: { Serial.println("Press the spacebar to toggle relay on/off"); } } if (relayVal) digitalWrite(RELAY_PIN, HIGH); else digitalWrite(RELAY_PIN, LOW); } }

Credits

I’m not sure who designed this circuit diagram, but I found it referenced on the Arduino website and it was very useful while designing my relay circuit.

I would like thank Oracle, spiffed and the others who helped me make this a better and safer article.