LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
receiveData.ino
Go to the documentation of this file.
1/**
2* @brief: Example: Receive detection data from the LD2410
3*
4* @details
5* This sketch initializes the LD2410 radar sensor on Serial1 and
6* prints detection data to the Serial Monitor as soon as it arrives.
7* This sketch demonstrates how to:
8* 1. Initialize the radar on Serial1.
9* 2. register a callback to receive detection data.
10* 3. Get a pointer to the detection data struct.
11*
12* @warning
13* Important:
14* Make sure to adjust RADAR_RX_PIN and RADAR_TX_PIN to match you actual wiring.
15*/
16
17#include <Arduino.h>
18#include "LD2410Async.h"
19
20
21
22// ========================= USER CONFIGURATION =========================
23
24 // UART pins for the LD2410 sensor
25#define RADAR_RX_PIN 16 // ESP32 pin that receives data from the radar (radar TX)
26#define RADAR_TX_PIN 17 // ESP32 pin that transmits data to the radar (radar RX)
27
28// UART baudrate for the radar sensor (default is 256000)
29#define RADAR_BAUDRATE 256000
30
31// ======================================================================
32
33/**
34* Create a HardwareSerial instance (ESP32 has multiple UARTs) bound to UART1
35*/
36HardwareSerial RadarSerial(1);
37
38/**
39* @brief Creates LD2410Async object bound to the serial port defined in RadarSerial
40*/
42
43/*
44 * @brief Callback function called whenever new detection data arrives
45 *
46 * @details
47 * The detection data is accessed via a reference to avoid making a copy. This is more efficient and saves memory.
48 * The callback provides the presenceDetected variable for convenience, but all other detection data is available
49 * in the DetectionData struct that can be accessed using .getDetectionDataRef()
50 *
51 * @note
52 * In callback methods is is generally advised to access members of the LD2410Async instance via the sender pointer.
53 * This ensures that you are allways working with the correct instance, which is important if you have multiple LD2410Async instances.
54 * Also keep in mind that callbacks should be as short and efficient as possible to avoid blocking the background task of the library.
55 */
56void onDetectionDataReceived(LD2410Async* sender, bool presenceDetected) {
57 // Access detection data efficiently without making a copy
58 const LD2410Types::DetectionData& data = sender->getDetectionDataRef();
59
60 Serial.println("=== Detection Data ===");
61
62 Serial.print("Target State: ");
63 Serial.println(LD2410Types::targetStateToString(data.targetState));
64
65 Serial.print("Presence detected: ");
66 Serial.println(data.presenceDetected ? "Yes" : "No");
67
68 Serial.print("Moving presence: ");
69 Serial.println(data.movingPresenceDetected ? "Yes" : "No");
70
71 Serial.print("Stationary presence: ");
72 Serial.println(data.stationaryPresenceDetected ? "Yes" : "No");
73
74 Serial.print("Moving Distance (cm): ");
75 Serial.println(data.movingTargetDistance);
76
77 Serial.print("Stationary Distance (cm): ");
78 Serial.println(data.stationaryTargetDistance);
79
80 Serial.print("Light Level: ");
81 Serial.println(data.lightLevel);
82
83 Serial.println("======================");
84}
85
86/**
87* @brief Arduino setup function which initializes the radar and registers the callback
88*
89* @details
90* radar.begin() starts the background task of the LD2410Async library which automatically handles
91* incoming data and triggers callbacks. The onDetectionDataReceived callback is registered to receive detection data.
92*
93*/
94void setup() {
95 // Initialize USB serial for debug output
96 Serial.begin(115200);
97 while (!Serial) {
98 ; // wait for Serial Monitor
99 }
100 Serial.println("LD2410Async Example: Receive Data");
101
102 // Initialize Serial1 with user-defined pins and baudrate
104
105 // Start the radar background task (parses incoming data frames)
106 if (radar.begin()) {
107 Serial.println("Radar task started successfully.");
108 // Register callback for detection updates
110 }
111 else {
112 Serial.println("ERROR! Could not start radar task.");
113 }
114
115
116}
117/**
118* @brief Arduino loop function which does nothing
119*
120* @details
121* The LD2410Async library runs a FreeRTOS background task that automatically handles all jobs that are related to the radar sensor.
122* Therefore the main loop doesnt have to da any LD2410 related work and is free for anything else you might want to do.
123*/
124void loop() {
125 // Nothing to do here!
126 // The LD2410Async library runs a FreeRTOS background task
127 // that automatically parses incoming radar data and triggers callbacks.
128 delay(1000); // idle delay (optional)
129}
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.
const LD2410Types::DetectionData & getDetectionDataRef() const
Access the current detection data without making a copy.
HardwareSerial RadarSerial(1)
void setup()
Arduino setup function which initializes the radar and registers the callback.
void onDetectionDataReceived(LD2410Async *sender, bool presenceDetected)
LD2410Async radar(RadarSerial)
Creates LD2410Async object bound to the serial port defined in RadarSerial.
#define RADAR_BAUDRATE
#define RADAR_RX_PIN
: Example: Receive detection data from the LD2410
#define RADAR_TX_PIN
void loop()
Arduino loop function which does nothing.
Holds the most recent detection data reported by the radar.
void print() const
Debug helper: print detection data contents to Serial.
bool stationaryPresenceDetected
True if a stationary target is detected.
byte lightLevel
Reported ambient light level (0-255).
TargetState targetState
Current detection state.
bool presenceDetected
True if any target is detected.
bool movingPresenceDetected
True if a moving target is detected.
unsigned int movingTargetDistance
Distance (cm) to the nearest moving target.
unsigned int stationaryTargetDistance
Distance (cm) to the nearest stationary target.