Build an ESP32 Presence Sensor Using an HLK-LD2420 mmWave

User Avatar Anand D
Author
Share
ESP32 Presence Sensor Using an HLK-LD2420

In this DIY ESP32 presence sensor project, we are going to use Hi-Link’s HLK-LD2420 Presence Sensor to detect human presence and do some actions with an ESP32. This ESP32 presence sensor is able to detect normal motions that a PIR sensor can capture, as well as very fine motions like breathing, small finger movements, etc., that a normal motion detection sensor cannot detect. That makes it a better ESP32 human presence sensor for desks, bedrooms and smart-home automations. If you want to check out a PIR based motion detection project, do check out our Motion detection and alert system using PIR sensor and ESP32 CAM tutorial. Now, we are going to see how to properly configure the HLK-LD2420 sensor using the software tool provided by the manufacturer and how to integrate it with an ESP32 as well. We use the ESP32 because the sensor can be configured to detect motion in different gates/detection zones, and we can have multiple actions for each of these zone-wise motions. This can be done with multiple GPIO pins of a microcontroller. An ESP32 presence sensor built with a mmWave radar can tell whether someone is in a room even when they are sitting still, something a normal PIR sensor cannot do.  Talking about ESP32, here is our wide range of ESP32-based projects that you can refer to for your next ESP32-based project.

What is an ESP32 Human Presence Sensor?

An ESP32 human presence sensor is a system where the ESP32 interprets signals from the sensors which detect when a person is present, not merely moving, and carries out some action, such as triggering the relay, illuminating an LED light, etc. The mmWave radar sensor is ideal for such purposes because it can detect minute movement as well as measure distance.

About HLK-LD2420 Presence Sensor

Let's learn a little bit about the HLK-LD2420 presence sensor.

HLK-LD2420 24 GHz mmWave presence sensor PCB showing the S3KM1110 radar chip and patch antennas

HLK-LD2420 is a 24 GHz millimetre-wave radar sensor built on a 20mm x 20mm compact PCB. 
On the PCB Top Layer, we can see an IC labelled as S3KM1110, which is the main SoC that handles transmitting and receiving the 24 GHz microwave signals through the PCB patch antennas. We can see a secondary microcontroller on the PCB bottom layer, identified by the part number PUYAF003F17. It runs Hi-Link’s firmware and handles the logic of controlling the output pin and UART communication. It's quite interesting that motion can be detected using some onboard PCB patch antennas. We can see such antennas on ESP32 boards, and we have already covered a tutorial in which we used the same to detect motion. You can check it out in our Build an ESP32 WiFi Sensing System for Wireless Motion Detection tutorial. One another way of motion detection is using the ESP32 CAM Module and comparing the frames for motions. If interested, here is our ESP32 CAM No Parking Alert System that teaches the same.

On the PCB Top, we can see two 24 GHz patch antenna elements that transmit and receive the returning reflections. It comes with 5 pins: 3V3, GND, RX, OT1, and OT2. In current standard firmware versions, OT1 is used as the standard TX pin, and OT2 is a digital output pin that turns HIGH when presence/motion is detected and LOW when no presence/motion is detected. Even though the sensor can detect through plastics, wood, etc., we need to keep it away from any metallic shields.

How to Configure the HLK-LD2420 Presence Sensor

Let's go ahead and configure the HLK-LD2420 sensor. First of all, we need a USB-TTL Converter. Make the connections as shown below. Make sure to connect the TX on the sensor to the RX of the USB-TTL Converter and the RX of the sensor to the TX of the USB-TTL Converter. If you'd like to know about the various USB-TTL converters and how to use them, do check out our How to Program ESP32 CAM tutorial, where we talk about the same. Once the above part is done, let's configure the sensor.

Connecting the HLK-LD2420 presence sensor to a USB-TTL converter for configuration

Unlike any other motion detection sensors, this one uses a software tool provided by the manufacturer to do the configuration and set the values. Go to the GitHub link provided below, download the HLK-LD2420_TOOL -English.zip file and install it on your PC. Once it's installed, open it, and it takes you to a window as shown below. Now, let's see what all these mean to us and check the most important parts of this window.

HLK-LD2420 configuration tool showing default max range gate, absence report delay and thresholds

