Arduino boards are useful for controlling low-voltage electronics like LEDs, small servos, and sensors. But when you want to control something bigger—for example, a lamp or something you can plug in with an extension cord—these draw too much power for the Arduino to handle. Instead, you’ll need a relay. This guide will show you how to add a relay to your projects to control a desk lamp, plus a wide array of other high-voltage gadgets.
Relays are small modules that you can use to control high-voltage electronics. Since the higher voltage of devices like lamps can destroy Arduino boards, they need to be kept separate. Inside a relay, an electromagnet is controlled by a low-voltage wire (like the kind coming off an Arduino pin). When activated, the electromagnet closes a switch on the high-voltage wire. This keeps the high and low-voltage circuits isolated from each other.
On the high-voltage side of the relay, there are three connections. Normally Closed (NC) on one side, the Common Pin (COM) in the middle, and Normally Open (NO) on the other. In the diagram next to the relay, the line coming off COM will be pointing towards NC. NC is used for devices that will stay on but only turn off when you send a signal from the Arduino. NO is used when you want the device to stay off until you send a signal from the Arduino to turn it on. For our purposes, we’ll be using NO since we’re going to turn a lamp on and off, much like we would an LED.
On the low-voltage side of the relay, you will usually find a VCC and GND pin, as well as input pins for the number of relays on your module. They can be as few as a single relay, but some modules can have eight or more individual relays sharing a board.
Your relay module might also have a jumper on it. With this jumper connected, the electromagnet will be powered by the Arduino. However, if something goes wrong with the relay, then it’s possible to damage the Arduino. If you want to supply a separate power supply to power the relay module, remove this jumper. For our purposes, though, we’ll keep it on for a simple demonstration.
Warning: It is absolutely imperative that you use caution any time you’re working with high voltages. If handled incorrectly, high-voltage cables can cause injury or death. Never touch any exposed wires while electricity is flowing through them. For this guide, we’ll be cutting into an extension cord, so do not modify it while it’s plugged in. Only plug your project into a high-voltage outlet once everything else is done.
While we’ll use this project to control a lamp, what we’re really going to make is an Arduino-controlled extension cord, since these are cheap and easy to manipulate. Then, once everything is ready, you can plug a lamp into the extension cord. With that in mind, here’s what you’ll need:
Since the wiring is complicated and needs to be done safely, it’s a good idea to get the Arduino wiring out of the way first.
You can copy the code below into your IDE and upload it to your Arduino before connecting anything to the relay module (and while the extension cord is unplugged from the wall outlet).
In the setup section of this code, we’ll set up a single output pin. The loop section will alternately turn the relay on and off for ten seconds each. You can now build on this to add sensors or control the light using code. Try changing the duration the light is on or check out our guide on controlling a light with a motion sensor to turn on your lamp whenever there’s movement in the room.
If you want to change this code at all, make sure to unplug the extension cord before touching the Arduino to connect it to the USB cable in your computer. When you’re done with the project, always disconnect high-voltage cables first.
With that in mind, let’s move onto the wiring.
First, we’ll need to cut into the extension cord to connect it to the relay. Make sure your cord is disconnected from any power source before continuing. Most extension cords have two distinct cables that are stuck together. One, the ground wire, has ridges on the outside edge. The other is smooth on the outside. This one is the live wire, and it’s the one we’ll cut into.
Using a knife, slice in between the two wires of the cable along the length of the cord to separate them, then cut through just the live wire (with the smooth side) to expose the wiring within. Then, use the stripping tool to strip away a small amount of the insulation. Lightweight extension cords typically use 16 gauge wire, while heavy-duty cords can go up to 10 gauge. Make sure you’re using the right gauge on the stripping tool.
With the exposed ends visible, insert one end into the NO input on the relay and the other end into COM. In this configuration, no power will flow through the extension cord unless you send a signal from the Arduino to turn it on.
Once this part is done, we’re finished working with the extension cord for a little while, but do not connect it to power until you’re finished with the rest of these instructions.
Next, we’ll move onto the Arduino wiring. Using typical Arduino wires, run a wire from 5V on the Arduino to VCC on the relay module. Next, run a wire from GND on the Arduino to GND on the relay. Finally, connect a wire from pin 8 on the Arduino to input 1 (or whichever input corresponds to the relay you used) on the relay module.
Once that’s finished, connect a power source to your Arduino (this can be an independent power source or the USB cable you use to upload sketches), then connect the extension cord to a wall outlet. Once you do so, your lamp should turn on for ten seconds, then turn back off for ten seconds. If it doesn’t, make sure your lamp itself is turned on.
Again, when you’re finished, make sure to unplug the extension cord first before touching anything else. And never touch the exposed wires coming off of the extension cord while it’s plugged into the wall.