A no_std, zero-heap Rust library for reading and controlling the HLK-LD2410S 24GHz radar presence sensor over UART.
- Portability: Core driver is
no_stdand uses zero heap allocations (viaheapless). - Flexible IO: Supports Blocking (Sync) and Async (via
embedded-io-async). - Backend Agnostic: Works on desktop (via
serialport) and embedded (viaesp-idf-hal) through a simple trait abstraction. - Unified Parsing: Built-in frame parsing for:
- Minimal Packets (Presence state, Distance)
- Standard/Engineering Packets (Distance, Signal Energy arrays)
- Firmware Version & Serial Number
- Full Configuration:
- Switch between Minimal and Standard modes.
- Set reporting frequencies, response speeds, and gate thresholds.
- Support for Automatic Calibration.
This library implements the protocol defined in the official HLK-LD2410S manuals:
Add to your Cargo.toml:
[dependencies]
# Desktop Serial (Sync)
ld2410s = { version = "0.3.0", features = ["serial"] }
# Async Support
# ld2410s = { version = "0.3.0", features = ["async"] }
# Embedded ESP-IDF
# ld2410s = { version = "0.3.0", features = ["embedded"] }cargo run --example serial --features serial -- /dev/tty.usbserial-123
let port = serialport::new("/dev/tty.usbserial-123", BAUD_RATE)
.timeout(Duration::from_millis(50))
.open()?;
// 1. Initialize sensor (Standard Mode = Engineering Data)
// LD2410S::new(UART, TIMER, MODE)
let mut sensor = LD2410S::new(SerialPortWrapper(port), StdTimer::default(), OutputMode::Standard);
sensor.init()?;
loop {
if let Ok(Some(packet)) = sensor.next_packet() {
match packet {
ld2410s::Packet::Standard(s) => println!("Dist: {} Energy: {:?}", s.distance_cm, s.energy),
_ => {}
}
}
}Runs the sensor's automatic threshold calibration. Keep the detection area empty while it runs (default 120 s).
cargo run --example calibrate --features serial -- /dev/tty.usbserial-123 [seconds]
cargo run --example async_serial --features async
let port = tokio_serial::new("/dev/tty.usbserial-123", BAUD_RATE).open_native_async()?;
let mut sensor = LD2410SAsync::new(FromTokio::new(port), OutputMode::Standard);
sensor.init().await?;
loop {
if let Ok(Some(packet)) = sensor.next_packet().await {
// handle packet...
}
}cargo run --example esp --features embedded
let uart = UartDriver::new(peripherals.uart1, pins.gpio4, pins.gpio5, None, None, &cfg)?;
let mut sensor = LD2410S::new(EspUartWrapper(uart), EspTimer, OutputMode::Standard);
sensor.init()?;
loop {
if let Ok(Some(packet)) = sensor.next_packet() {
// handle packet...
}
}cargo run --example async_esp --features embedded,async
let uart = AsyncUartDriver::new(peripherals.uart1, pins.gpio4, pins.gpio5, None, None, &cfg)?;
let mut sensor = LD2410SAsync::new(uart, OutputMode::Standard);
block_on(async {
sensor.init().await.unwrap();
loop {
if let Ok(Some(packet)) = sensor.next_packet().await {
// handle packet...
}
}
})serial→ Use serialport (desktop/hosted).embedded→ Use esp-idf-hal (ESP32).async→ Use embedded-io-async and embassy-time.
Copyright (c) 2025 Max van der Schee. Licensed under the EUPL-1.2.