Example 4: Buzzer Control
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
{
"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
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Buzzer Periodic Alarm - Report Data: ❌ Disable
- 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 5Execute every 5 seconds
Step 3: Configure Actions (3 actions needed)
Action 1: Start Buzzer
- Click Add Action button
- Fill in:
- Action Type: Select HIGH Level
- Target Peripheral: Select
buzzer_gpio
Action 2: Delay 200ms
- Click Add Action button again
- Fill in:
- Action Type: Select Command Script
- Script Content:
DELAY 200
Action 3: Stop Buzzer
Click Add Action button a third time
Fill in:
- Action Type: Select LOW Level
- Target Peripheral: Select
buzzer_gpio
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
- Click New Rule
- Fill in basic configuration:
- Rule Name:
Buzzer Play Tone - 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 Match Pattern Trigger when received data equals comparison value Comparison Value {"freq":1000}Command to match
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select Set PWM
- Target Peripheral: Select
buzzer_pwm - Action Params:
{"duty":128,"frequency":1000}
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
- Active vs Passive: Active buzzers have a built-in oscillator, sound when powered; passive ones need PWM square wave
- PWM Frequency Range: Human hearing range is 20Hz~20kHz, common alarm tones 1kHz~4kHz
- Duty Cycle: Passive buzzers recommend 50% (duty=128/8bit), too low may not produce sound
- Noise Control: Reduce duty cycle during debugging to lower volume
