Projects / Auto-off when the room is empty

Auto-off when the room is empty

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32LD2410

    Wire the radar

    Power it from 5 V.

  2. ESP32LD2410

    Read presence in the serial monitor

    Wave and sit still.

  3. ESP32ON

    Add the relays

    With no mains connected yet.

  4. ESP32

    Test with a low-voltage lamp

    Battery and bulb.

  5. ESP32

    Shorten the delay to 30 s

    For testing only.

  6. ESP32

    Have the mains side wired

    By a qualified electrician.

  7. ESP32

    Set the delay back to 10 minutes

    And watch for a day.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LD2410 TXESP32 GPIO16 (RX1)Radar data to the ESP32
LD2410 RXESP32 GPIO17 (TX1)Commands to the radar
LD2410 VCC and GNDESP32 5V (VIN) and GNDPower (it needs 5 V)
Relay IN1ESP32 GPIO26Light relay
Relay IN2ESP32 GPIO27Fan relay
Relay VCC and GNDESP32 5V (VIN) and GNDCoil power
Light and fanThrough the relay contacts, by a qualified electricianMains 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.

CheckExpected
Walk inRelays switch on
Sit still for 5 minutesStay on
Leave for 10 minutesSwitch off
ReturnSwitch 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 →

Ask Lark AI

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.