The first thing to notice is the bottom-left part, where it shows the port number assigned to the USB-TTL converter. Keep the baud rate at 115200, as that's the one for the standard firmware. 256000 is the baud rate for older firmware, so we don't want to use that. Once these values are set, click the connect button, and it shows the device connected on the screen. Now, let's see the 4 different navigation tabs available right above this connection control. We are going to explore two of the most important and used tabs: the View/Set Parameter tab, which is shown right above and the Realtime Data tab, which we are going to discuss below. The View/Set Parameter tab allows us to set the detection parameters. The Max Range Gate helps us set the max detection range or the boundary.

Gate in HLK-LD2420 Sensor

Gate here refers to the distance /range slices that this sensor has. In simple words, they are some concentric distance zones that stretch outward from the sensor. Each gate is 0.70m or 70cm of distance. So, if we keep the Max Range Gate value at 3, it means we have 4 gates from 0 to 3 and each of them covers 0.70m and in total, the range is 2.8m. That's how the whole calculation is done. It has a total of 16 gates from 0 to 15, meaning the detection range is from 0.70m to 11.2m. The next parameter that we are going to see is the Absence Report Delay, which tells the sensor how many seconds it should be in the ‘ON’ state, right after the target exits the detection area or the motion stops, before automatically switching to the ‘OFF’ state. (Here, ON means motion is detected and OFF means no motion is detected) By default, its value is set to 30s; setting it to 5s increases the responsiveness of the sensor.

Trigger Threshold is the minimal radar signal energy required at a specific gate to detect motion. We can adjust the value for each gate from 00 to 15, meaning each gate can have different trigger thresholds, which is a really interesting feature that this sensor provides.
Hold Threshold decides whether to keep the detections that were triggered by the trigger threshold even if the target makes weaker motions like breathing or typing, before the Absence Report Delay timer runs out to No Detection. Hold Threshold can also be set individually for each gate. After setting up these values, clicking the Set Sensor Config option saves the values to the sensor. Read Sensor Config reads the already set values from the sensor and updates them on the window. Load/Save Config File helps to roll back to/restore a known working configuration.

HLK-LD2420 Realtime Data tab showing energy values, thresholds and a rolling motion graph

In the Realtime Data window, we can see Energy Value, Trigger Threshold and Hold Threshold and how motions in the physical environment are captured by the sensor. It also shows a rolling graph to visualise every motion. The green-coloured circle at the top indicates no motion, and if some motion is detected, it turns red, and the distance will be displayed in the section next to it.

ESP32 Presence Sensor Project: Desk Lamp Controller

Now, let's try to build a simple ESP32-based presence sensor using this sensor. We will use a relay to switch a desk lamp on and off, as this demonstration will be done on a table. The sensor should detect the finest motions, like breathing, and keep the lamp on even if there is no practical work/motion happening, which means it should detect the presence. The moment I leave the desk, the lamp should turn off as well.

The sensor already comes with an output pin, but we are demonstrating this with an ESP32 is because, if we use a microcontroller, we can have multiple detection zones by utilising different gates of the sensor as mentioned above and utilise multiple GPIOs of the ESP32 to take multiple actions. Here, we are going to turn on and off a simple overhead lamp. Let's see how this works.

Components Required for ESP32 Presence Sensor

We are using just 3 components to make this project. A XIAO ESP32 S3 board to act as the main microcontroller, an HLK-LD2420 presence sensor to detect presence and any motion, and a relay to turn on and off an external lamp when presence is detected. All the components are to be mounted on a breadboard, as that is the best way to get things done without soldering. After all the wiring and mounting, the whole setup looks like something in the image below.

S NoItemDescriptionQuantity
1HLK-LD2420Presence sensor1
2XIAO ESP32 S3Main microcontroller1
35V single-channel relayTo turn an external device on and off1
4BreadboardTo connect all the components1
5Jumper WiresTo connect all the components    -

Circuit Diagram and Wiring

Let's take a look at the wiring connections.

HLK-LD2420 PinXIAO ESP32-S3 PinRelay PinNotes
3V33V3    -Supply 3.3 V only (do not feed 5 V into 3V3)
TXD7 (GPIO44 / RX)   - Crossed UART: radar TX to XIAO RX
RXD6 (GPIO43 / TX) - Crossed UART: radar RX to XIAO TX
GNDGNDGND Common ground across radar, XIAO, and relay
—5VVCC5V supply for relay module.
—D2 (GPIO3)INRelay control signal pin

We are connecting the sensor to the ESP32 to its 3V3, GND and UART pins (D7 of XIAO - TX of mmWave sensor, D6 of XIAO  - RX of mmWave). The UART pins should be connected properly; it's explained in the HLK-LD2420 Configuration section too.  

Working 

