Skip to content

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 LabelGPIO PinConnected Device
D1GPIO15LED (active low)
D2GPIO2LED (active low)
D3GPIO0LED (active low)
D4GPIO4LED (active low)

The Puzhong board LEDs use common-anode wiring, GPIO outputs LOW to light the LED.

JSON Configuration Example ​

json
{
  "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 the LED through MQTT commands from the cloud platform

Web Interface Configuration Steps ​

Step 1: Create Rule

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Remote Control LED-D1
    • Report Data: ✅ Enable (report execution status to cloud platform)
    • Enabled: ✅ Enable

Step 2: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Platform TriggerReceive cloud platform commands
    Target PeripheralLeave empty or select led_d1Peripheral receiving the command
    OperatorSelect Match PatternTrigger when received data equals comparison value
    Comparison Value{"cmd":"on"}Command content to match

Step 3: Configure Action

  1. Click Add Action button

  2. Fill in action configuration:

    FieldValueDescription
    Action TypeSelect LOW LevelGPIO output LOW, lights up LED
    Target PeripheralSelect led_d1Peripheral ID to control
    Execution ModeSelect SynchronousWait for action to complete
  3. Click Save button

Test Method:

Send MQTT message via cloud platform:

json
{"cmd":"on"}

Function: Make LED blink once per second

Web Interface Configuration Steps ​

Step 1: Create Rule

  1. Click New Rule
  2. Fill in basic configuration:
    • Rule Name: LED-D1 Blink
    • Report Data: ❌ Disable (no need to report)
    • Enabled: ✅ Enable

Step 2: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Timer TriggerExecute at time intervals
    Timer ModeSelect Interval TimerExecute every X seconds
    Interval Seconds1Execute every 1 second

Step 3: Configure Actions (2 actions needed for blinking)

Action 1: Turn on LED

  1. Click Add Action button
  2. Fill in:
    • Action Type: Select LOW Level
    • Target Peripheral: Select led_d1

Action 2: Delay then turn off LED

  1. Click Add Action button again

  2. Fill in:

    • Action Type: Select Command Script
    • Script Content:
      DELAY 500
      PERIPH led_d1 HIGH
    • Execution Mode: Select Asynchronous
  3. 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
json
{
  "id": "exec_led_d1",
  "name": "Remote Control LED-D1",
  "enabled": true,
  "triggers": [
    {
      "triggerType": 0,
      "triggerPeriphId": "led_d1",
      "operatorType": 0,
      "compareValue": ""
    }
  ],
  "actions": [
    {
      "targetPeriphId": "led_d1",
      "actionType": 1,
      "actionValue": ""
    }
  ]
}

Notes ​

  1. GPIO0: Must be pulled LOW during flashing, can be used as LED output during normal operation, but be careful during debugging
  2. GPIO2: Some ESP32 modules have a built-in LED also connected to this pin
  3. Level Logic: Puzhong board LEDs are active-low, opposite to the common active-high convention
  4. Current Limiting: ESP32 GPIO outputs max 12mA, onboard LEDs already have current-limiting resistors

FastBee | fastbee.cn