How to Build a DIY Wi‑Fi Controlled Light Switch Using an ESP8266 for Under $15

Ever walked into a dark room and fumbled for the switch, only to wish you could just tap a phone? With cheap Wi‑Fi modules and a bit of solder, you can turn that wish into a reality for less than a coffee run. Here’s a step‑by‑step guide that I used in my own apartment, and it works just as well in a starter home or a rental.

What You’ll Need

ItemApprox. Cost
ESP8266 NodeMCU board$5
2‑channel relay module (5 V)$4
Small project box (plastic)$2
2‑core wire (22 AWG)$1
9 V battery or wall adapter$1
Misc: heat‑shrink, solder, tape$1

All of these parts can be found on popular online marketplaces or a local electronics store. The total stays comfortably under $15.

Quick note on the ESP8266

The ESP8266 is a tiny microcontroller with built‑in Wi‑Fi. Think of it as a brain that can talk to your phone, Alexa, or Home Assistant over your home network. The NodeMCU board version comes with a USB port, so you can program it just like an Arduino.

Wiring the Switch

  1. Prepare the relay – The relay acts like an electronic switch that can handle mains voltage safely. The 2‑channel board gives you room to control two lights later if you want.
  2. Connect the ESP8266 to the relay
    • Connect ESP8266 GPIO 5 (D1) to the relay’s IN1 pin.
    • Connect ESP8266 GPIO 4 (D2) to IN2 if you plan a second light.
    • Tie the relay’s VCC to the ESP’s 3.3 V pin and GND to GND.
      The relay module usually has a jumper for 5 V power; leave it set for 5 V because the relay coil needs that voltage.
  3. Wire the mains sideNever work on live wires. Turn off the breaker, strip the ends of the existing switch wiring, and connect them to the relay’s COM and NO (normally open) terminals. When the relay is energized, the circuit closes and the light turns on.
  4. Power the ESP – Plug the ESP into a 5 V wall adapter or a 9 V battery with a regulator. A small USB charger works fine and keeps the whole thing tidy.

Flashing the Firmware

I like to keep things simple with the Arduino IDE. If you haven’t used it before, just download it from arduino.cc, add the ESP8266 board URL (http://arduino.esp8266.com/stable/package_esp8266com_index.json) in the preferences, and install the “esp8266 by ESP8266 Community” package.

Sketch Overview

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

ESP8266WebServer server(80);

const int relayPin = D1; // GPIO5

void handleRoot() {
  String html = "<h1>Light Switch</h1>"
                "<p><a href=\"/on\"><button>ON</button></a> "
                "<a href=\"/off\"><button>OFF</button></a></p>";
  server.send(200, "text/html", html);
}

void handleOn() {
  digitalWrite(relayPin, LOW); // Relay active low
  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "");
}

void handleOff() {
  digitalWrite(relayPin, HIGH);
  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "");
}

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // start off
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  server.on("/", handleRoot);
  server.on("/on", handleOn);
  server.on("/off", handleOff);
  server.begin();
}

void loop() {
  server.handleClient();
}

A few things to note:

  • Relay active low – Most cheap relay boards turn on when the control pin is pulled low (0 V). That’s why digitalWrite(relayPin, LOW) turns the light on.
  • Web server – The ESP runs a tiny web server on port 80. Visiting its IP address shows two buttons. No fancy app needed.
  • Security – This is a local network project, so I keep it simple. If you expose it to the internet, add a password or use HTTPS.

Upload the sketch, watch the serial monitor for the IP address, and you’re ready to test.

Testing and Mounting

  1. Power up – Plug the ESP, turn the breaker back on, and check that the relay clicks when you hit the “ON” button in your browser.
  2. Safety check – Use a multimeter to verify there’s no voltage on the relay’s control pins when the ESP is idle.
  3. Mount – Slip the project box into the wall plate space, secure the wires, and snap the cover back on. The ESP can sit in the box or be tucked behind the plate; just keep it away from direct heat.

I mounted mine behind a kitchen light switch. The only thing I miss is the tactile click, but a quick tap on my phone feels just as satisfying.

Adding a Voice Assistant

If you already use Alexa or Google Home, you can add a simple integration with IFTTT:

  1. Create a new Webhooks applet.
  2. Set the URL to http://<ESP_IP>/on for “turn on” and .../off for “turn off”.
  3. Link the applet to an Alexa routine or a Google Assistant phrase like “Hey Google, turn on kitchen light”.

No extra hardware, just a couple of clicks in the IFTTT dashboard.

Troubleshooting Tips

  • ESP won’t connect to Wi‑Fi – Double‑check SSID/password spelling. Some routers hide the network; make sure it’s visible or use a static IP.
  • Relay never clicks – Verify the relay’s VCC is truly 5 V. Some modules need a separate 5 V supply; the ESP’s 3.3 V pin won’t cut it.
  • Light flickers – Loose mains connections are the usual culprit. Turn off the breaker and tighten the screw terminals.

Why This Project Rocks

  • Cost – Under $15, you get a smart switch that can last years. Compare that to a $60 commercial smart switch that often needs a hub.
  • Learning – You’ll get hands‑on with Wi‑Fi, basic web servers, and safe mains wiring. Those skills pay off in bigger home‑automation projects.
  • Flexibility – Add more relays, integrate sensors, or swap the web UI for MQTT if you move to Home Assistant later. The ESP8266 is a tiny Swiss army knife.

I built this for a hallway light that was far from a power outlet. The result? A sleek, silent switch that I control from my phone while lying in bed. If you’re a DIY hobbyist like me, the sense of turning a simple piece of code into a real‑world action is priceless.

Happy hacking, and may your lights always obey your commands.

Reactions