# Example 27: Vibration Sensor

## Experiment Overview

A vibration sensor (SW-420) outputs a digital signal when vibration is detected, suitable for anti-theft alarms, collision detection, and similar applications. The sensor internally uses a spring-type contact that disconnects and outputs HIGH when vibration occurs.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| VIB | GPIO4 | Vibration sensor digital output (DO) |

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "vibration_01",
      "name": "Vibration Sensor",
      "type": 18,
      "enabled": false,
      "pins": [4],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Vibration Alarm (Event Trigger)

**Feature**: Trigger buzzer alarm for 2 seconds when vibration is detected

#### 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**: `Vibration Alarm`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

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

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Event Trigger** | Respond to interrupt events |
   | **Event Type** | `100` | GPIO interrupt event |
   | **Event Source** | `vibration_01` | Vibration sensor peripheral ID |

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

**Action 1: Buzzer ON**

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

**Action 2: Delay 2 Seconds**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Delay**
   * **Action Parameter**: `2000`

**Action 3: Buzzer OFF**

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

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

3. Click **Save** button

### Detection Method Comparison

| Method | Peripheral Type | Advantage | Disadvantage |
|--------|----------------|-----------|--------------|
| Interrupt Trigger | type: 18 | Fast response, power-efficient | May trigger multiple times |
| Polling Detection | type: 11 | Simple and reliable | Uses CPU |

## Notes

1. **Sensitivity Adjustment**: Some modules include a potentiometer for sensitivity adjustment
2. **False Triggers**: At high sensitivity, wind and sound can trigger the sensor
3. **Interrupt Method Recommended**: Use rising edge interrupt (type: 18) to avoid polling overhead
4. **Debouncing**: A single vibration may produce multiple triggers; set a 1-2 second cooldown period
