HLK LD2420
Posted: Wed Jan 01, 2025 8:14 pm
Hi there,
i ordered some HLK LD2420 presence sensors from AliExpress (https://www.aliexpress.com/item/1005005 ... ry_from%3A), now i try to get them to work using my ESP32 DevKit (I think V2, but not 100% sure from here https://www.az-delivery.de/products/esp ... pmentboard).
Im using PlatformIO or Arduino IDE
I was going to use a 3rd party library: https://github.com/Bolukan/ld2420
but i cant get it to work.
here is my code:
Excuse the delay() those are only for debug purposes.
but i cant get the communication to work i always get
"Failed to initialize LD2420."
I tried switching RX/TX(OT1) and even RX/OT1/OT2, BaudRates between 115200 and 256000, and exchanged the sensors (since I got 5) nothing works.
The sensor is supplied with 3.1V which should be enough since its accepting 3.0-3.6V. All pins Output 3.1V so i guess its wired correctly.
Does anyone have experience with the LD2420? And can help me out/push me in the right direction?
Thanks in advance.
i ordered some HLK LD2420 presence sensors from AliExpress (https://www.aliexpress.com/item/1005005 ... ry_from%3A), now i try to get them to work using my ESP32 DevKit (I think V2, but not 100% sure from here https://www.az-delivery.de/products/esp ... pmentboard).
Im using PlatformIO or Arduino IDE
I was going to use a 3rd party library: https://github.com/Bolukan/ld2420
but i cant get it to work.
here is my code:
Code: Untitled.cpp Select all
#include <Arduino.h>
#include <ld2410.h>
#define STATUS_LED 25
#define RX_PIN 33
#define TX_PIN 32
#define OT2_PIN 27
ld2410 sensor;
uint8_t statusPinState = HIGH;
unsigned long lastStatusLedToggle = 0;
const unsigned long toogleInterval = 3000;
unsigned long lastReadSensor = 0;
const unsigned long readSensorInterval = 1000;
void toggleStatusLED();
void readSensor();
void setup()
{
Serial.begin(115200);
Serial2.begin(256000, SERIAL_8N1, RX_PIN, TX_PIN);
delay(100); // Add delay after Serial2 initialization
pinMode(OT2_PIN, INPUT);
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, HIGH);
Serial.println("starting");
if (sensor.begin(Serial2))
{
// Enable debugging
sensor.debug(Serial2); // Request firmware version and current configuration
sensor.requestFirmwareVersion();
sensor.requestCurrentConfiguration();
Serial.println("LD2420 initialized successfully!");
// sensor.setsetSensitivity(5); // Setzt die Empfindlichkeit (1-9)
// sensor.setDistanceRange(0, 500); // Setzt den Distanzbereich (0-500 cm)
// sensor.setVelocityRange(0, 200); // Setzt den Geschwindigkeitsbereich (0-200 cm/s)
Serial.println("LD2420 configured successfully!");
}
else
{
Serial.println("Failed to initialize LD2420.");
}
delay(3000);
}
void loop()
{
Serial.println("loop");
unsigned long now = millis();
sensor.read();
// toggle status led
if (now - lastStatusLedToggle >= toogleInterval)
{
toggleStatusLED();
lastStatusLedToggle = now;
//Serial.println("switchLED");
}
if (now - lastReadSensor >= readSensorInterval)
{
readSensor();
lastReadSensor = now;
}
delay(500);
}
void toggleStatusLED()
{
Serial.println("toggle");
if (statusPinState == HIGH)
{
statusPinState = LOW;
digitalWrite(STATUS_LED, LOW);
}
else
{
statusPinState = HIGH;
digitalWrite(STATUS_LED, HIGH);
}
}
void readSensor()
{
// Check connection
if (sensor.isConnected())
{
Serial.println("LD2420 is connected and sending data."); // Presence detection
}
else
{
Serial.println("LD2420 is not connected.");
}
if (sensor.presenceDetected())
{
Serial.println("Presence detected!"); // Stationary target detection
if (sensor.stationaryTargetDetected())
{
Serial.print("Stationary Target Distance: ");
Serial.println(sensor.stationaryTargetDistance());
Serial.print("Stationary Target Energy: ");
Serial.println(sensor.stationaryTargetEnergy());
} // Moving target detection
if (sensor.movingTargetDetected())
{
Serial.print("Moving Target Distance: ");
Serial.println(sensor.movingTargetDistance());
Serial.print("Moving Target Energy: ");
Serial.println(sensor.movingTargetEnergy());
}
}
int ot2_state = digitalRead(OT2_PIN);
Serial.print("OT2: ");
Serial.println(ot2_state);
}
but i cant get the communication to work i always get
"Failed to initialize LD2420."
I tried switching RX/TX(OT1) and even RX/OT1/OT2, BaudRates between 115200 and 256000, and exchanged the sensors (since I got 5) nothing works.
The sensor is supplied with 3.1V which should be enough since its accepting 3.0-3.6V. All pins Output 3.1V so i guess its wired correctly.
Does anyone have experience with the LD2420? And can help me out/push me in the right direction?
Thanks in advance.