Piezo buzzers are basic components with versatility to emit tones, beeps, and even melodies. In this codelab, learn how to set up a piezo buzzer with your Raspberry Pi, and start making sound with just a few lines of code. Whether you're building a fun alert system or adding sound effects to a DIY project, you can integrate a piezo buzzer into your Viam project quickly using a prebuilt module.

close up view of piezo

What You'll Build

piezo with megaphone

Prerequisites

What You'll Need

What You'll Learn

Watch the Video

See a demonstration sounding the piezo buzzer in this video.

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 authentication. raspberry pi enable SSH
  11. 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.
  12. 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.
  2. 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
    
  3. 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
  4. Update your Raspberry Pi to ensure all the latest packages are installed
    sudo apt update
    sudo apt upgrade
    

Add your piezo buzzer

  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

photo of webcam and piezo connected to Raspberry Pi

Now that we have physically connected our hardware components, let's configure the software in the next section.

Configure your machine

  1. In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine. add machine
  2. Click View setup instructions. setup 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-agent. select 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 Component. add 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
  7. Set the signal to Low to turn off the buzzer.

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"
    }
    
  4. Select your board board-1 from the Depends on field. configure piezo
  5. Click Save to apply your configuration changes. This may take a moment.
  6. 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
      }
    }
    
    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.
  7. 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": {}
    }
    

What is the primary purpose for using PWM (Pulse Width Modulation) to control a passive piezo buzzer?

To provide a constant high voltage to the buzzer. To generate varying tones by controlling the frequency of the signal. To make sure the buzzer operates at a fixed tone without additional wiring. To power the buzzer directly from a USB port.

What you learned

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.

piezo enclosure

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.

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

Real-world applications and projects for piezo buzzers

Piezo buzzers are small, but mighty components used in industries requiring audible feedback, alerts, or tone generation. Their applications include:

Related Viam resources