The sensor is configured to detect the finest movements, even my breathing. The parameters set in the software tool are shown below. The values can be adjusted as per requirements, like the detection and sensitivity. To find the exact values, we will have to do trial and error.

Once all these values are set properly and the sensitivity is finalised with a USB-TTL converter connected to the sensor and laptop, let's go ahead and build our circuit. The circuit diagram is shown below. We have set the Max Range Gate to 1 as that is enough for our tabletop application which covers around 1.40m. It can be adjusted as per the custom requirements.

Circuit Diagram of ESP32 Presence Sensor

When the sensor detects any reasonable motion in the set area, the Arduino sketch reads ON and over UART and drives D2 using setRelay(), and the relay gets triggered, and the light turns on. After this trigger, the hold threshold helps maintain this state by detecting any fine movements like breathing or small movements of the finger, or so. Once the Absence Report Delay expires with no presence detected, the sensor outputs OFF, and the ESP32 switches the relay off. To trigger the lamp, we are using a 5V single-channel relay module. It's connected to D2 of the XIAO ESP32 S3.

ESP32 Presence Sensor Code Explanation

#include <Arduino.h>
static const int RADAR_RX_PIN = D7;     // Connects to HLK-LD2420 TX
static const int RADAR_TX_PIN = D6;     // Connects to HLK-LD2420 RX
static const int RELAY_PIN    = D2;     // Connects to Relay IN pin
static const bool RELAY_ACTIVE_HIGH = true;
static const unsigned long RADAR_BAUD = 115200;

D7 and D6 are assigned the XIAO ESP32-S3 UART pins (RX and TX) that talk to the radar sensor; D2 sets the GPIO output pin driving the relay board's IN pin; RELAY_ACTIVE_HIGH flips output polarity (true = HIGH turns ON, false = LOW turns ON) to match the relay module; and RADAR_BAUD sets the serial speed (115200 baud; 256000 on older firmware), so incoming radar bytes aren't garbled.

void setRelay(bool turnOn) {
 relayState = turnOn;
 uint8_t level = turnOn
   ? (RELAY_ACTIVE_HIGH ? HIGH : LOW)
   : (RELAY_ACTIVE_HIGH ? LOW : HIGH);
 digitalWrite(RELAY_PIN, level);
 Serial.printf("[RELAY] Light %s\n", turnOn ? "ON" : "OFF");
}

Converts turnOn (true or false) into the exact HIGH or LOW voltage level required by your relay module and updates relayState and prints [RELAY] Light ON or [RELAY] Light OFF to the USB Serial Monitor so you can see whether the code tried to switch the relay.

