How to Use a Piezo Buzzer With a Raspberry Pi

by joyceJetson in Circuits > Raspberry Pi

118 Views, 3 Favorites, 0 Comments

How to Use a Piezo Buzzer With a Raspberry Pi

PXL_20250103_165858778.MP.jpg
PXL_20250103_171335032.MP.jpg

I needed a module for a piezo buzzer that could do more than just beep - it had to multitask! I wanted to sound a quick alert when my air quality monitor picked up on too much particulate and play the right melody when my people detection system spotted either a friend or a foe.

So I created a reusable module that I can add to any of my current and future projects.

Supplies

  1. Sign up for a free Viam account, and then sign in to the Viam app
  2. Hardware and supplies requirements
  3. 1 - Raspberry Pi 4
  4. 1 - microSD card to use with your Pi
  5. 1 - power supply for your Pi
  6. 1 - passive piezo buzzer
  7. 2 - jumper wires

Set Up Your Piezo Buzzer

Piezo buzzer | DEMO
  1. Connect the piezo buzzer: The passive piezo buzzer can be controlled via a GPIO pin on the Raspberry Pi. Refer to the following wiring diagram to connect the Raspberry Pi to the piezo buzzer.

wiring diagram

  1. Pin 35 (GPIO 19) to Positive
  2. Pin 39 (GND) to Negative
The website pinout.xyz is a helpful resource with the exact layout and role of each pin for Raspberry Pi. When working with Viam, make sure to reference the physical pin numbers, and not the GPIO numbers listed on pinout.xyz.

photo of webcam and piezo connected to Raspberry Pi


Set Up Your Raspberry Pi

The Raspberry Pi boots from a USB flash drive (or microSD card). You need to install Raspberry Pi OS on a USB flash drive that you will use with your Pi. For more details about alternative methods of setting up your Raspberry Pi, refer to the Viam docs.

Install Raspberry Pi OS

  1. Connect the USB flash drive (or microSD card) to your computer.
  2. Download the Raspberry Pi Imager and launch it. raspberry pi imager
  3. Click CHOOSE DEVICE. Select your model of Pi, which is Raspberry Pi 4.
  4. Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
  5. Click CHOOSE STORAGE. From the list of devices, select the USB flash drive you intend to use in your Raspberry Pi. raspberry pi storage
  6. Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS.
  7. Check Set hostname and enter the name you would like to access the Pi by in that field, for example, test.
  8. Select the checkbox for Set username and password and set a username (for example, your first name) that you will use to log into the Pi. If you skip this step, the default username will be pi (not recommended for security reasons). And specify a password.
  9. Connect your Pi to Wi-Fi so that you can run viam-server wirelessly. Check Configure wireless LAN and enter your wireless network credentials. SSID (short for Service Set Identifier) is your Wi-Fi network name, and password is the network password. Change the section Wireless LAN country to where your router is currently being operated. raspberry pi hostname username and password
  10. Select the SERVICES tab, check Enable SSH, and select Use password authenticationraspberry pi enable SSH
Be sure that you remember the hostname and username you set, as you will need this when you SSH into your Pi.
  1. Save your updates, and confirm YES to apply OS customization settings. Confirm YES to erase data on the USB flash drive. You may also be prompted by your operating system to enter an administrator password. After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to the USB flash drive.
  2. Remove the USB flash drive from your computer when the installation is complete.

Connect with SSH

  1. Place the USB flash drive into your Raspberry Pi and boot the Pi by plugging it in to an outlet. A red LED will turn on to indicate that the Pi is connected to power.
Make sure you are using a 5V 3A USB-C power supply to power your Raspberry Pi 4. Using a power supply with inadequate amperage can lead to instability, throttling, or unexpected behavior. Additionally, while USB boot is enabled by default on newer Raspberry Pi 4 models, older versions may require a firmware update to enable it. Refer to the Raspberry Pi documentation for detailed setup instructions and compatibility.
  1. Once the Pi is started, connect to it with SSH. From a command line terminal window, enter the following command. The text in <> should be replaced (including the < and > symbols themselves) with the user and hostname you configured when you set up your Pi.
ssh <USERNAME>@<HOSTNAME>.local
  1. If you are prompted "Are you sure you want to continue connecting?", type "yes" and hit enter. Then, enter the password for your username. You should be greeted by a login message and a command prompt. raspberry pi SSH login
  2. Update your Raspberry Pi to ensure all the latest packages are installed
sudo apt update
sudo apt upgrade


Configure Your Machine and Peripherals

Configure your machine

  1. In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machineadd machine
  2. Click View setup instructionssetup instructions
  3. To install viam-server on the Raspberry Pi device that you want to use to communicate with and control your webcam, select the Linux / Aarch64 platform for the Raspberry Pi, and leave your installation method as viam-agentselect platform
  4. Use the viam-agent to download and install viam-server on your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi. installation agent
  5. The setup page will indicate when the machine is successfully connected. successful toast

Configure your Raspberry Pi board

To access the GPIO pins, let's add our Raspberry Pi board to our machine in the Viam app.

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Componentadd component
  3. Select board, and find the raspberry-pi:rpi4 module. This adds the module for working with the Raspberry Pi 4's GPIO pins. Leave the default name board-1 for now.
  4. Notice adding this module adds the board hardware component called board-1. The collapsible card on the right corresponds to the part listed in the left sidebar. added board
  5. Click Save in the top right to save and apply your configuration changes.
  6. Expand the TEST section of the panel to experiment with writing to physical pin 35. For example, try setting a High signal, along with 70% duty cycle and 1200 Hz frequency. test GPIO pins on board
Since we are using a passive buzzer, you must provide a PWM (Pulse Width Modulation) signal to generate different tones. A constant high signal will not produce a sound as they would for an active buzzer, since passive buzzers require varying frequencies to produce audible tones.
  1. Set the signal to Low to turn off the buzzer.
TROUBLESHOOTING: Double check the wiring on your piezo component. Also check under the LOGS tab to see what might be going wrong.

You can manually and programmatically use the GPIO pins of the board component to send PWM signals to control your buzzer. However, to streamline the following steps, let's use a prebuilt module from the Viam registry in the next section.

Configure your piezo buzzer

  1. In the Viam app under the CONFIGURE tab, click the + icon in the left-hand menu and select Component.
  2. Select generic, and find the buzzer:piezo module. This adds the module for controlling your buzzer. Name the component piezo. select generic piezo component
  3. In the new piezo panel, configure your component by adding the following attributes in the CONFIGURE field. This tells your piezo component to use a specific pin (physical pin 35) on a specific board (called board-1 in the Viam app).
{
"piezo_pin": "35",
"board": "board-1"
}
  1. Select your board board-1 from the Depends on field. configure piezo
  2. Click Save to apply your configuration changes. This may take a moment.
  3. Since we are using a generic component, let's test it out under the CONTROL tab. Find your piezo component on this page. Expand the DO COMMAND field, input the following code, and hit Execute.
{
"sound_buzzer": {
"frequency": 1200,
"duration": 1.5,
"duty_cycle": 0.7
}
}
  1. test piezo You are executing a DoCommand on a generic component that has been predefined to accept parameters such as frequency, duration, and duty_cycle.
  2. Experiment with different values in those fields to see what happens when you execute the command again. If you have extra time, try sending the following Do command.
{
"play_harry_potter": {}
}


Finishing Touches

PXL_20250103_171342203.MP.jpg

You can 3-D print a case for the Raspberry Pi that allows access to your GPIO pins for expanding your project (like this one here). You can also design and 3-D print a custom mount to hold your buzzer in place, or 3-D print a megaphone (like this one here) to amplify the buzzer volume.

Building advanced scenarios with Viam and piezo buzzers

At this point, you have configured and tested your machine and piezo buzzer to make sound, but nothing is happening automatically. You can create automatic processes that trigger actions when certain conditions are met, such as in the following examples.

  1. People detection alert with Computer Vision
  2. Automate air filtration with air quality sensors
  3. Use a QR code scanner

In addition to the project ideas mentioned above, consider other ways to continue your journey with Viam.

  1. Browse other modules in the Viam registry similar to the prebuilt module for the buzzer.
  2. Learn how to create your own module with custom functionality for the piezo buzzer or other components and services.
This tutorial was originally published on Viam codelabs: Use a piezo buzzer with a Raspberry Pi.