Example 1: Light First LED
Example 1: Light First LED
Experiment Overview
Control the on/off state of an LED via GPIO digital output, the introductory experiment for embedded development. The Puzhong development board has 4 LEDs (D1-D4), connected to GPIO15, GPIO2, GPIO0, GPIO4 respectively.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| D1 | GPIO15 | LED (active low) |
| D2 | GPIO2 | LED (active low) |
| D3 | GPIO0 | LED (active low) |
| D4 | GPIO4 | LED (active low) |
The Puzhong board LEDs use common-anode wiring, GPIO outputs LOW to light the LED.
JSON Configuration Example
{
"peripherals": [
{
"id": "led_d1",
"name": "LED-D1",
"type": 12,
"enabled": false,
"pins": [15],
"params": {
"initialState": 0
}
},
{
"id": "led_d2",
"name": "LED-D2",
"type": 12,
"enabled": false,
"pins": [2],
"params": {
"initialState": 0
}
},
{
"id": "led_d3",
"name": "LED-D3",
"type": 12,
"enabled": false,
"pins": [0],
"params": {
"initialState": 0
}
},
{
"id": "led_d4",
"name": "LED-D4",
"type": 12,
"enabled": false,
"pins": [4],
"params": {
"initialState": 0
}
}
]
}Peripheral Execution Linkage
Scenario 1: Remote Control LED (Platform Trigger)
Function: Control LED via MQTT/HTTP commands from the cloud platform
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:
Remote Control LED-D1 - Report Data: ✅ Enable (report execution status to cloud platform)
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Platform Trigger Receive cloud platform commands Target Peripheral Leave empty or select led_d1Peripheral receiving the command Operator Select Match Pattern Trigger when received data equals comparison value Comparison Value {"cmd":"on"}Command content to match
Step 3: Configure Action
Click Add Action button
Fill in action configuration:
Field Value Description Action Type Select LOW Level GPIO output LOW, lights up LED Target Peripheral Select led_d1Peripheral ID to control Execution Mode Select Synchronous Wait for action to complete Click Save button
Test Method:
Send MQTT message via cloud platform:
{"cmd":"on"}Scenario 2: Timed LED Blink (Timer Trigger)
Function: Make LED blink once per second
Web Interface Configuration Steps
Step 1: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
LED-D1 Blink - Report Data: ❌ Disable (no need to report)
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Timer Trigger Execute at time intervals Timer Mode Select Interval Timer Execute every X seconds Interval Seconds 1Execute every 1 second
Step 3: Configure Actions (2 actions needed for blinking)
Action 1: Turn on LED
- Click Add Action button
- Fill in:
- Action Type: Select LOW Level
- Target Peripheral: Select
led_d1
Action 2: Delay then turn off LED
Click Add Action button again
Fill in:
- Action Type: Select Command Script
- Script Content:
DELAY 500 PERIPH led_d1 HIGH - Execution Mode: Select Asynchronous
Click Save button
Execution Flow:
Every 1 second
↓
Turn on LED (LOW level)
↓
Delay 500ms
↓
Turn off LED (HIGH level)
↓
Wait for next 1-second cycle{
"name": "Remote Control LED-D1",
"enabled": true,
"triggers": [
{ "type": "platform", "params": { "topic": "led/d1" } }
],
"actions": [
{ "type": "gpio_write", "params": { "peripheralId": "led_d1", "value": 0 } }
]
}Notes
- GPIO0: Must be pulled LOW during flashing, can be used as LED output during normal operation, but be careful during debugging
- GPIO2: Some ESP32 modules have a built-in LED also connected to this pin
- Level Logic: Puzhong board LEDs are active-low, opposite to the common active-high convention
- Current Limiting: ESP32 GPIO outputs max 12mA, onboard LEDs already have current-limiting resistors
