Skip to content

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 LabelGPIO PinConnected Device
TILTGPIO4Tilt 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 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:

    FieldValueDescription
    Trigger TypeSelect Polling TriggerPeriodic condition check
    Target PeripheralSelect tilt_01Tilt sensor
    Polling Interval100100ms
    Condition Expressionvalue == 0LOW = 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 ​

MethodAdvantageDisadvantageSuitable Scenario
Polling TriggerSimple and reliableUses CPUContinuous monitoring
Interrupt TriggerFast response, power-efficientPossible bouncingOccasional 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

FastBee | fastbee.cn