# Example 34: Flame Sensor

## Experiment Overview

A flame sensor detects specific infrared wavelengths (760nm~1100nm) emitted by flames via an IR receiver tube, outputting an analog signal indicating flame intensity. Suitable for fire alarms, firefighting robots, and other safety applications.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| AO | GPIO34 | Flame sensor analog output |
| DO | GPIO4 | Flame sensor digital output (optional) |

> When flame is detected, the analog value decreases (the larger the flame, the lower the value); digital output goes LOW.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "flame_01",
      "name": "Flame Sensor",
      "type": 15,
      "enabled": false,
      "pins": [34],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Fire Alarm (Polling Trigger)

**Feature**: Trigger buzzer and LED alarm when flame is detected (ADC < 1000)

#### Web UI Configuration Steps

**Step 1: Ensure Alarm Peripherals are Configured**

* Buzzer: `buzzer_gpio` (GPIO Output)
* LED: `led_d1` (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**: `Fire Alarm`
   * **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 `flame_01` | Flame sensor |
   | **Polling Interval** | `500` | 0.5 seconds (fast response) |
   | **Condition Expression** | `value < 1000` | Flame threshold |

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

**Action 1: Buzzer Alarm**

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

**Action 2: LED Alarm**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **LOW** (common-anode LED on)
   * **Target Peripheral**: Select `led_d1`

**Action 3: Send Alarm Event**

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

2. Fill in:
   * **Action Type**: Select **Send Event**
   * **Event Type**: `alarm`
   * **Event Message**: `Flame detected!`

3. Click **Save** button

### Flame Sensor Value Reference

| Status | ADC Value | Description |
|--------|-----------|-------------|
| No Flame | >2000 | Normal environment |
| Distant Flame | 1000-2000 | Weak flame |
| Close Flame | <1000 | Strong flame |

## Notes

1. **Detection Angle**: Sensor detection angle is ~60°; must face the flame direction
2. **Detection Range**: Effective detection range is ~20-100cm (depends on flame size)
3. **False Alarms**: Strong sunlight or incandescent lamps may also trigger; set threshold appropriately
4. **Safety Distance**: Be cautious of fire safety during testing; a small lighter flame is sufficient for testing
5. **Multiple Sensors**: Deploy multiple sensors to cover different directions
