LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
changeConfig.ino
Go to the documentation of this file.
1/**
2 * @brief Example: Change configuration of the LD2410
3 *
4 * @details
5 * This sketch shows how to:
6 * 1. Initialize the radar on Serial1.
7 * 2. Query all current configuration values from the sensor.
8 * 3. Clone the config into a local variable.
9 * 4. Modify some settings (timeout, sensitivities).
10 * 5. Write the modified config back to the sensor.
11 *
12 * @warning
13 * Important:
14 * Make sure to adjust RADAR_RX_PIN and ADAR_TX_PIN to match you actual wiring.
15 */
16
17#include <Arduino.h>
18#include "LD2410Async.h"
19
20 // ========================= USER CONFIGURATION =========================
21
22 // UART pins for the LD2410 sensor
23#define RADAR_RX_PIN 16 // ESP32 pin that receives data from the radar (radar TX)
24#define RADAR_TX_PIN 17 // ESP32 pin that transmits data to the radar (radar RX)
25
26// UART baudrate for the radar sensor (default is 256000)
27#define RADAR_BAUDRATE 256000
28
29// ======================================================================
30
31/**
32* Create a HardwareSerial instance (ESP32 has multiple UARTs) bound to UART1
33*/
34HardwareSerial RadarSerial(1);
35
36/**
37* @brief Creates LD2410Async object bound to the serial port defined in RadarSerial
38*/
40
41/**
42* @brief Callback after the new config has been written to the sensor
43*
44* @details
45* This method just checks and prints the result
46*/
49 Serial.println("Config updated successfully!");
50 }
51 else {
52 Serial.println("Failed to apply config.");
53 }
54}
55
56/**
57* @brief Callback after receiving the config data
58*
59* @details
60* Checks the result of the async requestAllConfigSettingsAsync() command, gets a clone of the config data using getConfigData(), modifies it and writes back
61* using the async configureAllConfigSettingsAsync() command.
62*
63* @note
64* Always check the result of the async command that has triggered the callback. Otherweise async commands can fail, timeout or get canceled without you relizing it.
65* In callback methods is is generally advised to access members of the LD2410Async instance via the sender pointer.
66* This ensures that you are allways working with the correct instance, which is important if you have multiple LD2410Async instances.
67* Also keep in mind that callbacks should be as short and efficient as possible to avoid blocking the background task of the library.
68*/
71 Serial.println("Failed to request config data.");
72 return;
73 }
74
75 Serial.println("Config data received. Cloning and modifying...");
76
77 // Clone the current config
79 //alternatively you could also call the LD2410Async instance directly instead of using the pointer in sender:
80 //LD2410Types::ConfigData cfg = radar.getConfigData();
81 //However, working with the reference in sender is a good practice since it will always point to the LD2410Async instance that triggered the callback (important if you have several instances resp. more than one LD2410 connected to your board)..
82
83
84 // Example modifications:
85 cfg.noOneTimeout = 120; // Set "no-one" timeout to 120 seconds
86 cfg.distanceGateMotionSensitivity[2] = 75; // Increase motion sensitivity on gate 2
87 cfg.distanceGateStationarySensitivity[4] = 60; // Adjust stationary sensitivity on gate 4
88
89 // Apply updated config to the radar
90 if (!sender->configureAllConfigSettingsAsync(cfg, false, onConfigApplied)) {
91 //It is good practive to check the return value of commands for true/false.
92 //False indicates that the command could not be sent (e.g. because another async command is still pending)
93 Serial.println("Error! Could not update config on the sensor");
94 };
95
96 //alternatively you could also call the LD2410Async instance directly instead of using the pointer in sender:
97 //radar.configureAllConfigSettingsAsync(cfg, false, onConfigApplied);
98 //However, working with the reference in sender is a good practice since it will always point to the LD2410Async instance that triggered the callback (important if you have several instances resp. more than one LD2410 connected to your board)..
99
100};
101
102/**
103* @brief Arduino setup function which initializes the radar and starts the config change process
104*
105* @details
106* begin() starts the background task of the LD2410Async library which automatically handles
107* incoming data and triggers callbacks. The onDetectionDataReceived callback is registered to
108* receive detection data.
109* requestAllConfigSettingsAsync() will fetch all config data from the sensor and triggers the
110* onConfigReceived callback when done.
111*/
112void setup() {
113 // Initialize USB serial for debug output
114 Serial.begin(115200);
115 while (!Serial) {
116 ; // wait for Serial Monitor
117 }
118 Serial.println("LD2410Async example: change radar config");
119
120 // Initialize Serial1 with user-defined pins and baudrate
122
123 // Start the radar background task (parses incoming data frames)
124 if (radar.begin()) {
125 Serial.println("Radar task started successfully.");
126
127 // Request all config data, then modify in callback
129 //It is good practive to check the return value of commands for true/false.
130 //False indicates that the command could not be sent (e.g. because another async command is still pending)
131 Serial.println("Error! Could not start config data request");
132 }
133
134 }
135 else {
136 Serial.println("Error! Could not start radar task");
137 }
138
139}
140
141/**
142* @brief Arduino loop function which does nothing
143*
144* @details
145* The LD2410Async library runs a FreeRTOS background task that automatically handles all jobs that are related to the radar sensor.
146* Therefore the main loop doesnt have to da any LD2410 related work and is free for anything else you might want to do.
147*/
148void loop() {
149 // Nothing to do here.
150 // The library handles communication and invokes callbacks automatically.
151 delay(1000);
152}
HardwareSerial RadarSerial(1)
void onConfigReceived(LD2410Async *sender, LD2410Async::AsyncCommandResult result)
Callback after receiving the config data.
void setup()
Arduino setup function which initializes the radar and starts the config change process.
void onConfigApplied(LD2410Async *sender, LD2410Async::AsyncCommandResult result)
Callback after the new config has been written to the sensor.
LD2410Async radar(RadarSerial)
Creates LD2410Async object bound to the serial port defined in RadarSerial.
#define RADAR_BAUDRATE
#define RADAR_RX_PIN
Example: Change configuration of the LD2410.
#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.
AsyncCommandResult
Result of an asynchronous command execution.
Definition LD2410Async.h:47
@ SUCCESS
Command completed successfully and ACK was received.
bool configureAllConfigSettingsAsync(const LD2410Types::ConfigData &configToWrite, bool writeAllConfigData, AsyncCommandCallback callback)
Applies a full ConfigData struct to the LD2410.
LD2410Types::ConfigData getConfigData() const
Returns a clone of the current configuration data of the radar.
bool requestAllConfigSettingsAsync(AsyncCommandCallback callback)
Requests all configuration settings from the sensor.
byte distanceGateStationarySensitivity[9]
Stationary sensitivity values per gate (0-100).
byte distanceGateMotionSensitivity[9]
Motion sensitivity values per gate (0-100).
unsigned short noOneTimeout
Timeout (seconds) until "no presence" is declared.