if (line.equalsIgnoreCase("ON") || line.startsWith("ON")) {
   presenceDetected = true;
   waitingToTurnOff = false;
   if (!relayState) {
     setRelay(true);
   }
 } else if (line.equalsIgnoreCase("OFF") || line.startsWith("OFF")) {
   presenceDetected = false;
   if (EXTRA_OFF_HOLD_MS == 0) {
     if (relayState) {
       setRelay(false);
     }
   }

Checks each complete line received from the HLK-LD2420. When "ON" arrives, it cancels any pending shutoff timer and calls setRelay(true) if the relay is off. When "OFF" arrives, it marks presence as cleared and turns the relay off immediately (when EXTRA_OFF_HOLD_MS is 0).

// In setup(): 
setRelay(false);
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
// In loop(): 
while (Serial1.available() > 0) {
   char c = static_cast<char>(Serial1.read());
   if (c == '\n') {
     handleRadarLine(lineBuffer);
     lineBuffer = "";
   } else if (c != '\r') {
     lineBuffer += c;
     if (lineBuffer.length() > 128) {
       lineBuffer = ""; // Prevent runaway buffer on malformed stream
     }
   }

setup() sets the relay output to OFF before enabling OUTPUT mode (preventing a boot glitch), then opens Serial1 on D7/D6 at RADAR_BAUD. In loop(), the code collects characters from Serial1 into lineBuffer until a newline (\n) arrives, then sends the full line to handleRadarLine() and clears the buffer.

Troubleshooting Common Problems

ProblemLikely CauseFix
No data from the sensorTX and RX not crossed, or sensor not poweredSwap TX and RX and confirm 3.3 V and common GND
Garbled charactersWrong baud rateUse 115200 for standard firmware, 256000 for older firmware
Relay works backwardsActive-LOW relay moduleSet RELAY_ACTIVE_HIGH to false
Light turns off while you sit stillHold threshold too highLower the hold threshold for the gates where you sit
False triggersFans, curtains, motion outside the zone, nearby metalReduce the Max Range Gate, raise trigger thresholds, move the sensor
Light stays on too longLong Absence Report DelayLower it from the 30 s default, for example to 5 s

Live Demo of ESP32 Presence Sensor

Watch the HLK-LD2420 mmWave radar and XIAO ESP32-S3 switch a desk lamp on as soon as I sit down, and keep it on even when I'm barely moving. The lamp turns off after the Absence Report Delay expires once I leave the desk, so you can see the full presence detection cycle in action.

Final Results 

As shown in the image below, we have mounted the sensor on a 3D-printed stand so that it sits at the same level as a person working on the workbench. It can even be kept inside a plastic case, and it can detect through it. The ESP32 S3 can be powered from a suitable 5V power supply. We have connected the relay module to the 5V pin of the XIAO ESP32 S3.

Conclusion 

Pairing the HLK-LD2420 24 GHz mmWave radar with a Seeed Studio XIAO ESP32-S3 and a relay module turns basic motion lighting into a responsive, room-aware automation system. Calibrating the 0.70 m range gates, pairing Trigger and Hold Thresholds to match your mounting height and movement style, and tuning the Absence Report Delay let you keep lights reliably on while you're present, without unnecessary drop-offs or lingering delays after you leave.

Reliable operation comes down to keeping hardware and software clean as you move from bench testing to live control. Supply a steady 3.3 V rail with a common ground, keep metal away from the etched patch antenna, verify relay polarity (HIGH vs LOW) and mains safety early, and parse ON/OFF UART messages with clear serial logging for a dependable lighting setup ready for future schedules or smart-home integrations.

GitHub Repository

Get the complete sketch for this ESP32 presence sensor project, along with the HLK-LD2420 configuration tool, from our GitHub repository. Open the sketch in the Arduino IDE, select the XIAO ESP32-S3 board, set RELAY_ACTIVE_HIGH to match your relay module, and upload it to start switching your own lamp.

ESP32 Presence Sensor GitHub RepoESP32 Presence Sensor Zip File

Frequently Asked Questions About ESP32 Presence Sensor

⇥ How does the mmWave sensor sense someone who is stationary?
A mmWave radar transmits high-frequency radio waves, and it measures the returned waveforms, taking into account any variation in the waveform that is caused by subtle breathing or body movements of a seated individual, and the sensor’s firmware is calibrated against a set of thresholds to determine whether there is presence of a person in the vicinity even if no significant changes can be observed. Unlike PIR sensors that only sense motion, mmWave sensors can detect still people.

⇥ What are the differences between mmWave and PIR sensors?
Unlike a PIR sensor which works on detecting changes in the intensity of infrared signals emitted by moving warm bodies, a mmWave sensor works by detecting the change in waveform caused by any fine motion. While mmWave sensors are costlier and need some calibration (to fine-tune thresholds), PIR sensors are relatively cheap and easier to implement to receive alerts in the case of motion detection.

⇥ Which ESP32 board is compatible with the HLK-LD2420?
Any ESP32 board with spare hardware UART ports will do. For the purpose of this project I used the XIAO ESP32-S3 since it is a small form factor device with available UART ports on D6 and D7. All other ESP32 devices should work provided that RX and TX are connected to valid GPIO ports and that the baud rate of 115200 is set accordingly.

⇥ What is the detection range of the HLK-LD2420 sensor?
Every range gate corresponds to a 0.70 m distance. The sensor features 16 range gates - numbered 0 through 15. When you use the max gate of 1, you are able to detect objects at a distance of 1.40 m, which is enough for detecting presence near a desk. Using gate arithmetic, it is possible to achieve a maximum range of 11.2 m, but in practice, this number can vary because of the sensor positioning, surroundings, and set threshold settings.

⇥ Will the sensor transmit its signal through a plastic or a wall?
mmWave can transmit through many types of plastics, so using a plastic housing will generally work. However, metal will block or reflect the signal, so make sure that you place the sensor somewhere that isn't blocked by metal. Since walls are not made of the same material and are not all the same thickness, it is best to test your setup on your own rather than assume that you will receive a reading through them.

Smart Motion Detection & AI-Based Security Projects

Explore DIY security and automation projects using PIR sensors, Raspberry Pi, OpenCV, and ESP32-CAM to detect movement, trigger alarms, and recognise human faces. From automatic motion-sensing lights to intelligent CCTV monitoring and AI-powered face detection, discover practical ways to build smarter security systems.

Build your own PIR Motion Sensor Light

Build your own PIR Motion Sensor Light

In this project, we'll learn how to make a motion sensor light without the need for a microcontroller. A motion sensor light turns on when it senses movement nearby. It's handy for lighting up an area when someone walks by, helping them see better. 

CCTV Motion Detection with Alarm

CCTV Motion Detection with Alarm 

In this article, we will learn how to leverage the power of OpenCV and build a Raspberry Pi Motion Detection system on our live CCTV footages. If you do not have a CCTV installed, you can still build a Raspberry Pi Surveillance system by connecting USB cameras directly to your Pi.

ESP32-CAM for Face Detection Using CircuitDigest Cloud

ESP32-CAM for Face Detection Using CircuitDigest Cloud

In this project, we take that movie-like idea and turn it into a real working system using just a small ESP32-CAM module, no external camera, no bulky setup. By simply uploading the code, the device can capture images and detect human faces in real time.

Complete Project Code

#include <Arduino.h>
// XIAO ESP32-S3 pins
static const int RADAR_RX_PIN = D7;     // Connects to HLK-LD2420 TX
static const int RADAR_TX_PIN = D6;     // Connects to HLK-LD2420 RX
static const int RELAY_PIN    = D2;     // Connects to Relay IN pin
// Set to false if your relay board is active-LOW
static const bool RELAY_ACTIVE_HIGH = true;
// Radar UART baud rate (115200 is standard on newer HLK-LD2420 firmware; 256000 on older ones)
static const unsigned long RADAR_BAUD = 115200;
// Optional extra hold time in ms after sensor reports OFF (0 = follow sensor Absence Report Delay directly)
static const unsigned long EXTRA_OFF_HOLD_MS = 0;
String lineBuffer = "";
bool presenceDetected = false;
bool relayState = false;
unsigned long lastOffSeenAt = 0;
bool waitingToTurnOff = false;
void setRelay(bool turnOn) {
 relayState = turnOn;
 uint8_t level = turnOn
   ? (RELAY_ACTIVE_HIGH ? HIGH : LOW)
   : (RELAY_ACTIVE_HIGH ? LOW : HIGH);
 digitalWrite(RELAY_PIN, level);
 Serial.printf("[RELAY] Light %s\n", turnOn ? "ON" : "OFF");
}
void handleRadarLine(String line) {
 line.trim();
 if (line.length() == 0) return;
 Serial.printf("[LD2420] %s\n", line.c_str());
 // HLK-LD2420 ASCII output commonly reports "ON", "OFF", and "Range <cm>"
   if (line.equalsIgnoreCase("ON") || line.startsWith("ON")) {
     presenceDetected = true;
     waitingToTurnOff = false;
     if (!relayState) {
       setRelay(true);
     }
   } else if (line.equalsIgnoreCase("OFF") || line.startsWith("OFF")) {
     presenceDetected = false;
     if (EXTRA_OFF_HOLD_MS == 0) {
       if (relayState) {
         setRelay(false);
       }
     } else if (relayState && !waitingToTurnOff) {
     waitingToTurnOff = true;
     lastOffSeenAt = millis();
   }
 }
}
void setup() {
 Serial.begin(115200);
 delay(200);
 // Initialize relay pin before enabling output
 setRelay(false);
 pinMode(RELAY_PIN, OUTPUT);
 setRelay(false);
 // Hardware UART1 on XIAO ESP32-S3 (D7 = RX, D6 = TX)
 Serial1.begin(RADAR_BAUD, SERIAL_8N1, RADAR_RX_PIN, RADAR_TX_PIN);
 Serial.println("XIAO ESP32-S3 + HLK-LD2420 Relay Controller started.");
}
void loop() {
 while (Serial1.available() > 0) {
   char c = static_cast<char>(Serial1.read());
   if (c == '\n') {
     handleRadarLine(lineBuffer);
     lineBuffer = "";
   } else if (c != '\r') {
     lineBuffer += c;
     if (lineBuffer.length() > 128) {
       lineBuffer = ""; // Prevent runaway buffer on malformed stream
     }
   }
 }
 if (waitingToTurnOff && !presenceDetected) {
   if (millis() - lastOffSeenAt >= EXTRA_OFF_HOLD_MS) {
     waitingToTurnOff = false;
     setRelay(false);
   }
 }
}
Have any question related to this Article?

Add a comment

No account needed. Your comment appears once a moderator has reviewed it.

Never shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.