Example 6: Button Control
Example 6: Button Control
Experiment Overview
Read button state via GPIO digital input (pull-up), triggering LED, buzzer and other peripheral actions. The Puzhong development board has 4 buttons (KEY1-KEY4), which read LOW when pressed.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| KEY1 | GPIO14 | Button (LOW when pressed) |
| KEY2 | GPIO27 | Button (LOW when pressed) |
| KEY3 | GPIO26 | Button (LOW when pressed) |
| KEY4 | GPIO25 | Button (LOW when pressed) |
One side of the button connects to GPIO, the other to GND, using internal pull-up resistors, GPIO reads LOW when pressed.
JSON Configuration Example
{
"peripherals": [
{
"id": "key1",
"name": "Button 1",
"type": 13,
"enabled": false,
"pins": [14],
"params": {}
},
{
"id": "key2",
"name": "Button 2",
"type": 13,
"enabled": false,
"pins": [27],
"params": {}
},
{
"id": "key3",
"name": "Button 3",
"type": 13,
"enabled": false,
"pins": [26],
"params": {}
},
{
"id": "key4",
"name": "Button 4",
"type": 13,
"enabled": false,
"pins": [25],
"params": {}
}
]
}Peripheral Execution Linkage
Scenario 1: Button Controls LED (Poll Trigger)
Function: Press Button 1 to toggle LED-D1 on/off state
Web Interface Configuration Steps
Step 1: Create Rule
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Button Control LED - Report Data: ❌ Disable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Poll Trigger Periodically detect button state Target Peripheral Select key1Button peripheral to poll Poll Interval 50msDetection frequency, 50ms avoids missed presses Response Timeout 1000msDefault is fine Retry Count 2Default is fine Poll Delay 100msDefault is fine Comparison Operator Select Equals Exact match Comparison Value 0LOW level (0) when pressed
Step 3: Configure Action
Click Add Action button
Fill in action configuration:
Field Value Description Action Type Select Toggle Level Flip GPIO state Target Peripheral Select led_d1LED to control Click Save button
Execution Flow:
Detect every 50ms
↓
Read key1 state
↓
Check: key1 == 0 ? (pressed)
↓ Yes
Toggle led_d1 state (on→off or off→on)⚠️ Note: Poll interval recommended at 50ms, too fast wastes CPU, too slow may miss short presses
Scenario 2: Button Interrupt Control (Event Trigger)
Function: Use interrupt mode for faster response
Prerequisites
Need to configure button as interrupt type first:
- Edit
key1peripheral - Change Peripheral Type to Interrupt (Falling Edge) (type: 19)
- After saving, system auto-generates event:
button_key1.pressed
Web Interface Configuration Steps
Step 1: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
Button Interrupt Control - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Event Trigger Respond to system events Event Category Select Button Event Select from dropdown Specific Event Select button_key1.pressedButton 1 press event
Step 3: Configure Action
Click Add Action button
Fill in action configuration:
Field Value Description Action Type Select HIGH Level GPIO output HIGH Target Peripheral Select relay_01Control relay Click Save button
Execution Flow:
Button pressed (falling edge)
↓
Trigger interrupt event
↓
Emit button_key1.pressed event
↓
Match event trigger
↓
Execute action: relay energizes💡 Advantage: Interrupt mode response speed <1ms, much faster than polling's 50ms
{
"name": "Button Control LED",
"enabled": true,
"triggers": [
{
"type": "poll",
"params": {
"peripheralId": "key1",
"interval": 50,
"condition": "value == 0"
}
}
],
"actions": [
{ "type": "gpio_toggle", "params": { "peripheralId": "led_d1" } }
]
}Using Interrupt Mode (More Precise)
{
"name": "Button Interrupt Control",
"enabled": true,
"triggers": [
{
"type": "event",
"params": {
"eventType": 100,
"source": "key1"
}
}
],
"actions": [
{ "type": "gpio_write", "params": { "peripheralId": "relay_01", "value": 1 } }
]
}Notes
- Debouncing: FastBee internally applies 50ms software debouncing to GPIO input
- Pull-up/Pull-down: Buttons to GND use pull-up (type: 13), buttons to VCC use pull-down (type: 14)
- Poll Interval: 50ms interval recommended for button detection, balancing response speed and resource usage
- GPIO25 Conflict: If using both relay and Button 4, pins need to be changed
- Long Press Detection: Can distinguish long/short press by recording press duration in scripts
