LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
basicPresenceDetection.ino
Go to the documentation of this file.
1/**
2 * @brief Example: Basic Presence Detection with LD2410Async
3 *
4 * @details
5 * This sketch demonstrates how to use the LD2410Async library to detect presence
6 * using only the presenceDetected variable from the detection data callback.
7 * It prints a message with a timestamp whenever the presence state changes.
8 *
9 * @warning
10 * Important!
11 * Adjust RADAR_RX_PIN and RADAR_TX_PIN to match your wiring.
12 */
13
14#include <Arduino.h>
15#include "LD2410Async.h"
16
17 // ========================= USER CONFIGURATION =========================
18
19 // UART pins for the LD2410 sensor
20#define RADAR_RX_PIN 16 // ESP32 pin that receives data from the radar (radar TX)
21#define RADAR_TX_PIN 17 // ESP32 pin that transmits data to the radar (radar RX)
22
23// UART baudrate for the radar sensor (default is 256000)
24#define RADAR_BAUDRATE 256000
25
26// ======================================================================
27
28/**
29* Create a HardwareSerial instance (ESP32 has multiple UARTs) bound to UART1
30*/
31HardwareSerial RadarSerial(1);
32
33/**
34* @brief Creates LD2410Async object bound to the serial port defined in RadarSerial
35*/
37
38
39// Track last presence state
41bool firstCallback = true;
42
43/**
44* @brief Callback function called whenever new detection data arrives
45*
46* @details
47* Since only basic presence information is required in the example, it is just using the presenceDetected para of the callback.
48* The logic in this methods just ensures that only changes in presence state are printed to the Serial Monitor.
49*
50 * @note
51 * If only basic presence detection is needed, use the presenceDetected variable directly instead of accessing the full detection data struct.
52 * For more advanced use cases, the full detection data can be accessed using getDetectionDataRef() or getDetectionData().
53*/
54void onDetectionDataReceived(LD2410Async* sender, bool presenceDetected) {
55 if (firstCallback || presenceDetected != lastPresenceDetected) {
56 unsigned long now = millis();
57 Serial.print("[");
58 Serial.print(now);
59 Serial.print(" ms] Presence detected: ");
60 Serial.println(presenceDetected ? "YES" : "NO");
61 lastPresenceDetected = presenceDetected;
62 firstCallback = false;
63 }
64}
65
66/**
67* @brief Arduino setup function which initializes the radar and registers the callback
68*
69* @details
70* radar.begin() starts the background task of the LD2410Async library which automatically handles
71* incoming data and triggers callbacks. The onDetectionDataReceived callback is registered to receive detection data.
72*
73*/
74void setup() {
75 // Initialize USB serial for debug output
76 Serial.begin(115200);
77 while (!Serial) {
78 ; // wait for Serial Monitor
79 }
80 Serial.println("LD2410Async Example: Basic Presence Detection");
81
82 // Initialize Serial1 with user-defined pins and baudrate
84
85 // Start the radar background task (parses incoming data frames)
86 if (radar.begin()) {
87 Serial.println("Radar task started successfully.");
88 // Register callback for detection updates
90 }
91 else {
92 Serial.println("ERROR! Could not start radar task.");
93 }
94}
95
96/**
97* @brief Arduino loop function which does nothing
98*
99* @details
100* The LD2410Async library runs a FreeRTOS background task that automatically handles all jobs that are related to the radar sensor.
101* Therefore the main loop doesnt have to da any LD2410 related work and is free for anything else you might want to do.
102*/
103void loop() {
104 // Nothing to do here!
105 delay(1000); // idle delay (optional)
106}
bool lastPresenceDetected
HardwareSerial RadarSerial(1)
void setup()
Arduino setup function which initializes the radar and registers the callback.
void onDetectionDataReceived(LD2410Async *sender, bool presenceDetected)
Callback function called whenever new detection data arrives.
LD2410Async radar(RadarSerial)
Creates LD2410Async object bound to the serial port defined in RadarSerial.
#define RADAR_BAUDRATE
bool firstCallback
#define RADAR_RX_PIN
Example: Basic Presence Detection with LD2410Async.
#define RADAR_TX_PIN
void loop()
Arduino loop function which does nothing.
Asynchronous driver class for the LD2410 human presence radar sensor.
Definition LD2410Async.h:38
bool begin()
Starts the background task that continuously reads data from the sensor.
void onDetectionDataReceived(DetectionDataCallback callback)
Registers a callback for new detection data.