Skip to content

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 LabelGPIO PinConnected Device
BEEPGPIO4Active/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 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:

    FieldValueDescription
    Trigger TypeSelect Timer TriggerExecute at time intervals
    Timer ModeSelect Interval TimerExecute every X seconds
    Interval Seconds5Execute 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 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:

    FieldValueDescription
    Trigger TypeSelect Platform TriggerReceive cloud platform commands
    OperatorSelect Match PatternTrigger 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 ​

NoteFrequency (Hz)Description
DO523Middle C
RE587D
MI659E
FA698F
SO784G
LA880A
SI988B
DO+1046High 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

FastBee | fastbee.cn