DIY Smart Lighting Control with Inexpensive ESP32 Modules
If you’ve ever stared at a blinking ceiling fan and thought, “There’s got to be a smarter way,” you’re not alone. With remote work turning every room into a home office, the demand for flexible, energy‑savvy lighting has exploded. The good news? You don’t need a $200 hub or a subscription plan to get there. A tiny ESP32 board and a dash of curiosity can turn any lamp into a programmable, voice‑ready light source.
Why DIY Smart Lighting Makes Sense Today
The pandemic taught us that the home is no longer just a place to crash; it’s a productivity hub, a gym, and a cinema all rolled into one. Traditional smart bulbs are convenient, but they’re pricey and often lock you into a single ecosystem. Building your own solution gives you three big wins:
- Cost control – An ESP32 module costs under $5, a fraction of a smart bulb’s price.
- Flexibility – You decide how the light behaves, from sunrise simulations to motion‑triggered dimming.
- Energy insight – By adding a current sensor you can see exactly how many watts you’re sipping each night.
Meet the ESP32: Your New Lighting Brain
The ESP32 is a low‑cost, Wi‑Fi‑enabled microcontroller from Espressif. Think of it as a tiny computer that can talk to your router, run code, and switch a relay on or off. It’s the same chip that powers many commercial smart plugs, but here you get to program it yourself.
- Wi‑Fi – Connects directly to your home network, no extra hub needed.
- GPIO pins – General‑purpose input/output pins that can drive relays or read sensors.
- Low power – Sleeps when idle, which helps keep your electricity bill low.
If you’ve never soldered before, don’t worry. The ESP32 development board comes with pre‑soldered pins, and you can use a breadboard for the first prototype.
What You’ll Need (And Why)
| Item | Reason |
|---|---|
| ESP32 dev board | Core controller, Wi‑Fi capable |
| 5 V relay module | Safely switches mains voltage |
| Power supply (5 V, 2 A) | Powers the ESP32 and relay |
| Light fixture (lamp or LED strip) | The load you’ll control |
| Jumper wires | Connect everything |
| Optional: Current sensor (ACS712) | Monitor power usage |
| Optional: Enclosure | Keep it tidy and safe |
All of these can be found on Amazon or local electronics shops for under $20 total.
Wiring the Circuit – Keep It Safe
Warning: Mains electricity is dangerous. If you’re not comfortable working with 120 V/230 V, ask a qualified electrician to help with the relay side.
- Power the ESP32 – Plug the 5 V supply into the board’s VIN and GND pins.
- Connect the relay – Wire the relay’s VCC and GND to the ESP32’s 5 V and GND. Then connect the relay’s IN pin to a free GPIO (e.g., GPIO 23). This pin will tell the relay when to close.
- Hook up the lamp – Cut one wire of the lamp’s power cord, strip the ends, and route them through the relay’s normally open (NO) contacts. When the relay is energized, the circuit completes and the lamp lights.
- Add a current sensor (optional) – Place the sensor in series with the lamp’s live wire and connect its output to another GPIO (e.g., GPIO 34). This lets your code read real‑time amperage.
Double‑check every connection, use heat‑shrink tubing, and keep the high‑voltage side separate from the low‑voltage ESP32 side.
Flashing Firmware – From Blank Slate to Smart Light
The ESP32 runs code written in Arduino C or MicroPython. For most hobbyists, the Arduino IDE is the easiest route.
- Install the ESP32 board package – In the IDE, go to File → Preferences and add
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.jsonto the Additional Boards Manager URLs. Then open Boards Manager and install “esp32 by Espressif Systems.” - Select the right board – Choose “ESP32 Dev Module.”
- Write a simple sketch – Below is a minimal example that connects to Wi‑Fi and toggles the relay via an HTTP endpoint.
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
WebServer server(80);
const int relayPin = 23;
void handleToggle() {
digitalWrite(relayPin, !digitalRead(relayPin));
server.send(200, "text/plain", "Toggled");
}
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // start off
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
server.on("/toggle", handleToggle);
server.begin();
}
void loop() {
server.handleClient();
}
- Upload – Connect the ESP32 via USB, hit the upload button, and watch the console for “Connected” messages.
Now you can point any browser (or Home Assistant) to http://<esp32_ip>/toggle and watch the lamp flick on or off.
Integrating with Home Assistant – One Click Automation
If you already run Home Assistant, adding the ESP32 is a breeze. Use the “RESTful Switch” integration:
switch:
- platform: rest
resource: http://<esp32_ip>/toggle
name: "DIY Lamp"
method: GET
is_on_template: "{{ false }}"
Because the ESP32 only toggles, you’ll need a small template to keep state in sync, but the result is a fully fledged entity you can control from the dashboard, Alexa, or Google Assistant.
Fine‑Tuning for Energy Savings
Now that the light is online, you can start playing with schedules:
- Sunrise simulation – Gradually increase brightness (if you add a PWM‑controlled LED strip) over 30 minutes before your alarm.
- Motion‑based dimming – Pair a cheap PIR sensor with another GPIO; when the room is empty, dim to 10 % or turn off completely.
- Power alerts – Use the current sensor to trigger a notification if the lamp draws more than expected, indicating a possible bulb failure.
All of these automations live in Home Assistant’s automation editor, so you don’t need to re‑flash the ESP32 for each tweak.
The Bottom Line: Empowerment Over Convenience
Sure, buying a $30 smart bulb is easier than soldering a relay. But the DIY route gives you ownership of the hardware, the software, and the data. You learn how Wi‑Fi, relays, and APIs work together, and you end up with a solution that can be expanded to control fans, heaters, or even garden irrigation.
My first ESP32 lamp was a simple bedside reading light. After a week of trial, I added a motion sensor and a sunrise routine. The result? I’m up 15 % less on my electricity bill for that room, and I’ve got a conversation starter for every houseguest who asks, “Is that a smart plug?”
If you’re looking for a weekend project that actually saves you money and gives you bragging rights, grab an ESP32 and start wiring. The learning curve is gentle, the community is massive, and the payoff is a home that feels a little more like the future you imagined.
- → DIY Seasonal Décor: How to Automate Halloween Lighting with Affordable Smart Plugs @brightseasons
- → Secure Over-The-Air Firmware Updates for ESP32 IoT Devices @microchipchronicles
- → Boost Energy Efficiency with a DIY Home Automation Upgrade: Wiring Your First Smart Switch @brightswitchdiy
- → Smart Light Switch Installation Guide: A DIY Step‑by‑Step Walkthrough for Homeowners @brightswitchdiy
- → How to Upgrade Your Home Lighting with Smart Switches Without Hiring an Electrician @switchsavvy