# Example 26: Tilt Sensor

## Experiment Overview

A tilt sensor (ball switch) contains a metal ball inside. When the sensor tilts beyond a certain angle, the ball contacts the switch contacts and conducts, outputting a low level. Suitable for tilt detection, anti-theft alarms, and similar scenarios.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| TILT | GPIO4 | Tilt sensor (digital output) |

> The tilt sensor is a switch-type output, using internal pull-up; it outputs LOW when tilted.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "tilt_01",
      "name": "Tilt Sensor",
      "type": 13,
      "enabled": false,
      "pins": [4],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Tilt Alarm (Polling Trigger)

**Feature**: Check tilt status every 100ms; when tilted, trigger buzzer alarm for 1 second

#### 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**: `Tilt 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 `tilt_01` | Tilt sensor |
   | **Polling Interval** | `100` | 100ms |
   | **Condition Expression** | `value == 0` | LOW = tilted |

**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 1 Second**

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

**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 | Advantage | Disadvantage | Suitable Scenario |
|--------|-----------|--------------|-------------------|
| Polling Trigger | Simple and reliable | Uses CPU | Continuous monitoring |
| Interrupt Trigger | Fast response, power-efficient | Possible bouncing | Occasional detection |

## Notes

1. **Bouncing**: Ball switches may bounce frequently at critical angles; add debouncing logic
2. **Sensitivity**: Different models have different trigger angles (typically 15°~45°)
3. **Mounting Direction**: Sensor mounting orientation determines trigger direction
4. **Interrupt Method**: Can also use GPIO\_INTERRUPT\_CHANGE (type: 20) for interrupt-based detection
