My Project
Loading...
Searching...
No Matches
LD2410S Class Reference

Class to communicate and configure the LD2410S radar sensor. More...

#include <HLK-LD2410S.h>

Public Member Functions

 LD2410S (HardwareSerial &serial, int rxPin, int txPin)
 Constructor.
 ~LD2410S ()
 Destructor.
bool begin (uint32_t baudrate=115200)
 Initialize serial communication and enter config mode.
void loop ()
 Read and process incoming frames (non-blocking)
bool enterConfigMode ()
 Enter configuration mode.
bool exitConfigMode ()
 Exit configuration mode.
bool switchToStandardMode ()
 Switch to standard output mode (detailed)
bool switchToMinimalMode ()
 Switch to minimal output mode (shorter packets)
bool readFirmwareVersion (uint16_t &major, uint16_t &minor, uint16_t &patch)
 Read firmware version of the sensor.
bool readCommonParametersCommand (uint32_t &farthest_distance, uint32_t &nearest_distance, uint32_t &delay_time, uint32_t &freq_status, uint32_t &freq_distance, uint32_t &respond_speed)
 Read configuration parameters from sensor.
bool readSerialNumber (char *buffer, size_t len)
 Read 8-byte serial number.
bool isMotionDetected () const
 Check if motion is currently detected.
uint16_t getDistance () const
 Get target distance.

Detailed Description

Class to communicate and configure the LD2410S radar sensor.

Constructor & Destructor Documentation

◆ LD2410S()

LD2410S::LD2410S ( HardwareSerial & serial,
int rxPin,
int txPin )

Constructor.

Constructs an LD2410S object with the specified serial interface and pin configuration.

Parameters
serialReference to HardwareSerial object
rxPinGPIO used for sensor TX
txPinGPIO used for sensor RX

Initializes the LD2410S sensor driver, associating it with the provided HardwareSerial instance and configuring the RX and TX pins for communication.

Parameters
serialReference to a HardwareSerial object used for UART communication with the sensor.
rxPinGPIO pin number used for receiving data from the sensor.
txPinGPIO pin number used for transmitting data to the sensor.

◆ ~LD2410S()

LD2410S::~LD2410S ( )
default

Destructor.

Destructor for the LD2410S class.

Cleans up any resources allocated by the LD2410S instance. As this destructor is defaulted, no custom cleanup is performed.

Member Function Documentation

◆ begin()

bool LD2410S::begin ( uint32_t baudrate = 115200)

Initialize serial communication and enter config mode.

Initializes the LD2410S sensor and configures its communication parameters.

Parameters
baudrateSerial baudrate (default = 115200)
Returns
true if successful

This function sets up the serial communication with the specified baud rate and pin configuration, attempts to enter configuration mode (with up to two retries), writes generic parameters to the sensor, and switches the sensor to minimal data output mode. Delays are used to ensure proper timing between operations.

Parameters
baudrateThe baud rate for serial communication with the LD2410S sensor.
Returns
true if initialization and configuration succeed; false otherwise.

◆ enterConfigMode()

bool LD2410S::enterConfigMode ( )

Enter configuration mode.

Sends the command to enter configuration mode on the LD2410S device.

Returns
true if success

This function transmits a specific command frame to the LD2410S device via the serial interface to request entry into configuration mode. It waits for up to 500 milliseconds for an 18-byte acknowledgment frame from the device. The function checks specific bytes in the response to verify successful entry into configuration mode.

Returns
true if the device successfully enters configuration mode and the expected acknowledgment is received; false otherwise.

◆ exitConfigMode()

bool LD2410S::exitConfigMode ( )

Exit configuration mode.

Exits the configuration mode of the LD2410S device.

Returns
true if success

Sends the appropriate command to the device to exit configuration mode and waits for an acknowledgment. The function waits up to 500 milliseconds for a response from the device. If a valid acknowledgment frame is received, it logs a success message and returns true. Otherwise, it returns false.

Returns
true if the device successfully exits configuration mode, false otherwise.

◆ getDistance()

uint16_t LD2410S::getDistance ( ) const

Get target distance.

Retrieves the last measured distance value.

Returns
Distance in cm

This method returns the most recent distance measurement obtained by the LD2410S sensor.

Returns
The distance value in units defined by the sensor (typically millimeters).

◆ isMotionDetected()

bool LD2410S::isMotionDetected ( ) const

Check if motion is currently detected.

Checks if motion has been detected by the LD2410S sensor.

Returns
true if motion detected
true if motion is currently detected; false otherwise.

◆ loop()

void LD2410S::loop ( )

Read and process incoming frames (non-blocking)

read data serial continously

