Auto-off when the room is empty
Build it step by step
0 of 7 doneWire the radar
Power it from 5 V.
Read presence in the serial monitor
Wave and sit still.
Add the relays
With no mains connected yet.
Test with a low-voltage lamp
Battery and bulb.
Shorten the delay to 30 s
For testing only.
Have the mains side wired
By a qualified electrician.
Set the delay back to 10 minutes
And watch for a day.
How do you build an auto-off when the room is empty?
Radar presence switches off lights and fans after a room has been empty for 10 minutes. You need ESP32 DevKit V1, LD2410 24 GHz human presence radar and Relay module 2-channel 5 V. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 3
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- InterfacesBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| LD2410 TX | ESP32 GPIO16 (RX1) | Radar data to the ESP32 |
| LD2410 RX | ESP32 GPIO17 (TX1) | Commands to the radar |
| LD2410 VCC and GND | ESP32 5V (VIN) and GND | Power (it needs 5 V) |
| Relay IN1 | ESP32 GPIO26 | Light relay |
| Relay IN2 | ESP32 GPIO27 | Fan relay |
| Relay VCC and GND | ESP32 5V (VIN) and GND | Coil power |
| Light and fan | Through the relay contacts, by a qualified electrician | Mains side |
Code
The LD2410 is a 24 GHz radar that senses a person even when they sit very still, which a PIR sensor often misses, so a reader or someone at a desk will not have the light go off on them. The sketch keeps the time of the last detection and switches the relays off only after ten minutes without one, then switches them on again as soon as someone returns. The mains side must be wired by a qualified electrician, in a proper enclosure, with the relay ratings and fuses checked against the light and fan; do not wire mains yourself unless you are qualified. Build and test the whole thing first with low-voltage lamps from a battery on the relay contacts. The radar sees through thin walls and doors, so aim it into the room and tune its range with the manufacturer's app if it picks up the corridor. It is for saving electricity, not for safety: never use it to switch anything that matters if it is turned off, such as a heater, an extractor over a gas stove, or medical equipment. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Room presence switch: relays on while someone is detected, off after 10 minutes of absence.
// Library: ld2410 (ncmreynolds). Board: ESP32.
#include <ld2410.h>
ld2410 radar;
const int RX_PIN = 16, TX_PIN = 17, RELAY_LIGHT = 26, RELAY_FAN = 27;
const bool RELAY_ON = LOW; // most relay boards switch on with a LOW input
const unsigned long EMPTY_MS = 10UL * 60UL * 1000UL;
unsigned long lastSeen = 0;
bool on = false;
void setRelays(bool state) {
on = state;
digitalWrite(RELAY_LIGHT, state ? RELAY_ON : !RELAY_ON);
digitalWrite(RELAY_FAN, state ? RELAY_ON : !RELAY_ON);
}
void setup() {
pinMode(RELAY_LIGHT, OUTPUT); pinMode(RELAY_FAN, OUTPUT);
setRelays(false);
Serial.begin(115200);
Serial1.begin(256000, SERIAL_8N1, RX_PIN, TX_PIN);
radar.begin(Serial1);
}
void loop() {
radar.read();
bool present = radar.isConnected() && radar.presenceDetected();
if (present) {
lastSeen = millis();
if (!on) setRelays(true);
} else if (on && millis() - lastSeen > EMPTY_MS) {
setRelays(false);
Serial.println("Room empty, switched off");
}
delay(100);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Walk in | Relays switch on |
| Sit still for 5 minutes | Stay on |
| Leave for 10 minutes | Switch off |
| Return | Switch on again |
If it does not work
Never detects
Check the TX and RX are crossed and the baud rate is 256000.
Stays on forever
The radar sees a fan or a person in the corridor; reduce its range.
Relays act backwards
Change RELAY_ON.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the auto-off when the room is empty?
ESP32 DevKit V1, LD2410 24 GHz human presence radar and Relay module 2-channel 5 V.
How long does the auto-off when the room is empty take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the auto-off when the room is empty works?
Walk in: Relays switch on. Sit still for 5 minutes: Stay on. Leave for 10 minutes: Switch off.
Never detects: what should I check?
Check the TX and RX are crossed and the baud rate is 256000.
Stays on forever: what should I check?
The radar sees a fan or a person in the corridor; reduce its range.
Relays act backwards: what should I check?
Change RELAY_ON.
