RD03Radar is a comprehensive Arduino library for the Ai-Thinker RD-03 24GHz mmWave radar sensor. Transform a cheap $5 radar sensor into an intelligent presence detection system with motion-based entry detection, automated lighting control, and advanced watchdog protection.
| Feature | RD03Radar | Commercial Solutions |
|---|---|---|
| Cost | $15-20 | $50-200+ |
| Open Source | โ Yes | โ No |
| Customizable | โ Fully | |
| Community Support | โ Active | โ None |
| Motion Detection | โ Advanced | โ Basic |
๐ฐ Save 80% compared to commercial presence sensors!
| ๐ Smart Detection | ๐๏ธ Fully Configurable | ๐ก๏ธ Bulletproof Reliability |
|---|---|---|
| Motion-based entry detection | Adjustable range (20-600cm) | Advanced watchdog protection |
| Real-time distance monitoring | Sensitivity levels (1-5) | Auto-recovery from failures |
| Multiple detection algorithms | Customizable timeouts | Error handling & diagnostics |
| ๐ Flexible Control | ๐ก IoT Ready | ๐ Developer Friendly |
|---|---|---|
| Automatic, Manual, Force modes | Callback-based architecture | Easy Arduino integration |
| Light control integration | Event-driven programming | Comprehensive documentation |
| Safety timeout protection | Multi-platform support | Open source & MIT licensed |
- ๐ Smart Home Automation - Bathroom lights, security systems
- ๐ Security Systems - Intruder detection, motion alerts
- ๐ก Lighting Control - Automated room lighting
- ๐ค IoT Projects - Custom presence-based applications
- ๐ญ Industrial - Occupancy detection, automation
// Connect to MQTT broker
radar.setupMQTT("192.168.1.100", 1883);
radar.connectMQTT();
// Publish status automatically
radar.publishStatus(); // JSON: {"presence":true,"distance":125.5,"state":"PRESENCE_DETECTED"}// Start web server on port 80
radar.setupWebServer(80);
radar.startWebServer();
// Access at: http://esp-ip/
// API: http://esp-ip/api/status- Multi-Protocol Support: MQTT + Web Server simultaneously
- Automatic Reconnection: Smart retry logic for network issues
- REST API: JSON endpoints for status and control
- Real-time Monitoring: Live status updates
- Remote Control: Control via MQTT commands or web interface
MQTT_Example.ino- Complete MQTT integrationWebServer_Example.ino- Web interface demoFull_Features_Example.ino- Combined capabilities
# Arduino IDE (Recommended)
1. Sketch โ Include Library โ Manage Libraries
2. Search "RD03Radar" โ Install
# Or download ZIP and install manuallyESP32 / ESP8266 RD-03 Radar LED
---------------- ----------- ---
GPIO16/12 โโโโโโ TX
GPIO17/14 โโโโโโ RX
GND โโโโโโโโโโโโ GND
5V โโโโโโโโโโโโโ VCC
GPIO2 โโโโโโโโโโ LED+ (optional)
#include <RD03Radar.h>
RD03Radar radar(Serial2, radarConfig);
void onPresenceChange(RD03PresenceState state, float distance) {
if (state == RD03PresenceState::PRESENCE_DETECTED) {
digitalWrite(LED_BUILTIN, HIGH); // Turn on light
Serial.println("Presence detected!");
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn off light
Serial.println("No presence");
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
radar.onPresenceChange(onPresenceChange);
radar.begin(16, 17);
}
void loop() {
radar.loop();
}๐ That's it! Your smart presence detector is ready!
# Arduino IDE Library Manager
1. Sketch โ Include Library โ Manage Libraries
2. Search "PubSubClient" โ Install// Add to your code after WiFi setup
radar.setupMQTT("192.168.1.100", 1883); // MQTT broker IP and port
radar.connectMQTT();
// Publish status every 30 seconds
if (millis() - lastPublish > 30000) {
radar.publishStatus();
}- Published:
rd03radar/status- JSON status updates - Subscribed:
rd03radar/commands- Control commands
# Send via MQTT to rd03radar/commands
automatic # Switch to automatic mode
force_on # Force lights ON
force_off # Force lights OFF
reset # Reset presence detection// Add to setup() after WiFi connection
radar.setupWebServer(80); // Port 80
radar.startWebServer();
// Print local IP
Serial.print("Web server: http://");
Serial.println(WiFi.localIP());- Main Page:
http://esp-ip/- Control panel with buttons - API Status:
http://esp-ip/api/status- JSON status - API Control:
POST http://esp-ip/api/command- Send commands
// JavaScript example
fetch('/api/command', {
method: 'POST',
body: 'automatic' // or 'force_on', 'force_off', 'reset'
});- Open Arduino IDE
- Go to Sketch โ Include Library โ Manage Libraries
- Search for "RD03Radar"
- Click Install
- Download
RD03Radar_FINAL.zipfrom GitHub Releases - Open Arduino IDE
- Go to Sketch โ Include Library โ Add .ZIP Library
- Select the downloaded ZIP file
# Clone the repository
git clone https://github.com/gomgom-40/RD03Radar.git
# Copy to Arduino libraries folder
cp -r RD03Radar ~/Documents/Arduino/libraries/Add to platformio.ini:
lib_deps =
gomgom-40/RD03RadarFor a ready-to-use ESPHome configuration with advanced presence logic, safety timeouts, fast exit, and direct HA integration: ๐ https://github.com/gomgom-40/RD-03_presence_radar (Includes pre-compiled binary, demo GIF, and full documentation)
| Component | Specification | Cost |
|---|---|---|
| Microcontroller | ESP32, ESP8266, or Arduino with UART | $5-15 |
| RD-03 Radar | Ai-Thinker RD-03 24GHz mmWave Sensor | $5 |
| Power Supply | 5V/1A USB or DC adapter | $2-3 |
| Optional Relay | For controlling lights/appliances | $1-2 |
ESP32 RD-03 Radar Relay / LED
----- ----------- -----------
GPIO17 โโโโโโ TX
GPIO16 โโโโโโ RX
GND โโโโโโโโโ GND
5V โโโโโโโโโโ VCC
GPIO2 โโโโโโโ Control Pin (Optional)
ESP8266 RD-03 Radar Relay / LED
------- ----------- -----------
GPIO14 (D5) โโ TX
GPIO12 (D6) โโ RX
GND โโโโโโโโโโ GND
5V โโโโโโโโโโโ VCC
GPIO2 โโโโโโโโ Control Pin (Optional)
RD03Radar(HardwareSerial& serial, const RD03Config& config = RD03Config());bool begin(int rxPin, int txPin); // ESP32 with custom pins
bool begin(); // ESP8266 with SoftwareSerial
void end(); // Stop radar operationvoid setConfig(const RD03Config& config);
RD03Config getConfig() const;
void setRange(float minRange, float maxRange);
void setSensitivity(uint8_t sensitivity); // 1-5
void setHoldTime(uint16_t seconds);void setControlMode(RD03ControlMode mode);
void manualLightControl(bool turnOn);
void resetPresence();RD03PresenceState getPresenceState() const;
float getDistance() const;
RD03Status getStatus() const;
bool isOperational() const;
uint32_t getUptime() const;
uint32_t getLastActivityTime() const;void setupMQTT(const char* server, uint16_t port = 1883, const char* username = nullptr, const char* password = nullptr);
void connectMQTT();
void disconnectMQTT();
bool isMQTTConnected() const;
void publishStatus();
void subscribeCommands();
void setMQTTCallback(std::function<void(char*, uint8_t*, unsigned int)> callback);void setupWebServer(uint16_t port = 80);
void startWebServer();
void stopWebServer();
bool isWebServerRunning() const;void onPresenceChange(PresenceCallback callback);
void onStatusChange(StatusCallback callback);
void onDistanceMeasurement(DistanceCallback callback);
void onLightControl(LightControlCallback callback);struct RD03Config {
float minRange = 20.0f; // Minimum detection range (cm)
float maxRange = 500.0f; // Maximum detection range (cm)
uint8_t sensitivity = 3; // Sensitivity level (1-5)
uint16_t holdTime = 30; // Hold time (seconds)
uint16_t maxAbsenceTime = 300; // Safety timeout (seconds)
float motionThreshold = 2.0f; // Motion detection threshold (cm)
uint8_t motionHitsRequired = 1; // Motion detection sensitivity
uint32_t baudRate = 115200; // UART baud rate
uint8_t rxBufferSize = 256; // UART buffer size
};NO_PRESENCE- No presence detectedPRESENCE_DETECTED- Presence detectedMOTION_DETECTED- Motion detected (entry)MAINTAINING- Maintaining presenceFAST_EXIT- Fast exit (no target)SAFETY_TIMEOUT- Safety timeout reached
AUTOMATIC- Full automatic modeMANUAL_ON- Manual override ONFORCE_ON- Force ON overrideFORCE_OFF- Force OFF override
OK- Everything working normallyERROR- General errorNO_SIGNAL- No signal from radarBUFFER_OVERFLOW- UART buffer overflowINVALID_DATA- Invalid data receivedWATCHDOG_RESET- Watchdog initiated reset
#include <RD03Radar.h>
RD03Config bathroomConfig = {
.minRange = 30.0f,
.maxRange = 400.0f,
.sensitivity = 4,
.holdTime = 45,
.maxAbsenceTime = 180
};
RD03Radar radar(Serial2, bathroomConfig);
const int LIGHT_RELAY_PIN = 19;
const int MANUAL_SWITCH_PIN = 18;
void setup() {
pinMode(LIGHT_RELAY_PIN, OUTPUT);
pinMode(MANUAL_SWITCH_PIN, INPUT_PULLUP);
radar.onLightControl([](bool turnOn, const char* reason) {
digitalWrite(LIGHT_RELAY_PIN, turnOn ? HIGH : LOW);
Serial.printf("Light %s: %s\n", turnOn ? "ON" : "OFF", reason);
});
radar.begin(16, 17);
}
void loop() {
if (digitalRead(MANUAL_SWITCH_PIN) == LOW) {
radar.setControlMode(RD03ControlMode::MANUAL_ON);
} else {
radar.setControlMode(RD03ControlMode::AUTOMATIC);
}
radar.loop();
}#include <RD03Radar.h>
RD03Radar radar(Serial2);
void onPresenceChange(RD03PresenceState state, float distance) {
if (state == RD03PresenceState::MOTION_DETECTED) {
Serial.println("๐จ Intruder detected!");
// Send alert, turn on alarm, etc.
}
}
void setup() {
radar.onPresenceChange(onPresenceChange);
radar.begin(16, 17);
}
void loop() {
radar.loop();
}// Small room (office/bathroom)
radarConfig.minRange = 20.0f;
radarConfig.maxRange = 300.0f;
// Large room (living room)
radarConfig.minRange = 50.0f;
radarConfig.maxRange = 600.0f;
// Security application (wide area)
radarConfig.minRange = 100.0f;
radarConfig.maxRange = 800.0f;| Level | Description | Use Case |
|---|---|---|
| 1 | Most Sensitive | Quiet environments |
| 2 | Very Sensitive | Normal home use |
| 3 | Balanced | Default setting |
| 4 | Less Sensitive | Noisy environments |
| 5 | Least Sensitive | Industrial areas |
Hold Time = Sensitivity Level ร 10 seconds
Level 1: 10 seconds
Level 2: 20 seconds
Level 3: 30 seconds (default)
Level 4: 40 seconds
Level 5: 50 seconds
- Check UART connections (TXโRX crossed)
- Verify power supply (5V stable)
- Check baud rate settings
- Adjust detection range (minRange/maxRange)
- Check line-of-sight (no obstructions)
- Reduce sensitivity if environment is noisy
- Increase minRange to filter close objects
- Adjust motionThreshold
- Check for interference sources
- Increase sensitivity level
- Adjust holdTime
- Check maxAbsenceTime setting
void loop() {
radar.loop();
// Print debug info every 10 seconds
static unsigned long lastDebug = 0;
if (millis() - lastDebug > 10000) {
lastDebug = millis();
Serial.printf("Status: %s\n", radar.isOperational() ? "OK" : "ERROR");
Serial.printf("Distance: %.1f cm\n", radar.getDistance());
Serial.printf("Uptime: %lu seconds\n", radar.getUptime() / 1000);
}
}- Published:
rd03radar/status- JSON status updates{ "presence": true, "distance": 125.5, "state": "PRESENCE_DETECTED", "uptime": 3600, "operational": true } - Subscribed:
rd03radar/commands- Control commands"automatic"- Switch to automatic mode"force_on"- Force lights ON"force_off"- Force lights OFF"reset"- Reset presence detection
- GET
/- Main control interface (HTML page) - GET
/api/status- JSON status information - POST
/api/command- Execute control commands
- Power Consumption: 0.5-1W (radar only)
- Detection Range: 20cm - 600cm (configurable)
- Update Rate: 30-45ms per processing cycle
- UART Baud Rate: 115200 bps
- Memory Usage: ~2KB RAM + ~15KB Flash
- Supported Platforms: ESP32, ESP8266, Arduino Mega/Uno
Contributions are welcome! Please feel free to submit a Pull Request.
- Follow Arduino coding standards
- Add comprehensive documentation
- Test on multiple platforms
- Update examples when adding features
- Maintain backward compatibility
- Use C++11 features compatible with Arduino
- Follow Arduino naming conventions
- Add Doxygen-style comments for public APIs
This library is licensed under the MIT License - see the LICENSE file for details.
- Ai-Thinker - For the affordable RD-03 radar sensor
- ESPHome Community - For inspiration and testing
- Arduino Community - For the amazing platform
- GitHub Issues: For bugs and feature requests
- Documentation: Check README and examples first
- Community: Arduino forums and ESP32 communities
- MQTT integration for IoT connectivity
- Web server interface for local control
- REST API for status monitoring
- Real-time status publishing
- Remote control via MQTT/Web
- Multi-zone presence detection support
- Energy monitoring features
- Mobile app companion
- Additional radar sensor support
- Advanced calibration tools
- Home Assistant integration
- Data logging and analytics
- Camera integration for visual verification
- Machine learning for behavior analysis
- Multi-room coordination
- Commercial security features
Made with โค๏ธ by Mohamed Eid (gomgom-40)
Transforming budget sensors into intelligent solutions
- English - English documentation
- ุงูุนุฑุจูุฉ - ุงูุชูุซูู ุจุงููุบุฉ ุงูุนุฑุจูุฉ
โญ Star this repository if you find it useful!

