Introduction
This tutorial allows you to integrate the C4002 millimeter-wave sensor into HomeAssistant, and view and configure sensor data in real time.
Tutorial
Preparation
- Hardware
- C4002 Millimeter-Wave Motion and Static Presence Detection Module
- ESP32-S2-DevKitC-1 (Other ESP32 development boards with 5V power supply are also compatible)
- HomeAssistant host
- Software
- Install the ESPHome add-on in HomeAssistant OS
Wiring Diagram
| C4002 | ESP32-S2-DevKitC-1 |
|---|---|
| VIN | 5V |
| GND | G |
| RX | IO17 |
| TX | IO16 |
Add a New Device in ESPHome
-
Click the "NEW DEVICE" button on the ESPHome homepage.
-
Click "CONTINUE".
-
Select "New Device Setup".
-
If this is your first time using ESPHome, enter the "Name", "Network name", and "Network Password". For subsequent uses, only the "Name" is required.
-
Select the model of the development board you are using.
-
Save the device's Encryption key and click SKIP.
-
Click "EDIT" on the corresponding device card on the ESPHome homepage to view the automatically generated default configuration.
Modify the Configuration
Copy the configuration below to the end of the automatically generated default code, paying attention to indentation and line breaks.
# UART Configuration
uart:
id: uart_bus
tx_pin: GPIO25
rx_pin: GPIO26
baud_rate: 115200
# External components
external_components:
- source:
type: git # Source
url: https://github.com/cdjq/esphome.git # Must be the root directory
ref: dev # Branch
components:
dfrobot_c4002 # Component
# C4002 Component Configuration
dfrobot_c4002:
id: my_c4002
# Sensor Configuration Section
sensor:
- platform: dfrobot_c4002
c4002_id: my_c4002
movement_distance:
name: "Motion Distance"
id: movement_distance_sensor # Motion distance
existing_distance:
name: "Presence Distance"
id: existing_distance_sensor # Presence distance
movement_speed:
name: "Motion Speed"
id: movement_speed_sensor
movement_direction:
name: "Motion Direction"
id: movement_direction_sensor # Motion direction
internal: true
target_status:
name: "Target Status"
id: target_status_sensor
internal: true
text_sensor:
- platform: template
name: "Movement Direction"
id: movement_direction_text
icon: "mdi:directions"
lambda: |-
int d = id(movement_direction_sensor).state;
if (d == 0) return {"Approaching"};
else if (d == 1) return {"No Direction"};
else if (d == 2) return {"Away"};
else return {"Unknown"};
update_interval: 1s
- platform: template
name: "Target Status"
id: target_status_text
icon: "mdi:human-greeting"
lambda: |-
int d = id(target_status_sensor).state;
if (d == 0) return {"No Target"};
else if (d == 1) return {"Static Presence"};
else if (d == 2) return {"Motion"};
else return {"Unknown"};
update_interval: 0.5s
- platform: dfrobot_c4002
c4002_id: my_c4002
c4002_text_sensor:
name: "C4002 log"
icon: "mdi:message-text-outline"
# Switch Configuration Section
switch:
- platform: dfrobot_c4002
switch_out_led:
name: "Out LED Switch"
switch_run_led:
name: "Run LED Switch"
switch_factory_reset:
name: "Factory Reset"
switch_environmental_calibration:
name: "Sensor Calibration"
# Select Configuration Section
select:
- platform: dfrobot_c4002
c4002_id: my_c4002
operating_mode:
name: "OUT Mode"
options:
- "Mode_1"
- "Mode_2"
- "Mode_3"
# Minimum and Maximum Detection Range Configuration
number:
- platform: dfrobot_c4002
max_range:
name: "Max detection distance"
min_range:
name: "Min detection distance"
light_threshold:
name: "Light Threshold"
area1_min:
name: "Area 1 Excluded Range Min"
area1_max:
name: "Area 1 Excluded Range Max"
area2_min:
name: "Area 2 Excluded Range Min"
area2_max:
name: "Area 2 Excluded Range Max"
area3_min:
name: "Area 3 Excluded Range Min"
area3_max:
name: "Area 3 Excluded Range Max"
target_disappeard_delay_time:
name: "Target Disappear Delay Time"
For configuration explanations, please refer to the link.
First-time Flashing
- After modifying the configuration, click "INSTALL".
-
For the first flash, select "Plug into this computer". After the initial setup, you can click "Wirelessly" for OTA updates.
-
Wait for the system to complete compilation, then click "Download project".
-
Click "Factory format (Previously Modern)" to download the firmware locally.
-
Connect the development board to your computer, then click "Open ESPHome Web".
-
Select your device's serial port in the following order.
-
Select the firmware to be flashed in the following order and proceed with flashing.
-
Wait for the flashing process to finish.
Integrate into HomeAssistant
Return to the HomeAssistant homepage, and navigate to Settings > Devices & services to view your device information.
Click "Add" to complete the integration; you may need to enter the Encryption key.
Navigate to Settings > Devices & services > ESPHome > C4002 to view detailed device information.
