Example 26: Tilt Sensor
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
{
"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
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Tilt Alarm - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Polling Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Polling Trigger Periodic condition check Target Peripheral Select tilt_01Tilt sensor Polling Interval 100100ms Condition Expression value == 0LOW = tilted
Step 4: Configure Actions (3 actions required)
Action 1: Buzzer ON
- Click Add Action button
- Fill in:
- Action Type: Select HIGH
- Target Peripheral: Select
buzzer_gpio
Action 2: Delay 1 Second
- Click Add Action button again
- Fill in:
- Action Type: Select Delay
- Action Parameter:
1000
Action 3: Buzzer OFF
Click Add Action button a third time
Fill in:
- Action Type: Select LOW
- Target Peripheral: Select
buzzer_gpio
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
- Bouncing: Ball switches may bounce frequently at critical angles; add debouncing logic
- Sensitivity: Different models have different trigger angles (typically 15°~45°)
- Mounting Direction: Sensor mounting orientation determines trigger direction
- Interrupt Method: Can also use GPIO_INTERRUPT_CHANGE (type: 20) for interrupt-based detection
