Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsThis is the classic first ESP32 project: the board joins your Wi-Fi and serves a small web page with an on and off button. Tapping the button calls a URL on the ESP32, which switches a relay. No app, cloud account or extra library is needed, only the WebServer library that ships with the ESP32 Arduino core. Most relay modules switch on when their input pin goes LOW, so the sketch has a single setting to flip if yours works the other way round.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Never touch the relay terminals while the lamp circuit is plugged in.
Tick anything you already have and the total updates.
Switches the lamp or fan; has an optocoupler for safety
Follow the colours: red is power, dark grey is ground, blue is a signal.
Power the ESP32 from a USB phone charger. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
web_relay.inoVerify & upload in Electrolark#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_NAME = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true; // set false if your relay switches on with HIGH
WebServer server(80);
bool on = false;
void apply() { digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH); }
void page() {
String s = "<html><head><meta name='viewport' content='width=device-width,initial-scale=1'></head>";
s += "<body style='font-family:sans-serif;text-align:center'><h2>Lamp is ";
s += on ? "ON" : "OFF";
s += "</h2><p><a href='/on'><button style='font-size:28px'>ON</button></a> ";
s += "<a href='/off'><button style='font-size:28px'>OFF</button></a></p></body></html>";
server.send(200, "text/html", s);
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
apply();
WiFi.begin(WIFI_NAME, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { delay(300); Serial.print("."); }
Serial.println();
Serial.print("Open http://"); Serial.println(WiFi.localIP());
server.on("/", page);
server.on("/on", []() { on = true; apply(); server.sendHeader("Location", "/"); server.send(303); });
server.on("/off", []() { on = false; apply(); server.sendHeader("Location", "/"); server.send(303); });
server.begin();
}
void loop() { server.handleClient(); }
The phone and the ESP32 must be on the same 2.4 GHz network. Check the IP address in the Serial Monitor.
Flip ACTIVE_LOW at the top of the sketch.
The relay draws a burst of current. Use a better USB cable or charger, and keep the relay on VIN rather than 3V3.
Not with this version: it works on your home network only. Adding MQTT or a cloud service is the next step.
Anyone on your Wi-Fi can open the page. Keep it for a lamp or fan, not for a door lock.
Yes. Add one pin and one pair of URLs per relay, or use a 4-channel relay board.
4 parts: ESP32-WROOM-32 38-Pin Development Board, 5V Relay Module with Optocoupler, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 1 hour. The build on this page has 6 steps.
No. This page has the complete, tested code (web_relay.ino).
About ₹530 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Joining Wi-Fi with an ESP32; Serving a web page and handling button URLs; Driving a relay module safely.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first. Never touch the relay terminals while the lamp circuit is plugged in.
Ideas that reuse this build: ESP32 MQTT temperature dashboard. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.