Example 20: Ultrasonic Distance (HC-SR04)
Example 20: Ultrasonic Distance (HC-SR04)
Experiment Overview
HC-SR04 ultrasonic sensor measures distance by transmitting and receiving ultrasonic waves, measurement range 2~400cm, precision ~3mm. FastBee has a built-in SENSOR_GENERIC generic sensor type (type: 38), combined with category: ultrasonic to support ultrasonic distance measurement.
This experiment first verifies stable readings at a fixed distance, then connects to alarm rules. If fixed distance readings are unstable, first check power supply, level shifting, reflection surface and Trig/Echo wiring before adjusting rule thresholds.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| TRIG | GPIO14 | HC-SR04 trigger pin |
| ECHO | GPIO27 | HC-SR04 echo pin |
TRIG sends a 10μs HIGH pulse to trigger measurement, ECHO returns HIGH level duration representing distance.
JSON Configuration Example
{
"peripherals": [
{
"id": "ultrasonic_01",
"name": "Ultrasonic Distance",
"type": 38,
"enabled": false,
"pins": [14, 27],
"params": {
"category": "ultrasonic"
}
}
]
}Peripheral Execution Linkage
Scenario 1: Periodic Distance Measurement (Timer Trigger)
Function: Read ultrasonic distance every 1 second
Web Interface Configuration Steps
Step 1: Create Rule
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Ultrasonic Periodic Measurement - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Timer Trigger Periodic acquisition Timer Mode Select Fixed Interval By millisecond interval Interval Time 10001 second
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select Read Sensor
- Target Peripheral: Select
ultrasonic_01 - Data Field:
distance
Click Save button
Scenario 2: Proximity Alarm (Poll Trigger)
Function: Check distance every 0.5 seconds, trigger buzzer short beep when distance < 20cm
Web Interface Configuration Steps
Step 1: Ensure buzzer peripheral is configured
- Peripheral ID:
buzzer_gpio - Type: GPIO Output
Step 2: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
Proximity Alarm - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 3: Configure Trigger (Poll Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Poll Trigger Periodically detect condition Target Peripheral Select ultrasonic_01Ultrasonic sensor Poll Interval 5000.5 seconds Condition Expression distance < 20Distance less than 20cm
Step 4: Configure Actions (3 actions needed)
Action 1: Buzzer ON
- Click Add Action button
- Fill in:
- Action Type: Select HIGH Level
- Target Peripheral: Select
buzzer_gpio
Action 2: Delay 200ms
- Click Add Action button again
- Fill in:
- Action Type: Select Delay
- Action Params:
200
Action 3: Buzzer OFF
Click Add Action button a third time
Fill in:
- Action Type: Select LOW Level
- Target Peripheral: Select
buzzer_gpio
Click Save button
HC-SR04 Features
| Feature | Parameter | Description |
|---|---|---|
| Measurement Range | 2cm ~ 400cm | Effective measurement distance |
| Precision | ±3mm | High precision |
| Blind Zone | 2cm | Minimum measurement distance |
| Measurement Angle | 15° | Sound wave cone emission angle |
| Sampling Interval | ≥60ms | Avoid echo interference |
Notes
- Power: HC-SR04 needs 5V power, ECHO outputs 5V, needs voltage division to 3.3V before connecting to ESP32
- Blind Zone: Minimum measurement distance 2cm, unreliable below this
- Measurement Angle: Sound wave cone emission angle ~15°, narrow spaces may have reflection interference
- Temperature Compensation: Sound speed varies with temperature (~343m/s at 20°C), compensation needed for precise measurement
- Sampling Frequency: Recommend at least 60ms interval to avoid front/back echo interference
- Pin Order: First pin in array is TRIG, second is ECHO
