Home-made radar presence sensors: an LD2412 on an ESP32-C3

A 24 GHz radar module and a tiny ESP32-C3 in a printed case tell Home Assistant someone is in the room, even sitting still. What worked, and what ghosted.

Difficulty
Moderate
Parts cost
One LD2412 radar and one ESP32-C3 SuperMini per room, plus a printed case
Build time
A case print, a flash and four wires per unit, then a few nights of watching it
Skills
ESPHome, 3D printing, Home Assistant

The problem

I wanted the living-area lights to follow people. On when someone’s in there after dark, dimming through the evening, off a few minutes after everyone leaves. The catch is the sitting still part. Someone on the couch watching TV barely moves, and the lights mustn’t go off on them.

So I built my own presence sensors. Each one is an HLK-LD2412 radar module (a 24 GHz mmWave radar, the sort that picks up a person breathing, not just walking) wired to an ESP32-C3 SuperMini running ESPHome, in a case I designed and printed. The first one went on the lounge wall in July, aimed at the couch. More followed for the dining area, the hallway by the kids’ rooms, the entry door and the bedrooms.

This isn’t a clean success story. Several of them ghosted (said someone was there when nobody was), half of them were swapped out for bought Zigbee ceiling radars, and then some of them came back. Read the whole thing before you build six.

How it works

How a home-made radar presence sensor works The LD2412 radar sees moving and still people in distance bands 0.75 m deep. It sends what it sees over serial to an ESP32-C3 running ESPHome, which passes presence, distance and energy to Home Assistant over Wi-Fi and lets you set the max gate, min gate and hold time. Home Assistant puts each room's sensors in a group, and the lights watch the group, turning off only after the area has been empty for a few minutes. LD2412 radar (24 GHz) sees people moving or sitting still in gates 0.75 m deep serial, 115200 ESP32-C3 + ESPHome the built-in ld2412 component Reports • present / moving / still • distance and energy (throttled) Knobs, set from Home Assistant • max gate: caps the range • min gate and hold time Wi-Fi Home Assistant one group per area: on if any sensor in it sees someone Lights watch the group, never one sensor Off only after the area has been empty a few minutes.

The radar does the hard part. It reports whether it sees a moving target, a still target, or both, how far away each one is, and how strong the reflection is (its energy). It talks to the ESP32 over a plain serial link at 115200 baud. ESPHome has a built-in ld2412 component that understands it, so there’s no custom code. The ESP passes it all to Home Assistant over Wi-Fi.

The radar splits what it sees into gates: distance bands 0.75 m deep. You set a max gate (nothing past it counts) and a min gate (nothing closer counts), plus a hold time: how long it keeps saying “present” after it last saw someone. All three show up in Home Assistant as numbers you can change without reflashing.

In Home Assistant, the automations never look at a single sensor. Each area has a group helper that’s on if any sensor in it sees someone, and the lights only ever watch the group. That turned out to matter a lot later, when sensors started getting swapped.

Where they’re mounted and what they drive

  • Lounge: on the wall, aimed at the couch. Max gate 6 (4.5 m), min gate 1, hold 10 seconds.
  • Dining: one unit covering the dining area and kitchen, max gate 10.
  • The hallway by the kids’ rooms: max gate 8. That hallway has no natural light, so its light runs on presence day and night: full brightness in the day, 50% and warm from 10 pm to 4 am, off 2 minutes after it clears.
  • The entry door: aimed at the door, for the welcome lights.
  • The bedrooms: one in each of the kids’ rooms, and one built into a combined button-and-light box in the main bedroom.

The living-area lights run off the lounge and dining group. When it’s occupied and the sun is low, the lights come on at 100% and 4000 K until 8 pm, then step down every 10 minutes to 30% and a warm 2200 K by 10:30 pm. They go off when the whole area has been empty for 3 minutes. If someone switches the lights on or off by hand, the automation backs off until the room empties. Some of the radars also feed the house alarm.

What happened next

In late September I had a run of false presence. The worst was a bedroom unit that read “present” non-stop with nobody in the room. I think most of it came from my own case (see the lessons). In the meantime I’d bought some battery Zigbee ceiling radars, and on 1 October the lounge, dining and hallway units came down and those went up instead. Then the entry and one bedroom. The main bedroom radar got switched off in Home Assistant, leaving the rest of that box working.

