# Example 35: Smoke Sensor (MQ-2)

## Experiment Overview

The MQ-2 gas sensor detects combustible gases (LPG, propane, hydrogen) and smoke concentration, outputting an analog signal. Suitable for kitchen gas leak alarms, smoke fire alarms, and other safety scenarios.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| AO | GPIO34 | MQ-2 analog output |
| DO | GPIO4 | MQ-2 digital output (over-threshold alarm) |

> MQ-2 requires 1-2 minutes of preheating before readings stabilize.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "smoke_01",
      "name": "MQ-2 Smoke",
      "type": 15,
      "enabled": false,
      "pins": [34],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Smoke Alarm (Polling Trigger)

**Feature**: Trigger buzzer alarm when smoke concentration is too high (ADC > 2000)

#### Web UI Configuration Steps

**Step 1: Ensure Buzzer Peripheral is Configured**

* Peripheral ID: `buzzer_gpio`
* 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**: `Smoke 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 `smoke_01` | MQ-2 smoke sensor |
   | **Polling Interval** | `2000` | 2 seconds |
   | **Condition Expression** | `value > 2000` | Smoke concentration threshold |

**Step 4: Configure Actions (2 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: Send Alarm Event**

1. Click **Add Action** button again

2. Fill in:
   * **Action Type**: Select **Send Event**
   * **Event Type**: `alarm`
   * **Event Message**: `Smoke concentration too high!`

3. Click **Save** button

### MQ-2 Sensor Specifications

| Feature | Description |
|---------|-------------|
| Detectable Gases | LPG, propane, hydrogen, smoke |
| Preheat Time | 24-48 hours first time, 1-2 minutes daily |
| Power Consumption | ~800mW (heating element) |
| Lifespan | ~2-5 years |
| Cross-Sensitivity | Also responds to alcohol, acetone |

## Notes

1. **Preheating**: First power-on requires 24-48 hours preheating; daily use requires 1-2 minutes warm-up
2. **Power Consumption**: Heating element consumes ~800mW; stable 5V power supply required
3. **Cross-Sensitivity**: Also responds to alcohol, acetone, etc.; not smoke-specific
4. **Calibration**: Record baseline value in clean air as alarm threshold reference
5. **Lifespan**: Sensor lifespan is ~2-5 years; sensitivity gradually decreases
