Skip to content

PWM Buzzer

This section explains how to use the PWM peripheral to generate tones with the onboard or external buzzer connected to PF1 (M0PWM1). It demonstrates how to create different sounds when a button is clicked and double-clicked, providing simple audio feedback in embedded applications.

PWM0 / PF1 Audio Feedback
ComponentPinFunction
BuzzerPF1PWM output (M0PWM1)
Button S1PN1Input

Ensure the buzzer is connected to PF1 (default on BOOSTXL-EDUMKII) and that the PWM module (PWM0) is enabled in your system.


To use the button functionality and timing, include the following libraries in your project:

#include <stdint.h>
#include <stdbool.h>
extern "C" {
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/fpu.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include "inc/hw_ints.h"
}
#include "button.h"
#include "timerLib.h"
#include "elapsedTime.h"

The buzzer is driven by PWM0 Generator 0, Output 1. The function below sets up the PWM clock and configures the pin:

#define BUZZER_PWM_BASE PWM0_BASE
#define BUZZER_GEN PWM_GEN_0
#define BUZZER_OUTNUM PWM_OUT_1
#define BUZZER_OUTBIT PWM_OUT_1_BIT
void Buzzer_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
while (!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0));
while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF));
GPIOPinConfigure(GPIO_PF1_M0PWM1);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_64); // 120 MHz / 64 = 1.875 MHz
}

Step 2 — Create a Function to Play a Tone

Section titled “Step 2 — Create a Function to Play a Tone”

The Buzzer_Beep function generates a square wave of a specified frequency for a defined duration using PWM. For this example we use delay blocking for simplicity, but in a real application consider using timers or non-blocking methods. The expected use case is for short UI feedback sounds (40–80 ms).

void Buzzer_Beep(uint32_t freq_hz, uint32_t duration_ms)
{
if (freq_hz == 0) return;
uint32_t pwmClock = gSysClk / 64;
uint32_t period = pwmClock / freq_hz;
PWMGenConfigure(BUZZER_PWM_BASE, BUZZER_GEN, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(BUZZER_PWM_BASE, BUZZER_GEN, period);
PWMPulseWidthSet(BUZZER_PWM_BASE, BUZZER_OUTNUM, period / 2);
PWMOutputState(BUZZER_PWM_BASE, BUZZER_OUTBIT, true);
PWMGenEnable(BUZZER_PWM_BASE, BUZZER_GEN);
SysCtlDelay((gSysClk / 3 / 1000) * duration_ms);
PWMOutputState(BUZZER_PWM_BASE, BUZZER_OUTBIT, false);
}

Step 3 — Play Sounds on Button click and DoubleClick

Section titled “Step 3 — Play Sounds on Button click and DoubleClick”

Below is a simple program that demonstrates playing two different tones using the Button class.

Button btnPlay(S1);
Timer timer;
void onClick() {
// Play a higher tone when pressed
Buzzer_Beep(1200, 60);
}
void onDoubleClick() {
// Play a lower tone when released
Buzzer_Beep(600, 60);
}
int main(void)
{
gSysClk = SysCtlClockFreqSet(
SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480, 120000000);
timer.begin(gSysClk, TIMER0_BASE);
elapsedMillis buttonCheck(timer);
Buzzer_Init();
btnPlay.begin();
btnPlay.setDebounceMs(30);
btnPlay.attachClick(&onClick);
btnPlay.attachDoubleClick(&onDoubleClick);
while (1) {
if(buttonCheck > 20){
btnPlay.tick(); // Polls and triggers events
buttonCheck = 0;
}
}
}

EventToneDescription
Button Clicked1200 HzShort beep when button is pressed
Button DoubleClicked600 HzLower beep when button is released

You should hear two distinct tones as you interact with the button. The PWM automatically stops after each tone.


  • Keep tones short (40–80 ms) for UI feedback.
  • Use different frequencies to represent events (start, reset, error, etc.).

You can find a complete example project implementing this functionality

This demonstrates how to generate PWM tones with the buzzer and integrate sound feedback into your projects using button events.

Author: Edwin R. Version 1.0