STM32 Buzzer | Piezo Buzzer Example + Tone [Active & Passive]

In this tutorial, we’ll learn how to interface STM32 With Buzzer both Active & Passive Buzzers. We’ll also develop an STM32 PWM-based Tone() and noTone() functions to generate melody tones with STM32 and Passive buzzers.

We’ll start with the differences between Active Buzzers vs Passive Buzzers. Then, we’ll discuss how to interface each of them with STM32 microcontrollers with a couple of code examples to practice what we’ll learn. Without further ado, let’s jump right into it!

Table of Contents

  1. STM32 Buzzer Interfacing
  2. STM32 Active Buzzer Example
  3. STM32 Buzzer Tone Function
  4. STM32 Passive Buzzer Example
  5. Wrap Up

STM32 Buzzer Interfacing

Piezo buzzers are a type of audio transducer that can be used to generate sound. They are a popular choice for simple sound effects, alarms, and tone-generation applications due to their small size, low cost, and ease of use. Here is an interactive tool that you can play with to experience what a buzzer sounds like!

A piezo buzzer is a polarized electronic device which means it has a positive lead & a negative lead. It needs a voltage of 3.3v up to 12v to work, the working voltage is stated on the manufacturer page so make sure you get a 5v buzzer which is going to work just as fine with all microcontrollers.

Arduino-Piezo-Buzzer

Applying voltage to the piezo buzzer terminals will cause a slight deformation in the piezo disc and by changing this voltage the piezo disc will vibrate and produce the buzzer sound we all know. This is called a Passive Buzzer.

Another type of buzzer doesn’t need a varying voltage, you just give it constant voltage and it has an internal chopper (oscillator) circuit that turns the constant voltage to a square wave and feeds that to the piezo disc to generate a fixed-frequency tone. This type is called an Active Buzzer.

Active Buzzer vs Passive Buzzer Article

This tutorial will provide you with more in-depth information about the differences between Active Buzzers & Passive Buzzers and the technical specs and construction for each type.

STM32 With Active & Passive Buzzers

To interface our STM32 microcontroller with a buzzer (active or passive), we’ll use an NPN transistor (2n2222, 2n3904, or any NPN BJT). The transistor’s base will be connected to a GPIO output pin of the STM32 microcontroller for driving an Active Buzzer, or a PWM pin for driving a Passive Buzzer.

The wiring diagram that we’ll use for the two example projects hereafter in this tutorial is shown below.

STM32-Buzzer-Interfacing-Piezo-Buzzer-Active-Passive-Wiring


STM32 Active Buzzer Example

In this example, we’ll interface an Active Buzzer With STM32 using a GPIO output pin. An active buzzer generates a fixed tone as long as it’s driven HIGH. Therefore, we’ll only need 2x GPIO pins for this example. One GPIO pin is configured as an input pin to read a push button and the other GPIO pin is configured as an output pin to drive the buzzer.

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller.

Step #2

Configure the GPIO Pin A0 as an Output pin.

Configure the GPIO Pin B0 as an Input pin.

Step #3

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

STM32 RCC External Clock Selection CubeMX

Step #4

Go to the clock configurations page, and select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #5

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Buzzer Example Code

Here is The Application Code For This LAB (main.c)

Wiring

STM32-Buzzer-Interfacing-Piezo-Buzzer-Active-Passive-Examples

STM32 Active Buzzer Example Testing Demo


STM32 Buzzer Tone Function

As stated earlier in this tutorial, the passive buzzers need to be driven with an AC signal to generate sound. Therefore, we’ll develop a Tone() function to generate variable frequency & duration tones with STM32’s PWM output.

This function assumes that the PWM hardware is configured in such a way that a single timer tick has a period of 1µs. For example, the STM32F103 (BluePill) is configured to operate at a 72MHz system clock. Therefore, the PWM timer has an input clock of 72MHz as well. So, we need to set the timer’s prescaler to 71 so that the system clock is divided by (PSC+1) or (71+1). Now, the single timer tick time is 72MHz/72 = 1µs.

STM32 PWM Frequency Formula - STM32 PWM Frequency Equation

For other STM32 microcontrollers with different system clock frequencies, you need to do the same and set the PWM Timer’s prescaler in such a way that guarantees a timer single tick time of 1µs.

Now, it’s easy to control the output FPWM by only changing one parameter which is the ARR (auto-reload register) as shown in the equation above. The OCR register of the PWM channel will always be half of the ARR value to achieve a 50% duty cycle for the output PWM waveform.

Here is the final Tone() function’s implementation.

And this is the noTone() function’s implementation.

We’ll use both functions in the next example for interfacing passive buzzers with the STM32 microcontroller.


STM32 Passive Buzzer Example

In this example, we’ll interface a Passive Buzzer With STM32 using a PWM output pin. We’ll configure one GPIO pin as an input pin to read a push button and a PWM output channel to drive the passive buzzer.

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller.

Step #2

Configure the GPIO Pin B0 as an Input pin.

Step #3

Configure Timer2 PWM With Output On CH1 with the following settings

STM32-Buzzer-Tone-Passive-Buzzer-Code-Example-Configuration

Step #4

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

STM32 RCC External Clock Selection CubeMX

Step #5

Go to the clock configurations page, and select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #6

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Piezo Buzzer Example Code

Here is The Application Code For This LAB (main.c)

Wiring

STM32-Buzzer-Interfacing-Piezo-Buzzer-Active-Passive-Examples

STM32 Passive Buzzer Example Testing Demo


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored different types of buzzers (active & passive) and how to interface STM32 with Buzzers using a BJT transistor. We’ve also developed a Tone() function to generate variable frequency melody tones with passive buzzers.

You can build on top of the examples provided in this tutorial and/or explore the other STM32 Tutorials on our website.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment