MicroPython driver for HLK-LD2410C human presence radar sensor
Project description
micropython-ld2410
A MicroPython driver for the HLK-LD2410 human presence radar sensor.
Features
- Robust Protocol Parsing: Implements the frame header/footer checks and checksum validation from the Rust crate.
- MicroPython Optimized: Uses
readintoandmemoryviewwhere appropriate to minimize memory fragmentation. - Full Control: Supports reading presence data, engineering mode, firmware version, and configuration commands.
- Zero Dependencies: Pure MicroPython implementation.
Installation
Using mip (Recommended)
To install the library on your device using mpremote:
mpremote mip install github:melastmohican/micropython-ld2410
Manual Installation
Copy the src/ld2410 directory to the lib folder on your MicroPython device.
Usage
import time
from machine import UART, Pin
from ld2410 import LD2410
# Initialize UART
# Note: LD2410 default baudrate is 256000
# Raspberry Pi Pico UART1 default: TX=Pin(8), RX=Pin(9)
uart = UART(1, baudrate=256000, tx=Pin(8), rx=Pin(9))
# Create driver instance
sensor = LD2410(uart)
print("Starting sensor monitoring...")
# Optional: Read firmware version
try:
sensor.enable_config_mode()
version = sensor.read_firmware_version()
print("Firmware Version:", version)
sensor.exit_config_mode()
except Exception as e:
print("Failed to read firmware:", e)
# Main loop
while True:
try:
# Blocks until a valid presence frame is received
data = sensor.read_presence()
if data:
print(f"Target: {data.target_state}, Dist: {data.detection_distance_cm}cm")
print(f" Moving: {data.moving_distance_cm}cm (Energy: {data.moving_energy})")
print(f" Still: {data.still_distance_cm}cm (Energy: {data.still_energy})")
except Exception as e:
print("Error reading sensor:", e)
time.sleep(1)
API Reference
class LD2410(uart)
Initialize with a MicroPython machine.UART object.
read_presence(): Reads the next valid presence report. Skips engineering frames. ReturnsPresenceData.enter_config_mode(): Sends command to enter configuration mode.exit_config_mode(): Sends command to exit configuration mode.enable_engineering_mode(): Enables engineering mode output (more detailed data).disable_engineering_mode(): Disables engineering mode.read_firmware_version(): Reads the firmware version (requires config mode).restart(): Restarts the module.
class PresenceData
Data structure for sensor reports.
target_state: One ofTargetState.NO_TARGET,MOVING,STATIONARY,BOTH.moving_distance_cm: Distance to moving target (cm).moving_energy: Energy of moving target (0-100).still_distance_cm: Distance to stationary target (cm).still_energy: Energy of stationary target (0-100).detection_distance_cm: Overall detection distance (cm).
class TargetState
Constants for target status:
NO_TARGET(0x00)MOVING(0x01)STATIONARY(0x02)BOTH(0x03)
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file micropython_ld2410-0.1.0.tar.gz.
File metadata
- Download URL: micropython_ld2410-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf484035743eda7976ef7563c94b0d006f34b632825bfec3ae202282697c2274
|
|
| MD5 |
75325e1cbe90b761f0bffc569acdcc10
|
|
| BLAKE2b-256 |
10ce6b0dbc041f068e34ec8e3e6fb8d1c4fd33852205ea7ff0c6b345983b849f
|
File details
Details for the file micropython_ld2410-0.1.0-py3-none-any.whl.
File metadata
- Download URL: micropython_ld2410-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
840a61571806b0aad6404736eef819069e48f5117ec368a55686791c422e6ce3
|
|
| MD5 |
a099e935668e043705b259a4f1147d9c
|
|
| BLAKE2b-256 |
23016643bb44c70afd303db4cbbe6dddb96515b185c653db40d91e666be57de2
|