Example 11: PWM Breathing LED
Example 11: PWM Breathing LED
Experiment Overview
Control LED brightness via PWM (Pulse Width Modulation) output, creating a breathing effect from dim to bright and back. PWM simulates analog voltage output by adjusting the duty cycle.
Hardware Wiring
| Board Label | GPIO Pin | Connected Device |
|---|---|---|
| D1 | GPIO15 | LED (PWM dimming) |
ESP32 has 16 PWM channels, almost any GPIO can output PWM signals.
JSON Configuration Example
{
"peripherals": [
{
"id": "pwm_breath",
"name": "Breathing LED-PWM",
"type": 17,
"enabled": false,
"pins": [15],
"params": {
"pwmChannel": 0,
"pwmFrequency": 5000,
"pwmResolution": 8,
"defaultDuty": 0
}
}
]
}Parameter Description
| Parameter | Description | Recommended Value |
|---|---|---|
| pwmChannel | PWM channel number (0-15) | 0 |
| pwmFrequency | Frequency (Hz), high freq = no flicker | 5000 |
| pwmResolution | Resolution (bits), 8-bit = 0~255 | 8 |
| defaultDuty | Initial duty cycle | 0 |
Peripheral Execution Linkage
Scenario 1: Remote Brightness Control (Platform Trigger)
Function: Control LED brightness (0-255) via cloud platform commands
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 Brightness 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 Platform Trigger Receive cloud platform commands Operator Select Set Mode Received data as action parameters
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select Set PWM
- Target Peripheral: Select
pwm_breath
Click Save button
Test Method:
Send via cloud platform:
0→ Brightest (common-anode LED)128→ 50% brightness255→ Dimmest (off)
Scenario 2: Auto Breathing Effect (Timer Trigger + Script)
Function: Use script for automatic breathing effect (fade in → fade out cycle)
Web Interface Configuration Steps
Step 1: Create Rule
- Click New Rule
- Fill in basic configuration:
- Rule Name:
LED Breathing Light - 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 Timer Trigger Execute script on timer Timer Mode Select Fixed Interval Fast refresh Interval Time 2020ms (smooth breathing effect)
Step 3: Configure Action (Script)
Click Add Action button
Fill in:
- Action Type: Select Command Script
- Action Params:
var d=getVar('breath_duty',0); var dir=getVar('breath_dir',1); d+=dir*5; if(d>=255){d=255;dir=-1;} if(d<=0){d=0;dir=1;} pwmWrite('pwm_breath',d); setVar('breath_duty',d); setVar('breath_dir',dir);Click Save button
Script Description:
| Code | Description |
|---|---|
getVar('breath_duty',0) | Get current duty cycle, default 0 |
getVar('breath_dir',1) | Get direction, 1=brightening, -1=dimming |
d+=dir*5 | Change by 5 levels each time |
pwmWrite('pwm_breath',d) | Set PWM duty cycle |
setVar(...) | Save variable for next use |
⚠️ Note: Script executes every 20ms, consuming system resources, do not run multiple scripts simultaneously
Notes
- Frequency Selection: LED dimming recommend 1kHz+ to avoid flicker, 5kHz is common
- Resolution: 8-bit (256 levels) is sufficient for LED dimming, 12-bit (4096 levels) for fine control
- Channel Conflict: Different pins can share the same PWM channel (same frequency and duty cycle)
- Active Low: Puzhong board LEDs are common-anode, duty=0 is brightest, duty=255 is dimmest
- Multi-PWM: ESP32 supports 16 independent PWM outputs simultaneously
