What you will build
A hallway or bedside light that fades on when it is dark and someone walks past, stays on while there is movement, and fades off 60 seconds after the last movement. In daylight it stays off.
How it works
Two sensors feed one decision. A light-dependent resistor (LDR) in a voltage divider gives a reading on GPIO 35: lower light, lower number. A PIR motion sensor outputs HIGH on GPIO 13 when it sees body heat move. If it is dark and motion is detected, the ESP32 fades the strip up with PWM through a MOSFET module, which switches the strip's own 5 V supply. The ESP32 pin only controls the MOSFET; it never powers the strip.
Parts
| Part | Qty | Purpose | Required |
|---|---|---|---|
| ESP32-DevKitC V4 + breadboard + USB cable | 1 | Controller | Yes |
| AM312 mini PIR sensor (3.3 V) | 1 | Motion | Yes |
| LDR (GL5528) + 10 kΩ resistor | 1 + 1 | Darkness | Yes |
| Logic-level MOSFET module (e.g. AO3400 or IRLZ44N board) | 1 | Switches the strip | Yes |
| 5 V single-colour LED strip, ≤ 50 cm | 1 | The light | Yes |
| 5 V 2 A USB power supply + USB-to-terminal adapter | 1 | Powers the strip | Yes |
Build steps
- PIR: VCC → 3V3, GND → GND, OUT → GPIO 13.
- LDR divider: 3V3 → LDR → GPIO 35 → 10 kΩ → GND.
- MOSFET module: signal/IN → GPIO 26, module GND → ESP32 GND.
- Strip power: 5 V supply + → strip +; strip – → MOSFET module output (–); 5 V supply – → MOSFET module GND. The ESP32 and strip supply must share GND.
- Upload the code and open the Serial Monitor at 115200 baud. It prints the light reading every second.
- Note the reading with the room lit and with your hand covering the LDR. Set
DARK_THRESHOLDbetween the two. - Checkpoint: cover the LDR, wave at the PIR → strip fades on. Stay still for 60 s → strip fades off.
Try it in the browser
Playground Simulation. A light slider sets the virtual LDR and a "walk past" button triggers the virtual PIR. The virtual strip shows brightness and a countdown shows time left before switch-off.
Open Simulator PlaygroundCode
Download automatic-night-light.ino
// Automatic Night Light — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x
const int PIR_PIN = 13;
const int LDR_PIN = 35; // ADC1
const int STRIP_PIN = 26; // to MOSFET module IN
const int DARK_THRESHOLD = 1200; // calibrate: below this = dark
const unsigned long HOLD_MS = 60000;
int brightness = 0; // 0..255
unsigned long lastMotion = 0;
unsigned long lastPrint = 0;
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
ledcAttach(STRIP_PIN, 1000, 8);
}
void loop() {
int light = analogRead(LDR_PIN);
bool dark = light < DARK_THRESHOLD;
bool motion = digitalRead(PIR_PIN) == HIGH;
if (motion && (dark || brightness > 0)) lastMotion = millis();
bool wantOn = (lastMotion != 0) && (millis() - lastMotion < HOLD_MS);
// fade towards target
if (wantOn && brightness < 255) brightness++;
if (!wantOn && brightness > 0) brightness--;
ledcWrite(STRIP_PIN, brightness);
if (millis() - lastPrint > 1000) {
Serial.printf("light=%d dark=%d motion=%d strip=%d\n", light, dark, motion, brightness);
lastPrint = millis();
}
delay(4); // ~1 s full fade
}
Troubleshooting
| Symptom | Likely cause | Check |
|---|---|---|
| Strip on all the time | PIR warming up | PIR sensors need 30–60 s after power-up |
| Strip never turns on | Threshold wrong | Read the Serial values and re-set DARK_THRESHOLD |
| Turns on, then off immediately at night | The strip lights the LDR | Point the LDR away from the strip; the code keeps the light on while it is already lit |
| Strip flickers or ESP32 resets | No common ground / weak supply | Join GNDs; use a 2 A supply |
Safety and limitations
Use a 5 V strip and a 5 V supply only; no mains wiring. Keep strip length ≤ 50 cm on this supply. Not a security system: PIR sensors miss slow or very distant movement.
Checklist
Next project
Smart Room Climate Monitor: read real measurements and see them on your phone.

