Example 44: PIR Motion Sensor
Example 44: PIR Motion Sensor
Experiment Overview
The HC-SR501 PIR (Passive Infrared) module detects human presence by sensing changes in infrared radiation emitted by the human body. Outputs HIGH when a person is detected, suitable for automatic lighting, security alarms, people counting, and similar scenarios.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| OUT | GPIO4 | PIR module output (HIGH when person detected) |
HC-SR501 requires 5-20V power, outputs 3.3V HIGH, can connect directly to ESP32.
JSON Configuration Example
{
"peripherals": [
{
"id": "pir_01",
"name": "PIR Motion",
"type": 11,
"enabled": false,
"pins": [4],
"params": {}
}
]
}Peripheral Execution Linkage
Scenario 1: Motion-Activated Light (Polling Trigger)
Feature: Turn on relay when person detected, turn off after 30 seconds
Web UI Configuration Steps
Step 1: Ensure Relay Peripheral is Configured
- Peripheral ID:
relay_01 - Type: GPIO Output
Step 2: Create Rule
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Motion-Activated Light - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Polling Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Polling Trigger Periodic condition check Target Peripheral Select pir_01PIR motion sensor Polling Interval 200200ms Condition Expression value == 1Person detected
Step 4: Configure Actions (3 actions required)
Action 1: Open Relay
- Click Add Action button
- Fill in:
- Action Type: Select HIGH
- Target Peripheral: Select
relay_01
Action 2: Delay 30 Seconds
- Click Add Action button again
- Fill in:
- Action Type: Select Delay
- Delay Time:
30000(30000ms = 30 seconds)
Action 3: Close Relay
Click Add Action button a third time
Fill in:
- Action Type: Select LOW
- Target Peripheral: Select
relay_01
Click Save button
Scenario 2: Security Alarm (Polling Trigger)
Feature: Trigger buzzer alarm when human motion is detected
Configuration Steps:
- Create rule, name:
Intrusion Alarm - Trigger configuration:
- Trigger Type: Select Polling Trigger
- Target Peripheral: Select
pir_01 - Polling Interval:
500 - Condition Expression:
value == 1
- Action configuration (2 actions required):
- Action 1: Select HIGH, target
buzzer_gpio - Action 2: Select Send Event, type
alarm, messageHuman motion detected!
- Action 1: Select HIGH, target
- Click Save
PIR Sensor Specifications
| Feature | Description |
|---|---|
| Supply Voltage | 5-20V |
| Output Level | 3.3V (ESP32 compatible) |
| Detection Range | 3-7m (potentiometer adjustable) |
| Delay Time | 5s-5min (potentiometer adjustable) |
| Trigger Mode | L=non-repeatable, H=repeatable (jumper) |
| Warm-up Time | ~1 minute |
Notes
- Warm-up Time: HC-SR501 requires ~1 minute warm-up after power-on to stabilize
- Delay Adjustment: Module potentiometer adjusts HIGH output hold time (5s~5min)
- Sensitivity: Another potentiometer adjusts detection sensitivity/range (3~7m)
- Trigger Mode: L=non-repeatable trigger, H=repeatable trigger (jumper selection)
- Temperature Dependency: PIR detects temperature difference between human body and environment; sensitivity decreases when ambient temperature approaches body temperature
- Occlusion: Cannot detect through glass (glass blocks far-infrared)