The bought ones weren’t perfect either. Out of the box they ghosted and dropped people too, until I opened one up and tuned the radar inside through a hidden serial port. That’s its own guide: the hidden serial port in a cheap Zigbee ceiling radar.

So the home-made units came back, as a second sensor in the same rooms. Now the lounge, dining, entry and hallway each have one bought radar and one home-made one, joined in a group per room. Having two in the same place is how I caught the hallway’s bought radar lying: over one day it fired 173 times and the home-made one in the same hallway agreed with 66. Overnight it fired 22 times and the home-made one agreed with none. The home-made one runs the hallway light again now.

It cuts both ways. In the dining area, the tuned Zigbee radar held a person sitting still for 6 minutes 19 seconds over a stretch where the home-made one dropped them for 2 minutes 50 seconds.

The next version: two radars on the ceiling

The plan now (not built yet) is a ceiling unit with two radars on one ESP32-C3: the LD2412 for still people, plus an HLK-LD2450, which tracks the position of up to three moving targets.

  • The C3 has two hardware serial ports. Putting ESPHome’s logger on the USB port leaves both free: the LD2412 stays on GPIO3/GPIO4 at 115200, the LD2450 goes on GPIO20 (RX) and GPIO21 (TX) at 256000. GPIO2, 8 and 9 are off limits (they set the boot mode or drive the LED).
  • Both radars on 5 V, a few centimetres apart, facing the same way. They’re both 24 GHz, so the first unit gets watched for the two interfering with each other. The LD2450’s Bluetooth gets turned off.
  • Power: a 19–20 V laptop charger on a power point in the ceiling at one end of the house, through a fuse block, down solid-copper Cat5e or Cat6 (not copper-clad aluminium) to a small buck converter at each sensor, set to 5.0–5.1 V before anything is connected, with a 220–470 µF capacitor across the 5 V. Over a 40 m run I worked out 0.4–0.9 V of drop, which is fine. Keyed plugs so nothing goes in backwards. The buck has to take 19 V comfortably: an MP1584 or LM2596, not a Mini360 (23 V max).
  • Never plug USB into a SuperMini while it’s on that supply. Its 5V pin is wired to the USB power.
  • What I ruled out: PoE (the ESP32 boards with it were over A$100 each), long USB-C runs (too much drop at 5 V), and the steel roof trusses as the return wire (flaky joints, corrosion, noise, and not to wiring rules).

One lounge unit first, then a week of comparing it with the others.

What you need

  • An HLK-LD2412 radar module. Mine came as kit boards with header pins and dupont plugs fitted.
  • An ESP32-C3 SuperMini. Check what you’ve actually got (step 1). One I was sure was an S3 turned out to be a C3.
  • Four dupont jumper wires, if your boards have pins like mine.
  • A USB-C cable and a 5 V USB power supply. The radar runs off the ESP’s 5 V pin.
  • A 3D printer. My cases are PLA. I haven’t published the case files yet, so for now the case section below gives you the rules and the mistakes to design around.
  • A multimeter, to check each radar before you power it (see the lessons).
  • Home Assistant with the ESPHome integration, and ESPHome on your computer to do the first flash.

The case

The radar’s own manual is specific about what goes in front of it: about 1 mm of even, solid plastic, a 6 mm air gap between the window and the radar, and no metal. Print the body face-down. It needs no supports.

It took three goes:

  • Version 1 (30 × 50 × 19 mm) didn’t fit. I’d drawn it for bare boards, and the kit boards have header pins and dupont plugs sticking out about 16.5 mm behind each board. There was also no room for the USB plug to bend.
  • Version 2 (30 × 62 × 32 mm) has the boards side by side with the pins pointing back, a tilt-in mount for the ESP, and a slot in the bottom for the USB cable. It fitted. But the only thing holding the radar off the window was a 0.8 mm shelf that the board overlapped by about 0.5 mm, so the radar fell through and sat hard against the plastic. That’s my best explanation for the ghosting.
  • Version 3 has four stand-off blocks under the radar’s long edges to hold the air gap, snap hooks to keep it there, and rails under the ESP’s pin rows. The lid has a printed spring that presses the ESP down. The body printed perfectly first time. The first spring was too weak and twisted, so the redesigned lid has two stacked diamond-shaped springs that push the pad straight down instead of tipping it. No glue on the boards. Glue on the lid is fine.

