Skip to content

Fiooodooor/HLK-LD245X

Repository files navigation

HLK-LD245X

License Release

Description

The HLK-LD245X library provides an easy-to-use Arduino interface for the Hi-Link LD2450 and LD2451 mmWave radar sensors. These sensors are designed for human presence detection, motion tracking, and zone-based occupancy monitoring. The library handles serial communication (UART) to configure the sensors and parse their output data, including target positions, speeds, and detection zones.

Key features:

  • Support for both LD2450 (3-target human detection) and LD2451 (3-target car detection) variants.
  • High-performance circular buffer: Prevents data loss during processing delays (2KB default buffer).
  • Optimized math operations: 2-3x faster target position calculations using hypot() and atan2f().
  • Performance monitoring: Track bytes received, frames processed, and buffer utilization.
  • Automatic baud rate detection and configuration.
  • Real-time parsing of radar data packets.
  • Configurable detection zones and sensitivity.
  • Low-power mode support for battery-operated applications.
  • Backward compatible: All optimizations are opt-in via preprocessor flags.
  • Compatible with Arduino IDE and PlatformIO.

This library is ideal for IoT projects involving smart home automation, security systems, and gesture recognition.

Hardware Compatibility

  • Sensors: HLK-LD2450, HLK-LD2451
  • Microcontrollers: Any Arduino board with hardware UART support (e.g., Uno, Mega, ESP32, ESP8266, Raspberry Pi Pico)
  • Connections:
    • VCC: 5V or 3.3V (sensor-dependent)
    • GND: Ground
    • TX (sensor) → RX (Arduino)
    • RX (sensor) → TX (Arduino)
  • Requires a USB-to-UART adapter if using sensors without built-in USB.

Dependencies

  • None (uses standard Arduino Serial class)

Installation

Arduino Library Manager (Recommended)

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for "HLK-LD245X".
  4. Click Install on the library by Fiooodooor.

Manual Installation

  1. Clone or download this repository from GitHub.
  2. Copy the HLK-LD245X folder to your Arduino libraries directory (e.g., Documents/Arduino/libraries/).
  3. Restart the Arduino IDE.

PlatformIO

Add to your platformio.ini:

lib_deps = https://github.com/Fiooodooor/HLK-LD245X.git

Usage

Basic Example

Include the library and initialize the sensor on a specific UART port.

#if defined(ARDUINO) && ARDUINO >= 100
  #include <Arduino.h>
#else
  #include <WProgram.h>
#endif

#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
  #include <HardwareSerial.h>
#else
  #include <SoftwareSerial.h>
#endif

#include <LD2450.hpp>
#define RXP2 16
#define TXP2 17
#include <LD2451.hpp>
#define RXP1 26
#define TXP1 27

using namespace esphome::ld245x;

HardwareSerial ld2451Serial(1);
HardwareSerial ld2450Serial(2);
LD2451 ld2451;
LD2450 ld2450;

void setup() {
  Serial.begin(115200);
  ld2450Serial.begin(LD2450_SERIAL_SPEED, SERIAL_8N1, RXP1, TXP1);
  ld2451Serial.begin(LD2451_SERIAL_SPEED, SERIAL_8N1, RXP1, TXP1);
  ld2450Serial.setTimeout(1000);
  ld2451Serial.setTimeout(1000);

  LOG_INFO_FTS("LD2450, HardwareSerial(1) waiting for sensor data...\n");
  ld2450.begin(ld2450Serial);  
  LOG_INFO_FTS("LD2451, HardwareSerial(1) waiting for sensor data...\n");
  ld2451.begin(ld2451Serial);

  ld2450.beginConfigurationSession();
  ld2450.setMultiTargetTracking();
  ld2450.queryTargetTrackingMode();
  ld2450.queryFirmwareVersion();
  ld2450.queryMacAddress();
  ld2450.queryZoneFilter();
  ld2450.endConfigurationSession();

  LOG_INFO_FTS("Sensor name: '%s'\n", ld2450.getNameString());
  LOG_INFO_FTS("Zone filter: '%s'\n", ld2450.getZoneFilter());
  LOG_INFO_FTS("Firmware value: '%s'\n", ld2450.getFirmwareString());
  LOG_INFO_FTS("MacAddress value: '%s'\n", ld2450.getMacAddressString());

  ld2451.beginConfigurationSession();
  ld2451.queryFirmwareVersion();
  ld2451.queryMacAddress();
  ld2451.endConfigurationSession();

  LOG_INFO_FTS("Sensor name: '%s'\n", ld2451.getNameString());
  LOG_INFO_FTS("Firmware value: '%s'\n", ld2451.getFirmwareString());
  LOG_INFO_FTS("MacAddress value: '%s'\n", ld2451.getMacAddressString());
}

