Step‑by‑Step Guide: Building a Low‑Cost Relay‑Controlled Power Switch for Arduino

If you’ve ever wanted to turn a lamp on and off with a line of code, you’re not alone. The idea of a “smart” switch that you can control from your desk, your phone, or even a voice assistant has gone from sci‑fi fantasy to kitchen‑counter reality. The cheapest way to get there is with a humble relay and an Arduino. In this post I’ll walk you through every part of the build, from picking parts to wiring the final prototype. By the end you’ll have a working power switch that you can tinker with, expand, or simply brag about to your friends.

Why a Relay?

A relay is basically an electrically operated switch. When a small current flows through the coil, a magnetic field pulls a set of contacts together, allowing a much larger current to flow on the other side. Think of it as a tiny, reliable “on‑off” lever that you can command with a microcontroller. Relays are great because they can handle high voltages (120 V AC or more) while keeping the control side low‑voltage and safe for the Arduino.

Types of Relays

  • Electromechanical (EMR) – The classic “clicky” relay. It has a coil, a set of contacts, and a physical moving armature. Cheap, easy to find, and perfect for hobby projects.
  • Solid‑state (SSR) – No moving parts, just semiconductor switches. Faster and quieter, but usually pricier and need a heat sink for high loads.

For a low‑cost DIY switch we’ll stick with an EMR. A 5 V coil relay rated for at least 10 A at 120 V AC is more than enough for a lamp or a small fan.

Parts List

ItemTypical Part #Approx. Cost
Arduino Uno (or Nano)A000066$10
5 V 1‑Channel Relay ModuleSRD‑05VDC‑SL-C$3
Breadboard & Jumper Wires$5
2.1 mm Power Jack (optional)$2
Enclosure (plastic project box)$4
Lamp or any 120 V AC load
1 kΩ resistor (for base protection)$0.10
Diode 1N4007 (flyback protection)$0.05

Total: under $30, often less if you already have some of the bits.

Safety First

Working with mains voltage is not a joke. Even a brief slip can cause a nasty shock or start a fire. Here are my quick safety rules:

  1. Unplug everything before you touch the AC side.
  2. Use a properly rated enclosure to keep wires away from fingers.
  3. Double‑check the relay’s voltage and current rating matches your load.
  4. Add a flyback diode across the relay coil (the 1N4007) to protect the Arduino from voltage spikes when the coil turns off.
  5. If you’re unsure, ask a qualified electrician to look over your wiring.

Wiring the Circuit

Below is a simple schematic described in words. Grab a piece of paper and sketch it as you read – it helps the brain.

  1. Arduino Power – Connect the Arduino’s 5 V pin to the VCC rail on the breadboard. Ground (GND) goes to the GND rail.

  2. Relay Module Power – The relay module also needs 5 V. Connect its VCC pin to the same 5 V rail, and its GND pin to the ground rail.

  3. Control Pin – Choose a digital pin on the Arduino, say D7. Connect D7 to the IN pin on the relay module through a 1 kΩ resistor. The resistor limits current into the module’s input transistor.

  4. Flyback Diode – Solder the 1N4007 across the relay coil terminals on the module (if the module doesn’t already have one). The stripe (cathode) goes to the + side, the other end to the – side.

  5. AC Side – This is where the mains power meets the relay contacts.

    • Cut the hot (black) wire of a short extension cord.
    • Strip the ends and connect one side to the Common (COM) terminal of the relay.
    • Connect the other side to the Normally Open (NO) terminal. When the relay is off, the circuit stays open; when it’s on, COM and NO close, letting current flow.
    • The neutral (white) wire of the extension cord stays untouched and goes straight to the lamp’s neutral.
    • Ground (green) can be left connected to the lamp’s ground if it has one; most simple lamps don’t need it.
  6. Enclosure – Mount the relay module and Arduino inside the project box. Drill a small hole for the power jack (if you use one) and another for the lamp’s plug. Keep the AC wires insulated and away from the Arduino’s 5 V side.

Programming the Arduino

Now that the hardware is ready, let’s give it some brain. The code is straightforward: set the control pin as output, then toggle it HIGH to close the relay, LOW to open it. Here’s a minimal sketch:

const int relayPin = 7;   // Pin connected to relay IN through resistor

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Start with relay off
}

void loop() {
  // Turn lamp on for 5 seconds
  digitalWrite(relayPin, HIGH);
  delay(5000);

  // Turn lamp off for 5 seconds
  digitalWrite(relayPin, LOW);
  delay(5000);
}

Upload this to the Arduino, plug the extension cord into the relay’s COM/NO terminals, and watch the lamp blink on and off. If you want more control, replace the delay() calls with serial commands, a button, or even a Wi‑Fi module like the ESP‑01.

Testing and Troubleshooting

  1. Check the LED – Most relay modules have a small LED that lights when the coil is energized. If it never lights, verify the Arduino pin is outputting 5 V (use a multimeter) and that the resistor isn’t open.
  2. Listen for the click – When the coil energizes you should hear a faint click. No click? The coil may not be getting enough voltage; double‑check the 5 V rail and the diode orientation.
  3. Measure the AC side – With the power off, use a multimeter set to continuity to ensure COM and NO are isolated. Then power on and verify they close when the Arduino drives the pin HIGH.
  4. Heat – After a few minutes of continuous operation, feel the relay’s case. If it’s hot to the touch, you may be exceeding its current rating. Either reduce the load or upgrade to a higher‑amp relay.

Going Further

Now that you have a basic switch, the sky’s the limit:

  • Add a MOSFET driver to reduce the Arduino’s current draw and allow multiple relays on one pin.
  • Integrate a web server on an ESP8266 so you can toggle the switch from a phone browser.
  • Use a physical button inside the enclosure for manual control, and add debouncing code.
  • Combine with a sensor (like a motion detector) to automate lighting.

Every addition teaches a new piece of the electronics puzzle, and that’s why I love tinkering. The Relay Chronicles blog is full of similar projects, so feel free to explore other relay‑based ideas when you’re ready.

Final Thoughts

Building a relay‑controlled power switch is a perfect entry point for anyone who wants to bridge the gap between low‑voltage microcontrollers and the high‑voltage world of household appliances. The parts are cheap, the circuit is simple, and the result feels like a tiny piece of the future in your hands. Just remember the safety rules, double‑check your wiring, and have fun watching a line of code bring a lamp to life.

Reactions