Processes incoming serial data to detect and parse minimal data frames from the LD2410S sensor.

This function should be called repeatedly (typically in the main loop) to read bytes from the serial interface. It accumulates bytes into a buffer when a frame start byte (0x6E) is detected, and continues until a frame end byte (0x62) is found or the maximum frame size is reached. Upon detecting a complete frame, it logs the frame contents and invokes the minimal frame parser.

The function is non-blocking and processes all available bytes in the serial buffer during each call.

Note
The function uses static variables to maintain state between calls, allowing it to handle frames that span multiple invocations.

◆ readCommonParametersCommand()

bool LD2410S::readCommonParametersCommand ( uint32_t & farthest_distance,
uint32_t & nearest_distance,
uint32_t & delay_time,
uint32_t & freq_status,
uint32_t & freq_distance,
uint32_t & respond_speed )

Read configuration parameters from sensor.

Reads the common parameters from the LD2410S sensor.

Parameters
farthest_distanceMax detection distance
nearest_distanceMin detection distance
delay_timeDelay before entering static state
freq_statusReport frequency for motion/static state
freq_distanceReport frequency for distance
respond_speedSensor response speed (normal/fast)
Returns
true if successful

This function sends a command to the LD2410S sensor to request its common parameters, waits for a response, and parses the returned data. The parameters retrieved include the farthest and nearest detection distances, delay time, frequency status, frequency distance, and response speed. The function returns true if the parameters are successfully read and parsed, otherwise returns false.

Parameters
[out]farthest_distanceThe farthest detection distance reported by the sensor.
[out]nearest_distanceThe nearest detection distance reported by the sensor.
[out]delay_timeThe delay time parameter from the sensor.
[out]freq_statusThe frequency status parameter from the sensor.
[out]freq_distanceThe frequency distance parameter from the sensor.
[out]respond_speedThe response speed parameter from the sensor.
Returns
true if the parameters were successfully read and parsed, false otherwise.

◆ readFirmwareVersion()

bool LD2410S::readFirmwareVersion ( uint16_t & major,
uint16_t & minor,
uint16_t & patch )

Read firmware version of the sensor.

Reads the firmware version from the LD2410S device.

Parameters
majorMajor version
minorMinor version
patchPatch version
Returns
true if response received

This function sends a command to the LD2410S device to request its firmware version. It waits for a response and parses the major, minor, and patch version numbers from the received data.

Parameters
[out]majorReference to a uint16_t variable where the major version will be stored.
[out]minorReference to a uint16_t variable where the minor version will be stored.
[out]patchReference to a uint16_t variable where the patch version will be stored.
Returns
true if the firmware version was successfully read and parsed, false otherwise.

◆ readSerialNumber()

bool LD2410S::readSerialNumber ( char * buffer,
size_t len )

Read 8-byte serial number.

Reads the serial number from the LD2410S device via the serial interface.

Parameters
bufferBuffer to store null-terminated serial number
lenBuffer size (must be at least 9)
Returns
true if successful

This function sends a command to the LD2410S device to request its serial number, waits for the response, and extracts the serial number from the received data. The serial number is expected to be 8 bytes long and will be stored as a null-terminated string in the provided buffer.

The function first clears the serial buffer, sends the serial number request command, and waits up to 1 second for a valid response. If a valid response is received, the serial number is extracted and copied into the buffer.

Parameters
[out]bufferPointer to a character array where the serial number will be stored. The buffer must be at least 9 bytes long to accommodate the 8-byte serial number and the null terminator.
[in]lenThe length of the provided buffer.
Returns
true if the serial number was successfully read and stored in the buffer, false otherwise (e.g., if the buffer is too small or a valid response was not received).

◆ switchToMinimalMode()

bool LD2410S::switchToMinimalMode ( )

Switch to minimal output mode (shorter packets)

Switches the LD2410S device to minimal output mode.

Returns
true if success

This function sends a command frame to the LD2410S device to change its output mode to minimal. It writes the appropriate command and parameters to the serial interface, then waits for an acknowledgment response from the device. The function checks the response for specific header and command bytes to confirm the mode change was successful.

Returns
true if the device successfully switches to minimal output mode, false otherwise.

◆ switchToStandardMode()

bool LD2410S::switchToStandardMode ( )

Switch to standard output mode (detailed)

Switches the LD2410S device to standard output mode.

Returns
true if success

This function sends the appropriate command sequence to the LD2410S device over the configured serial interface to switch it into standard data output mode. It waits for an acknowledgment frame from the device to confirm the mode change.

Note
The function waits up to 1 second for the acknowledgment response.
Returns
true if the device successfully switches to standard output mode, false otherwise.

The documentation for this class was generated from the following files: