An ESP32-based presence detection system that automatically turns on/off fans and lights using a radar sensor and relay module. The system detects human presence and movement, providing hands-free automation for your home or office.
- Presence Detection: Uses radar sensor for accurate human presence detection
- Automatic Control: Turns fan/light on when presence is detected, off when no one is present
- Real-time Monitoring: Provides detailed target information including position, speed, and distance
- UART Communication: High-speed communication at 256kbps for reliable data transfer
- GPIO Control: Simple relay control via ESP32 GPIO pins
- Non-contact Detection: Works through walls, clothing, and in various lighting conditions
- ESP32 development board
- Radar sensor module (compatible with the protocol used in this code)
- Relay module (5V or 3.3V compatible)
- Fan and/or light fixtures
- Jumper wires
- Power supply (12V for fan, appropriate voltage for lights)
- Breadboard or PCB for connections
| Component | ESP32 Pin | Description |
|---|---|---|
| Radar RX | GPIO 6 | UART receive from radar |
| Radar TX | GPIO 5 | UART transmit to radar |
| Relay Control | GPIO 1 | Digital output to relay |
| Radar VCC | 3.3V/5V | Power supply |
| Radar GND | GND | Ground connection |
| Relay VCC | 5V | Relay power supply |
| Relay GND | GND | Ground connection |
- ESP-IDF framework
- FreeRTOS (included with ESP-IDF)
- Standard ESP32 libraries
git clone <repository-url>
cd radar-sensor-control# Set up ESP-IDF environment
. $HOME/esp/esp-idf/export.sh# Configure the project
idf.py menuconfig
# Build the project
idf.py build
# Flash to ESP32
idf.py -p /dev/ttyUSB0 flash monitormain.c- Main application logic and initializationradar_sensor.h- Header file with radar sensor definitionsradar_sensor.c- Radar sensor driver implementation (provided in paste.txt)
radar_sensor_init()- Initialize radar sensor structure and pinsradar_sensor_begin()- Configure UART communicationradar_sensor_update()- Parse incoming radar dataradar_sensor_parse_data()- Extract target information from raw dataradar_sensor_get_target()- Get current target dataradar_sensor_deinit()- Cleanup resources
- Initialize radar sensor on UART1
- Continuously monitor for presence
- Control relay based on detection status
- Log target information for debugging
- Baud Rate: 256,000 bps
- Data Bits: 8
- Parity: None
- Stop Bits: 1
- Flow Control: None
The radar sensor uses a specific frame format:
- Header:
0xAA 0xFF 0x03 0x00 - Data Frame: 24 bytes of target information
- Tail:
0x55 0xCC
- Position: X, Y coordinates in millimeters
- Speed: Movement speed in cm/s
- Distance: Calculated distance from sensor
- Angle: Direction of target relative to sensor
- Power On: Connect power to ESP32 and radar sensor
- Automatic Detection: The system continuously monitors for presence
- Fan/Light Control:
- Presence Detected: Relay activates (GPIO 1 HIGH), turning on fan/light
- No Presence: Relay deactivates (GPIO 1 LOW), turning off fan/light
- Monitor Logs: Use serial monitor to view detection status and target data
I (1234) RADAR_WATCH: Radar sensor initialized successfully
I (1456) RADAR_WATCH: Target detected - X: 850.00 mm, Y: 1200.00 mm, Speed: 15.50 cm/s, Distance: 1478.32 mm, Angle: 35.23°
I (1789) RADAR_WATCH: No target detected
Modify the detection logic in radar_sensor_parse_data() to change sensitivity thresholds.
Adjust the delay in the main loop:
vTaskDelay(pdMS_TO_TICKS(100)); // 100ms delayAdd more GPIO pins for controlling multiple devices:
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT); // Second relay
gpio_set_direction(GPIO_NUM_3, GPIO_MODE_OUTPUT); // Third relay-
No Radar Data Received
- Check UART pin connections (RX/TX may be swapped)
- Verify baud rate setting (256000)
- Ensure radar sensor is powered correctly
-
Relay Not Switching
- Check relay module power supply
- Verify GPIO pin connection
- Test relay module independently
-
False Detections
- Adjust radar sensor position
- Modify detection thresholds in code
- Check for electromagnetic interference
-
Compilation Errors
- Ensure all header files are included
- Check ESP-IDF version compatibility
- Verify component configuration
- Use
ESP_LOGI()statements for debugging - Monitor serial output for radar data
- Test relay independently with simple GPIO toggle
- Verify UART communication with oscilloscope if available
- Ensure proper electrical isolation between low-voltage control and high-voltage loads
- Use appropriate fuses and circuit breakers
- Follow local electrical codes and regulations
- Consider using optically isolated relays for better safety
- Test thoroughly before permanent installation
- Add web interface for remote monitoring
- Implement presence timeout delays
- Add motion-based sensitivity adjustment
- Include multiple detection zones
- Add smartphone app integration
- Implement energy usage monitoring
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Check the troubleshooting section
- Review ESP-IDF documentation
- Create an issue in the project repository