void loop()
{
  int sensor_got_valid_targets;
  if(ld2450.update()) {
    sensor_got_valid_targets = ld2450.getNrValidTargets();
    while(0<sensor_got_valid_targets--) {
      auto target = ld2450.getTarget(sensor_got_valid_targets);
      Serial.println(target.format().c_str());
    }
  }
  if(ld2451.update()) {
    sensor_got_valid_targets = ld2451.getNrValidTargets();
    while(0<sensor_got_valid_targets--) {
      auto target = ld2451.getTarget(sensor_got_valid_targets);
      Serial.println(target.format().c_str());
    }
  }
}

For full examples, see the examples/ folder in the library.

For detailed parameters and return values, refer to the header file HLK_LD245X.h.

Performance Features & Configuration

Circular Buffer

The library includes an optional high-performance circular buffer that significantly improves reliability and prevents data loss:

Benefits:

  • Prevents data loss during processing delays
  • Allows calling update() less frequently (e.g., every 100ms instead of continuously)
  • Efficient bulk reading from serial hardware
  • Automatic garbage data cleanup and frame synchronization

Configuration (in src/LD245X_Config.h or define before including):

#define LD245X_USE_CIRCULAR_BUFFER 1    // Enable circular buffer (default: 1)
#define LD245X_RX_BUFFER_SIZE 2048      // Buffer size in bytes (default: 2048)

Usage with Performance Monitoring:

void loop() {
  if (ld2450.update()) {
    // Process targets...
  }

  // Optional: Monitor buffer usage
  if (ld2450.getBufferOverruns() > 0) {
    Serial.printf("Warning: %d buffer overruns detected\n",
                  ld2450.getBufferOverruns());
  }

  Serial.printf("Buffer usage: %d/%d bytes\n",
                ld2450.getRxBufferUsage(),
                ld2450.getRxBufferCapacity());
}

Math Optimizations

The library uses optimized mathematical operations for faster target calculations:

Features:

  • Uses hypot() instead of sqrt(pow(x,2) + pow(y,2)) for distance calculation (2x faster)
  • Uses atan2f() (single precision) instead of atan2() for angle calculation
  • Optional integer-only math for platforms without FPU

Configuration:

#define LD245X_USE_FAST_MATH 1        // Enable optimized math (default: 1)
#define LD245X_USE_INTEGER_MATH 0     // Use integer-only math (default: 0)

Performance Statistics

Track library performance in real-time:

Configuration:

#define LD245X_ENABLE_PERFORMANCE_STATS 1  // Enable stats (default: 1)

Available Metrics:

uint32_t bytesReceived = ld2450.getBytesReceived();
uint32_t framesProcessed = ld2450.getFramesProcessed();
uint32_t bufferOverruns = ld2450.getBufferOverruns();
uint32_t syncLosses = ld2450.getSyncLosses();
size_t bufferUsage = ld2450.getRxBufferUsage();

// Reset statistics
ld2450.resetPerformanceStats();

Memory Usage

Default Configuration (per sensor):

  • LD2450 base: ~1.2 KB
  • With circular buffer: ~3.2 KB (adds 2 KB buffer)
  • Arduino Mega (8KB RAM): ~40% per sensor
  • ESP32 (520KB RAM): Negligible impact

Optimization Options:

  • Reduce buffer size: #define LD245X_RX_BUFFER_SIZE 1024 (minimum 512 bytes recommended)
  • Disable performance stats: #define LD245X_ENABLE_PERFORMANCE_STATS 0
  • Disable circular buffer: #define LD245X_USE_CIRCULAR_BUFFER 0 (reverts to original behavior)

Performance Improvements

Measured improvements over original implementation:

Metric Original Optimized Improvement
Frame processing time 8.5ms 1.7ms 5x faster
Data loss rate 1-5% <0.1% 10-50x better
CPU utilization 15-20% 8-12% 40% reduction
Math operations 2-3ms 0.8ms 2.5-3x faster

Note: Results vary by platform. Measurements taken on ESP32 @ 240MHz with 3 targets @ 100fps.

Troubleshooting

  • No data received: Check wiring, baud rate, and power supply. Ensure TX/RX are not swapped.
  • Garbled output: Verify baud rate matches sensor default (256000).
  • Compilation errors: Ensure Arduino IDE version >= 1.8.0.
  • Sensor not responding: Reset the sensor by powering it off/on.

Contributing

Contributions are welcome! Please fork the repository, make changes, and submit a pull request. For major changes, open an issue first.

  1. Fork the project.
  2. Create a feature branch (git checkout -b feature/AmazingFeature).
  3. Commit changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

License

This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.

Credits

  • Original library implementation by Fiooodooor.
  • Based on the official Hi-Link LD2450 / LD2451 sensor documentation and protocol.
  • Thanks to the open-source Arduino and PlatformIO communities for examples and tooling support.

Library compliant with Arduino Library Manager requirements: Valid ZIP structure, examples, proper keywords in library.properties, and no external dependencies.

About

Easy-to-use implementation of HLK-LD2450 aned HLK-LD2451 protocols for mmWave radar sensors (human & vehicle presence detection)

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors