# 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

```json
{
  "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**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `Remote Brightness Control`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

1. Click **Add Trigger** button
2. 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**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Set PWM**
   * **Target Peripheral**: Select `pwm_breath`

3. Click **Save** button

**Test Method**:

Send via cloud platform:

* `0` → Brightest (common-anode LED)
* `128` → 50% brightness
* `255` → 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**

1. Click **<i class="fas fa-plus"></i> New Rule**
2. Fill in basic configuration:
   * **Rule Name**: `LED Breathing Light`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

1. Click **Add Trigger** button
2. 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** | `20` | 20ms (smooth breathing effect) |

**Step 3: Configure Action (Script)**

1. Click **Add Action** button

2. 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);
   ```

3. 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

1. **Frequency Selection**: LED dimming recommend 1kHz+ to avoid flicker, 5kHz is common
2. **Resolution**: 8-bit (256 levels) is sufficient for LED dimming, 12-bit (4096 levels) for fine control
3. **Channel Conflict**: Different pins can share the same PWM channel (same frequency and duty cycle)
4. **Active Low**: Puzhong board LEDs are common-anode, duty=0 is brightest, duty=255 is dimmest
5. **Multi-PWM**: ESP32 supports 16 independent PWM outputs simultaneously
