Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsThe BH1750 reports light in lux, a real unit, which makes setting a threshold easy: about 20 lux is dim evening light. The ESP32 switches a relay on when the lux falls below the ON level and off above a higher OFF level, so passing clouds or the lamp's own light do not make it flicker. Place the sensor where the lamp's light does not reach it.
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.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
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.
dusk_switch.inoVerify & upload in Electrolark#include <Wire.h>
#include <BH1750.h>
BH1750 meter;
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
const float ON_LUX = 20, OFF_LUX = 60;
bool lampOn = false;
void setLamp(bool on) { lampOn = on; digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH); }
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
setLamp(false);
Wire.begin(21, 22);
meter.begin();
}
void loop() {
float lux = meter.readLightLevel();
Serial.println(lux);
if (!lampOn && lux < ON_LUX) setLamp(true);
else if (lampOn && lux > OFF_LUX) setLamp(false);
delay(5000);
}
The lamp's light reaches the sensor. Re-aim it or raise OFF_LUX.
Check I2C wiring and the sensor's address pin.
Use the Serial Monitor to read your own dusk lux value.
Around 10 to 50 lux, but it varies by place. Measure yours.
Yes, but a BH1750 gives calibrated lux and needs no tuning.
Yes, add a time check from the NTP clock project.
5 parts: ESP32-WROOM-32 38-Pin Development Board, Generic GY-302 BH1750 Light Intensity Module – I2C Interface, 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 (dusk_switch.ino).
About ₹651 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading lux from a BH1750 over I2C; Hysteresis to avoid flicker; Placing a light sensor sensibly.
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.
Ideas that reuse this build: ESP32 BH1750 lux meter OLED. 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.