# 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

```json
{
  "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**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `Motion-Activated Light`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 3: Configure Trigger (Polling Trigger)**

1. Click **Add Trigger** button
2. Fill in trigger configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Polling Trigger** | Periodic condition check |
   | **Target Peripheral** | Select `pir_01` | PIR motion sensor |
   | **Polling Interval** | `200` | 200ms |
   | **Condition Expression** | `value == 1` | Person detected |

**Step 4: Configure Actions (3 actions required)**

**Action 1: Open Relay**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **HIGH**
   * **Target Peripheral**: Select `relay_01`

**Action 2: Delay 30 Seconds**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Delay**
   * **Delay Time**: `30000` (30000ms = 30 seconds)

**Action 3: Close Relay**

1. Click **Add Action** button a third time

2. Fill in:
   * **Action Type**: Select **LOW**
   * **Target Peripheral**: Select `relay_01`

3. Click **Save** button

***

### Scenario 2: Security Alarm (Polling Trigger)

**Feature**: Trigger buzzer alarm when human motion is detected

**Configuration Steps**:

1. Create rule, name: `Intrusion Alarm`
2. Trigger configuration:
   * **Trigger Type**: Select **Polling Trigger**
   * **Target Peripheral**: Select `pir_01`
   * **Polling Interval**: `500`
   * **Condition Expression**: `value == 1`
3. Action configuration (2 actions required):
   * **Action 1**: Select **HIGH**, target `buzzer_gpio`
   * **Action 2**: Select **Send Event**, type `alarm`, message `Human motion detected!`
4. 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

1. **Warm-up Time**: HC-SR501 requires ~1 minute warm-up after power-on to stabilize
2. **Delay Adjustment**: Module potentiometer adjusts HIGH output hold time (5s~5min)
3. **Sensitivity**: Another potentiometer adjusts detection sensitivity/range (3~7m)
4. **Trigger Mode**: L=non-repeatable trigger, H=repeatable trigger (jumper selection)
5. **Temperature Dependency**: PIR detects temperature difference between human body and environment; sensitivity decreases when ambient temperature approaches body temperature
6. **Occlusion**: Cannot detect through glass (glass blocks far-infrared)