Wiring

Four wires, the same on every unit:

LD2412 ESP32-C3 SuperMini
5V 5V (not 3V3: the radar needs 5 V)
GND GND
TX GPIO4 (ESPHome rx_pin)
RX GPIO3 (ESPHome tx_pin)

TX and RX cross over: the radar’s transmit goes to the ESP’s receive. The names in the YAML are from the ESP’s point of view. This is the bit that gets wired wrong.

Firmware and config

This is the core of my config, with the names made generic. I’ve rebuilt it here from my notes rather than pasting the live file, so run esphome config room-presence.yaml on it before you flash. Every unit uses the same file. For a new room, copy it and change only the substitutions at the top.

substitutions:
  name: room-presence
  friendly_name: Room Presence

esphome:
  name: ${name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32-c3-devkitm-1
  variant: esp32c3

# Logs go out the USB port, which leaves the C3's hardware serial ports free for radars.
logger:
  hardware_uart: USB_SERIAL_JTAG

api:
  encryption:
    key: !secret api_encryption_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Room Presence Fallback"   # 32 characters at most
    password: !secret fallback_password

uart:
  id: radar_uart
  rx_pin: GPIO4    # from the radar's TX
  tx_pin: GPIO3    # to the radar's RX
  baud_rate: 115200
  parity: NONE
  stop_bits: 1

ld2412:
  id: radar
  uart_id: radar_uart

binary_sensor:
  - platform: ld2412
    has_target:
      name: Presence
    has_moving_target:
      name: Moving
    has_still_target:
      name: Still

# Distance and energy change constantly. These filters stop them flooding Home Assistant.
sensor:
  - platform: ld2412
    moving_distance:
      name: Moving distance
      filters:
        - throttle: 2s
        - delta: 5
    still_distance:
      name: Still distance
      filters:
        - throttle: 2s
        - delta: 5
    moving_energy:
      name: Moving energy
      filters:
        - throttle: 2s
        - delta: 5
    still_energy:
      name: Still energy
      filters:
        - throttle: 2s
        - delta: 5

number:
  - platform: ld2412
    timeout:
      name: Hold time
    max_distance_gate:
      name: Max distance gate
    min_distance_gate:
      name: Min distance gate

select:
  - platform: ld2412
    distance_resolution:
      name: Distance resolution

switch:
  - platform: ld2412
    engineering_mode:
      name: Engineering mode

text_sensor:
  - platform: ld2412
    version:
      name: Radar firmware

The Radar firmware sensor is the useful one for testing. It only fills in if the ESP can both send to and hear from the radar, so if it shows a version number, your TX and RX are right.

Tuning

Everything here is done from the device page in Home Assistant. No reflashing.

  • Max gate first. At 0.75 m a gate, the factory 13 reaches about 9.75 m, and the radar sees through walls. Count the gates to the far side of the room and stop there. My lounge runs 6 (4.5 m), the dining area 10, the hallway 8.
  • Min gate ignores the first 0.75 m or so. The lounge runs 1. If a unit reports presence with nobody there, raising the min gate from 0 to 1 is one thing to try.
  • Hold time is how long the radar keeps saying “present” after it last saw anyone. The lounge runs 10 seconds. The longer waits live in Home Assistant, where they’re easy to change: the living area waits 3 minutes after the whole area empties, the hallway 2 minutes.
  • Sensitivity. The radar also has a sensitivity threshold for every gate, separate for moving and still targets. I haven’t touched mine. As of mid-October every unit is still on the factory thresholds, and I’ve only just started reading them into Home Assistant to see how close an empty room gets to them. Get the gates and hold time right first.

Step by step

  1. Check the board. With only that board plugged in, run esptool.py chip_id. A C3 shows as single core. I usually have another project’s ESP32 plugged in as well, so I check I’m talking to the right one before I flash anything.
  2. Check the radar with a multimeter before you ever power it: measure across 5V and GND. Under about 100 Ω means it’s shorted. Then power it on its own and feel it for heat (see the lessons).
  3. Print the case: body face-down, no supports.
  4. Flash over USB: esphome run room-presence.yaml --device /dev/ttyACM0. After this, updates go over Wi-Fi.
  5. Unplug, wire the radar as in the table, and plug back in.
  6. Add it in Home Assistant. It should turn up on its own under Settings → Devices & services. Put it in the right area.
  7. Check Radar firmware shows a version. If it says unknown, restart the device (see the lessons).
  8. Set the max gate, min gate and hold time for the room.
  9. Make a group helper per area (a binary sensor group, on if any member is on) and point your automations at the group, not the sensor.
  10. Mount it and watch it for a few nights before it controls anything at night.

Testing it

  • Walk away from it and watch the moving distance climb, then sit still and watch the still distance and energy. If it still sees you after you’ve left the room, your max gate is too high.
  • Look at the history overnight. Count the times it said “present” when you know nobody was there. That’s your ghost count, and it’s the number to beat.
  • If you can, put a second sensor in the same room. A trigger the other one doesn’t agree with is a suspect. That comparison is what showed the hallway’s bought radar was faulty, and it’s how I’m judging the V3 case. Early days, but as of mid-October the units in the new case haven’t reported anyone in the stretches I know the room was empty.
  • When a light “doesn’t work”, read the automation’s trace and the logbook before touching the sensor. The logbook shows who or what changed the light.
  • Keep an eye on them dropping off Wi-Fi. Two of my home-made units dropped off the network in the same week. One turned out to be powered from a computer that had been shut down, so check the power before you blame the radio. Another played up while it was powered from the router’s USB port, and came off there.

Lessons learnt

The radar sees straight through walls. Out of the box the max gate is 13, about 9.75 m, so a sensor in one room happily reports people in the next. Cap the max gate to the room on every unit.

My case put the radar hard against the window, and it saw ghosts. The manual wants 1 mm of plastic and a 6 mm air gap. My V2 case held the radar on a 0.8 mm shelf it barely overlapped, so it fell through and sat on the plastic. The units started reporting people who weren’t there, and one bedroom unit read “present” non-stop. That was my design fault, not the radar’s. V3 holds the gap with stand-off blocks. If you design your own case, check the radar can’t slide forward.

Kit boards aren’t bare boards. My first case was drawn from bare board sizes. The kit boards have header pins and plugs sticking out the back, and nothing fitted. Measure the actual boards in your hand before you model anything.

Wire the radar after first boot and half the sensors stay unknown. Presence still worked, but the radar firmware and gate settings read “unknown”. The ESP only asks the radar for those when it starts up. Restart the device after wiring the radar.

The board I was sure was an S3 was a C3. Same size, same look. esptool.py chip_id settled it. Check the chip before you pick a board in your YAML, and check you’re flashing the board you think you are.

A unit I’d named “2410” had an LD2412 in it. The LD2410 is a close cousin with a different serial protocol. Set up as an LD2410, the log filled with “Max command length exceeded” at 256000 baud and “Invalid status” at 115200. Look at what’s printed on the radar, not what you called the device.

Two radars cooked on the bench. Building a replacement late one night, a brand-new LD2412 popped and smoked on the bench just after I’d flashed the ESP. The ESP survived. A second new radar spewed garbage over serial and got hot. I’d double-checked the wiring, so I suspect a bad batch, but I can’t rule out my own late-night wiring. Now each new radar gets a meter across 5V and GND before it sees power, and gets powered on its own first.

The fallback hotspot name has a 32-character limit. ESPHome makes a fallback Wi-Fi hotspot named after the device. A long device name plus “Fallback” was too long and the config failed. Keep it short.

The light that “wouldn’t come back on” was working fine. The automation had backed off because someone changed the lights by hand, and then I’d tapped “all lights off” myself. The trace and the logbook showed both. The sensor was never the problem.

When you swap a sensor, give the new one the old entity id. My radars were referenced in about ten places: automations, dashboards and more. Rename the old entity out of the way, give the new one the old id, and everything carries on without a single edit. That’s how the Zigbee radars went in, and why a group per room is easier still.

A group can hide a stuck sensor. The entry lights trigger when the entry group goes from clear to occupied. If one of the two sensors sticks on, the group can never go clear, so the lights can’t trigger again. Two sensors catch liars, but watch for one that never lets go.

Questions and comments

Loading comments…

    Your name and comment show publicly. To stop spam, a scrambled code made from your internet address is kept, never the address itself. Comments with links wait for a quick check before they show. Be decent: I'll remove anything that isn't.