Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA PIR sensor detects the infrared movement of a person. The ESP32 switches a relay on at the first movement and keeps it on until no movement has been seen for the delay time (30 seconds here), restarting the countdown on every new movement. Using millis() instead of delay() lets the ESP32 keep watching the sensor the whole time. A fixed LED lamp or strip on a low-voltage supply is the safest first load.
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.
pir_light.inoVerify & upload in Electrolarkconst int PIR_PIN = 27, RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
const unsigned long OFF_DELAY_MS = 30000;
unsigned long lastMotion = 0;
bool lightOn = false;
void setRelay(bool on) { digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH); lightOn = on; }
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
lastMotion = millis();
if (!lightOn) setRelay(true);
}
if (lightOn && millis() - lastMotion > OFF_DELAY_MS) setRelay(false);
}
PIR sensors can false-trigger from heaters or sunlight. Move it and lower the sensitivity pot.
Raise OFF_DELAY_MS or the PIR's own hold-time potentiometer.
Flip ACTIVE_LOW.
Yes. Add an LDR or the BH1750 project's light check and require it to be dark.
Typically 3 to 7 metres in a cone.
delay() would stop the ESP32 from seeing new movement.
5 parts: ESP32-WROOM-32 38-Pin Development Board, HC-SR501 PIR Motion Detector Sensor Module, 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 (pir_light.ino).
About ₹592 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Using millis() for non-blocking timers; Restarting a timeout on each trigger; Switching a relay from a PIR.
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: Pico PIR intruder alarm. 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.