A comprehensive ESP-IDF component for interfacing with the AI-Thinker RD-03D radar sensor module, featuring advanced target detection, position tracking, and intelligent retention filtering.
- Real-time Target Detection: Continuous monitoring of moving targets
- Position Tracking: X/Y coordinates, distance, angle, and speed measurements
- Intelligent Retention System: Configurable filtering to reduce false positives/negatives
- Human-Readable Descriptions: Automatic generation of position descriptions
- Raw Data Access: Access to both filtered and unfiltered sensor data
- Diagnostic Tools: Built-in functions for monitoring sensor state and performance
- ESP32 or compatible microcontroller
- AI-Thinker RD-03D radar sensor module
- UART connection (RX/TX pins)
idf.py add-dependency "heronet/esp_rd-03d"- Clone this repository into your project's
componentsdirectory - Include the component in your main CMakeLists.txt
#include "esp_rd-03d.h"
radar_sensor_t radar;
void app_main() {
// Initialize the radar sensor
esp_err_t ret = radar_sensor_init(&radar, UART_NUM_2, GPIO_NUM_16, GPIO_NUM_17);
if (ret != ESP_OK) {
ESP_LOGE("MAIN", "Failed to initialize radar sensor");
return;
}
// Start UART communication
ret = radar_sensor_begin(&radar, 256000);
if (ret != ESP_OK) {
ESP_LOGE("MAIN", "Failed to start radar sensor");
return;
}
// Main loop
while (1) {
if (radar_sensor_update(&radar)) {
radar_target_t target = radar_sensor_get_target(&radar);
if (target.detected) {
ESP_LOGI("MAIN", "Target detected at (%.1f, %.1f) mm, distance: %.1f mm",
target.x, target.y, target.distance);
ESP_LOGI("MAIN", "Position: %s", target.position_description);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}esp_err_t radar_sensor_init(radar_sensor_t* sensor, uart_port_t uart_port, gpio_num_t rx_pin, gpio_num_t tx_pin);Initialize the radar sensor structure and configure pins.
Parameters:
sensor: Pointer to radar sensor structureuart_port: UART port number (e.g., UART_NUM_2)rx_pin: GPIO pin for UART RXtx_pin: GPIO pin for UART TX
Returns: ESP_OK on success, error code otherwise
esp_err_t radar_sensor_begin(radar_sensor_t* sensor, uint32_t baud_rate);Start UART communication with the specified baud rate.
Parameters:
sensor: Pointer to initialized radar sensorbaud_rate: Communication baud rate (typically 115200)
bool radar_sensor_update(radar_sensor_t* sensor);Process incoming UART data and update target information.
Returns: true if new data was processed, false otherwise
radar_target_t radar_sensor_get_target(radar_sensor_t* sensor);Get the current filtered target data (with retention applied).
radar_target_t radar_sensor_get_raw_target(radar_sensor_t* sensor);Get the raw unfiltered target data directly from the sensor.
typedef struct {
bool detected; // Target detection status
float x; // X coordinate (mm)
float y; // Y coordinate (mm)
float speed; // Target speed
float distance; // Distance from sensor (mm)
float angle; // Angle in degrees
char position_description[64]; // Human-readable position
} radar_target_t;The retention system helps filter out false positives and negatives by applying configurable delays before changing detection state.
// Set custom retention times
radar_sensor_set_retention_times(&radar, 5000, 2000); // 5s detection, 2s absence
// Enable/disable retention
radar_sensor_enable_retention(&radar, true);
// Reset retention state
radar_sensor_reset_retention(&radar);- Detection Retention: 3000ms (3 seconds)
- Absence Retention: 1000ms (1 second)
The component automatically generates human-readable position descriptions:
- Horizontal: "Far Left", "Left", "Near Left", "Center", "Near Right", "Right", "Far Right"
- Vertical: "Far Behind", "Behind", "Near Behind", "Center", "Near Forward", "Forward", "Far Forward"
- Distance: "Very Close", "Close", "Medium", "Far", "Very Far"
Example outputs:
- "Left Forward - Medium (1.5m)"
- "Far Right Behind - Close (0.8m)"
- "Directly at sensor (0.3m)"
// Check if retention is currently active
bool is_active = radar_sensor_is_retention_active(&radar);
// Get timing information
uint32_t time_since_detection = radar_sensor_get_time_since_last_detection(&radar);
uint32_t time_since_absence = radar_sensor_get_time_since_last_absence(&radar);void configure_radar_for_security() {
radar_sensor_t radar;
// Initialize radar
radar_sensor_init(&radar, UART_NUM_2, GPIO_NUM_16, GPIO_NUM_17);
radar_sensor_begin(&radar, 115200);
// Configure for security application (longer retention)
radar_sensor_set_retention_times(&radar, 10000, 500); // 10s detection, 0.5s absence
while (1) {
radar_sensor_update(&radar);
radar_target_t target = radar_sensor_get_target(&radar);
if (target.detected) {
// Security alert logic
ESP_LOGW("SECURITY", "Intruder detected: %s", target.position_description);
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}void compare_filtered_vs_raw() {
radar_target_t filtered = radar_sensor_get_target(&radar);
radar_target_t raw = radar_sensor_get_raw_target(&radar);
ESP_LOGI("COMPARE", "Filtered: %s, Raw: %s",
filtered.detected ? "DETECTED" : "CLEAR",
raw.detected ? "DETECTED" : "CLEAR");
if (radar_sensor_is_retention_active(&radar)) {
ESP_LOGI("COMPARE", "Retention is active - states differ");
}
}The AI-Thinker RD-03D uses a coordinate system where:
- X-axis: Positive values = Left, Negative values = Right
- Y-axis: Positive values = Forward, Negative values = Behind
- Distance: Calculated as √(x² + y²)
- Angle: Measured in degrees, adjusted for sensor orientation
esp_err_t ret = radar_sensor_init(&radar, UART_NUM_2, GPIO_NUM_16, GPIO_NUM_17);
switch (ret) {
case ESP_OK:
ESP_LOGI("RADAR", "Initialization successful");
break;
case ESP_ERR_INVALID_ARG:
ESP_LOGE("RADAR", "Invalid arguments provided");
break;
default:
ESP_LOGE("RADAR", "Initialization failed: %s", esp_err_to_name(ret));
break;
}- Call
radar_sensor_update()regularly (recommended: 50-100ms intervals) - The retention system processes data even when no new sensor data arrives
- UART buffer size is optimized for the RD-03D frame format
- Position descriptions are updated automatically when target data changes
- Verify UART connections (RX/TX pins)
- Check baud rate configuration (115200 is standard)
- Ensure adequate power supply to the radar module
- Verify the radar module is configured for the correct output format
- Adjust retention times based on your application needs
- Use
radar_sensor_get_raw_target()to debug sensor behavior - Check the mounting and orientation of the radar module
- Verify GPIO pin assignments
- Check for conflicting UART usage
- Monitor ESP_LOGE messages for UART driver errors
This component is provided under the MIT License. See LICENSE file for details.
Contributions are welcome! Please submit pull requests or issues through the GitHub repository.
- AI-Thinker RD-03D Datasheet: Official Technical Documentation
- Arduino Implementation Guide: Detect and Track Humans with mmWave Radar on Arduino - Core Electronics tutorial showing similar implementation concepts
For technical support and questions:
- GitHub Issues: Repository Issues
- ESP32 Community Forums
- Component Registry: heronet/esp_rd-03d