ld2410: an example that runs
Yes. You can use the ld2410 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Presence radar, LD2410). The example below, "Read an LD2410 radar on an ESP32’s Serial1", runs on the ESP32 DevKit V1.
Built in on Mokxi: #include <ld2410.h> and it builds, with nothing to install. Free, and no account needed.
What ld2410 does
Ld2410 radar: begin(Serial1), read(), presenceDetected(), the moving and stationary targets’ distance and energy, the firmware version and the gate configuration commands. That is what ld2410, by ncmreynolds, covers. The sketch below uses it on the ESP32 DevKit V1: “Read an LD2410 radar on an ESP32’s Serial1”.
Ncmreynolds' ld2410 library's interface, for the HiLink LD2410 24 GHz human presence radar on a serial port.
As upstream, isConnected() is true while a report came in the last 100 ms, and a target counts as detected only with a distance and an energy over 0. The radar runs at 256000 baud: use a hardware serial port (an ESP32's Serial1 or Serial2, a Mega's Serial1), as SoftwareSerial cannot keep up.
The calls follow release 0.2.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.
| Library Manager name | ld2410 |
|---|---|
| Author | ncmreynolds |
| Include | #include <ld2410.h> |
| Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Presence radar, LD2410 |
Install it
It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6.
In Mokxi it is built in: #include <ld2410.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "ld2410" by ncmreynolds from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ld2410 to lib_deps in PlatformIO. Mokxi follows the calls of release 0.2.2, so a sketch written here builds there too.
What to know in the simulator: Talks to the LD2410 part at 256000 baud: its reports, the configuration commands and their acknowledgments. Use a hardware serial port (an ESP32’s Serial1 or Serial2, a Mega’s Serial1); SoftwareSerial cannot keep up. autoReadTask() is an ESP32 FreeRTOS task Mokxi does not run, so call read() in loop().
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- Arduino Pro Mini 3.3 V
- ESP8266 NodeMCU
- ESP32 DevKit
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- ESP32-S3-DevKitC-1
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- ESP32-C3
- ESP32-C6
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
The calls you will use
In the order a sketch meets them.
ld2410 radar
Makes radar, the ld2410 object the calls below are made on.
radar.begin(RADAR_SERIAL)
Starts it up and returns true if it started, false if it did not. Call it once in setup(). Arguments: radar stream, wait for radar.
radar.read()
Returns the value as true or false.
radar.isConnected()
Returns true or false for "is connected".
Example: Read an LD2410 radar on an ESP32’s Serial1
On the ESP32 DevKit V1. It runs in the browser exactly as printed.
/*
* Example sketch for reporting on readings from the LD2410 using whatever settings are currently configured.
*/
#if defined(ESP32)
#define MONITOR_SERIAL Serial
#define RADAR_SERIAL Serial1
#define RADAR_RX_PIN 32
#define RADAR_TX_PIN 33
#else
#include <SoftwareSerial.h>
#define MONITOR_SERIAL Serial
SoftwareSerial radarSerial(2, 3); // RX, TX
#define RADAR_SERIAL radarSerial
#endif
#include <ld2410.h>
ld2410 radar;
uint32_t lastReading = 0;
bool radarConnected = false;
void setup(void)
{
MONITOR_SERIAL.begin(115200); //Feedback over Serial Monitor
//radar.debug(MONITOR_SERIAL); //Uncomment to show debug information from the library on the Serial Monitor. By default this does not show sensor reads as they are very frequent.
#if defined(ESP32)
RADAR_SERIAL.begin(256000, SERIAL_8N1, RADAR_RX_PIN, RADAR_TX_PIN); //UART for monitoring the radar
#else
RADAR_SERIAL.begin(256000); //UART for monitoring the radar
#endif
delay(500);
MONITOR_SERIAL.print(F("LD2410 radar sensor initializing: "));
if(radar.begin(RADAR_SERIAL))
{
MONITOR_SERIAL.println(F("OK"));
MONITOR_SERIAL.print(F("LD2410 firmware version: "));
MONITOR_SERIAL.print(radar.firmware_major_version);
MONITOR_SERIAL.print('.');
MONITOR_SERIAL.print(radar.firmware_minor_version);
MONITOR_SERIAL.print('.');
MONITOR_SERIAL.println(radar.firmware_bugfix_version, HEX);
}
else
{
MONITOR_SERIAL.println(F("not connected"));
}
}
void loop()
{
radar.read();
if(radar.isConnected() && millis() - lastReading > 1000) //Report every 1000ms
{
lastReading = millis();
if(radar.presenceDetected())
{
if(radar.stationaryTargetDetected())
{
Serial.print(F("Stationary target: "));
Serial.print(radar.stationaryTargetDistance());
Serial.print(F("cm energy:"));
Serial.print(radar.stationaryTargetEnergy());
Serial.print(' ');
}
if(radar.movingTargetDetected())
{
Serial.print(F("Moving target: "));
Serial.print(radar.movingTargetDistance());
Serial.print(F("cm energy:"));
Serial.print(radar.movingTargetEnergy());
}
Serial.println();
}
else
{
Serial.println(F("No target"));
}
}
}Wiring
- ESP32 DevKit V1 D32 to Presence radar, LD2410 TX
- ESP32 DevKit V1 D33 to Presence radar, LD2410 RX
- ESP32 DevKit V1 VIN to Presence radar, LD2410 VCC
- ESP32 DevKit V1 GND to Presence radar, LD2410 GND
Projects that use ld2410
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Try ld2410 without installing anything
Open the example, change the sketch, and keep your version in a free account.