# Example 4: Buzzer Control

## Experiment Overview

Drive a buzzer to produce sound via GPIO output or PWM signal. Passive buzzers require PWM square wave drive (adjustable frequency), while active buzzers only need HIGH/LOW level switching.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| BEEP | GPIO4 | Active/Passive buzzer |

> The Puzhong board buzzer is connected to GPIO4, active buzzer driven by HIGH level.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "buzzer_gpio",
      "name": "Buzzer-Switch",
      "type": 12,
      "enabled": false,
      "pins": [4],
      "params": {
        "initialState": 0
      }
    },
    {
      "id": "buzzer_pwm",
      "name": "Buzzer-PWM",
      "type": 17,
      "enabled": false,
      "pins": [4],
      "params": {
        "pwmChannel": 1,
        "pwmFrequency": 2000,
        "pwmResolution": 8,
        "defaultDuty": 0
      }
    }
  ]
}
```

> Note: The same pin can only be configured in one mode, choose either GPIO or PWM.

## Peripheral Execution Linkage

### Scenario 1: Periodic Alarm (Timer Trigger)

**Function**: Sound every 5 seconds, lasting 200ms

#### 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**: `Buzzer Periodic Alarm`
   * **Report Data**: ❌ Disable
   * **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 at time intervals |
   | **Timer Mode** | Select **Interval Timer** | Execute every X seconds |
   | **Interval Seconds** | `5` | Execute every 5 seconds |

**Step 3: Configure Actions (3 actions needed)**

**Action 1: Start Buzzer**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **HIGH Level**
   * **Target Peripheral**: Select `buzzer_gpio`

**Action 2: Delay 200ms**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Command Script**
   * **Script Content**: `DELAY 200`

**Action 3: Stop Buzzer**

1. Click **Add Action** button a third time

2. Fill in:
   * **Action Type**: Select **LOW Level**
   * **Target Peripheral**: Select `buzzer_gpio`

3. Click **Save** button

**Execution Flow**:

```
Every 5 seconds
  ↓
Start buzzer (HIGH)
  ↓
Delay 200ms
  ↓
Stop buzzer (LOW)
```

***

### Scenario 2: PWM Tone Control (Platform Trigger)

**Function**: Control tone frequency via cloud platform commands

#### 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**: `Buzzer Play Tone`
   * **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 **Match Pattern** | Trigger when received data equals comparison value |
   | **Comparison Value** | `{"freq":1000}` | Command to match |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Set PWM**
   * **Target Peripheral**: Select `buzzer_pwm`
   * **Action Params**: `{"duty":128,"frequency":1000}`

3. Click **Save** button

### Common Tone Frequencies

| Note | Frequency (Hz) | Description |
|------|---------------|-------------|
| DO | 523 | Middle C |
| RE | 587 | D |
| MI | 659 | E |
| FA | 698 | F |
| SO | 784 | G |
| LA | 880 | A |
| SI | 988 | B |
| DO+ | 1046 | High C |

## Notes

1. **Active vs Passive**: Active buzzers have a built-in oscillator, sound when powered; passive ones need PWM square wave
2. **PWM Frequency Range**: Human hearing range is 20Hz~20kHz, common alarm tones 1kHz~4kHz
3. **Duty Cycle**: Passive buzzers recommend 50% (duty=128/8bit), too low may not produce sound
4. **Noise Control**: Reduce duty cycle during debugging to lower volume
