Ultrasonic Distance Alert Scenario
Ultrasonic Distance Alert Scenario
Scenario Description
Use an HC-SR04 ultrasonic sensor for periodic distance measurement. When an object is detected within a safe distance threshold, trigger a buzzer alarm and report alarm data via MQTT.
Required Peripherals
| Peripheral | Type | Description |
|---|---|---|
| ultrasonic1 | SENSOR(38) | HC-SR04 ultrasonic sensor |
| buzzer | GPIO_DIGITAL_OUTPUT(12) | Alarm buzzer |
Complete Configuration Process
Method 1: Web Interface Configuration (Recommended)
This scenario requires first adding the HC-SR04 and alarm buzzer, then using peripheral execution rules for distance collection, threshold evaluation, and alarm actions.
Step 1: Configure Peripherals
Step 1: Open Peripheral Management Page
- Open a browser and navigate to the ESP32 IP address
- After logging in, click Peripheral Configuration in the left menu
Step 2: Add HC-SR04 Ultrasonic Sensor
Click the Add Peripheral button
Fill in the configuration:
Field Value Description Peripheral ID ultrasonic1Ultrasonic distance Name Ultrasonic DistanceDisplay name Peripheral Type Universal Sensor (type: 38) HC-SR04 driver Pin Configuration 5,18Trig=GPIO5, Echo=GPIO18 Sensor Category ULTRASONICUltrasonic Collection Interval 500500ms (minimum 100ms) Click Save
💡 Tip: pins[0]=Trig pin, pins[1]=Echo pin
Step 3: Add Alarm Buzzer
Click the Add Peripheral button again
Fill in the configuration:
Field Value Description Peripheral ID buzzerAlarm buzzer Name Alarm BuzzerDisplay name Peripheral Type GPIO Digital Output (type: 12) Buzzer control Pin Configuration 4IN connected to GPIO4 Initial State 0Default off Click Save
Step 2: Configure Peripheral Execution Rules
Rule 1: Timed Distance Measurement and Reporting
Switch to Peripheral Execution Management tab
Click the Add Rule button
Fill in basic configuration:
- Rule Name:
Timed Distance Measurement - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Configure trigger:
- Trigger Type: Select Timer Trigger
- Timer Mode: Select Fixed Interval
- Interval Time:
5(5 seconds)
Configure action:
- Action Type: Select Sensor Read
- Target Peripheral: Select
ultrasonic1
Click Save
Rule 2: Too Close Alarm
Create rule, name:
Too Close AlarmTrigger configuration:
- Trigger Type: Select Platform Trigger
- Target Peripheral ID: Enter
ultrasonic1 - Operator: Select
Less Than (<) - Threshold:
30(30cm)
Action configuration:
- Action Type: Select Flash
- Target Peripheral: Select
buzzer - Flash Interval:
200(200ms rapid beeping)
Click Save
Rule 3: Distance Recovered - Dismiss Alarm
Create rule, name:
Distance Safe DismissTrigger configuration:
- Trigger Type: Select Platform Trigger
- Target Peripheral ID: Enter
ultrasonic1 - Operator: Select
Greater Than (>) - Threshold:
50(50cm)
Action configuration:
- Action Type: Select Low Level
- Target Peripheral: Select
buzzer
Click Save
💡 Tip:
- Alarm threshold 30cm, dismiss threshold 50cm, forming a 20cm hysteresis band
- Ultrasonic minimum sampling interval is 100ms; ≥500ms is recommended to avoid echo interference
- HC-SR04 requires 5V power; Echo outputs 5V — be mindful of ESP32 GPIO voltage tolerance (voltage divider recommended)
Method 2: JSON Configuration File Import
Distance Measurement Principle
- Trig pin outputs a 10μs high-level pulse
- Sensor emits ultrasonic wave and waits for echo
- Echo pin outputs high level; duration = round-trip time
- Distance(cm) = Duration(μs) × 0.034 / 2
- Effective measurement range: 2cm ~ 400cm
Precautions
- Hysteresis Band: Gap between alarm threshold (30cm) and dismiss threshold (50cm) prevents jitter
- Sampling Interval: Ultrasonic minimum sampling interval is 100ms; ≥500ms is recommended to avoid echo interference
- Blind Zone: HC-SR04 is inaccurate at close range (< 2cm)
- Installation Angle: Sensor must face the target object directly; excessive angle causes no echo return
- Power: HC-SR04 requires 5V power; Echo outputs 5V — be mindful of ESP32 GPIO voltage tolerance (voltage divider recommended)
