Command a Buzzer Tone With the Raspberry Pi Pico

The Raspberry Pi Pico is a low-cost microcontroller board that makes it easy for beginners to get started with electronics projects and learn how to code.

For this project, you will learn how to read an analog signal from a potentiometer and convert it into a PWM (pulse-width modulation) signal to manipulate the frequency, or tone, of a buzzer with the help of MicroPython code.

4

What Parts Are Needed?

This project is based on theKitronik Inventor’s Kit for Raspberry Pi Pico.All the required electronic components are included in the kit; however, these are common components that you may have lying around:

If you’re new to pulse-width modulation (PWM) and potentiometers, first check out our guide onhow to use a potentiometer with your Raspberry Pi Pico, which outlines how to use it to adjust the brightness of an LED with PWM.

sound wave

Required Assembly

One jumper wire (yellow in the photo) connects the left side of the potentiometer to the positive (+) rail of the breadboard. Another jumper wire connects the right side of the potentiometer to the negative (-) side of the breadboard. From the middle pin of the potentiometer, you will need to run a jumper wire to the GP26/A0 pin on the Pico.

The piezo buzzer will need to have one wire going from its negative leg to the negative breadboard rail and then another connection from its positive leg to the GP15 pin on the Raspberry Pi Pico.

Electronics and Wiring on Breadboard

You’ll also need to run a jumper wire from a GND pin on Pico to the negative rail on the breadboard, to ground it. Another jumper wire will connect the 3V3 Out pin on the Pico to the positive rail of the breadboard, to power the components.

Create the Code

You can grab the code from theMUO GitHub repository. Download the MicroPython file namedpiezo-buzzer.pyand then load this onto your Pico via a USB-connected computer running the Thonny IDE. Check out how toget started with MicroPython on the Raspberry Pi Picofor details.

The various parts of the code do the following:

code listing for Pico buzzer project

In summary, there are hundreds of pulses being sent per second and the buzzer tone will shift between 120Hz and 5kHz as the potentiometer is turned clockwise or counter-clockwise. Rotating the potentiometer alters the voltage that’s read by the Pico’s analog input pin, which in turn is used to adjust the buzzer frequency using PWM.

Run the code from Thonny (click the play icon or pressF5on your keyboard) and try it out for yourself. After your first run, will any code changes impact physical outcomes? For example, what happens if you change therange(0 to 65535)? This part of the code is located just belowwhile True:where thefrequencyis defined.

Picto intruder alarm

Setting the Tone

If you’re feeling adventurous, you may want to try using the buzzer to generate musical tones using martinkooij’spi-pico-toneslibrary on GitHub. By default, this library will generate sine waves; four tone generators can run on four different Pico pins as you may choose. Note that this project is based on C++ using the Raspberry Pi Pico SDK, rather than MicroPython, but full instructions are given in the GitHub readme.

The Buzz of Pico Electronics

Congratulations: you’ve learned how to read the analog input from a potentiometer and convert it into a PWM signal to control a buzzer tone. A potentiometer is versatile input device for electronics. A piezo buzzer is another handy component: with the addition of a PIR infrared motion sensor, for instance, you could detect the presence of intruders and sound the alarm.

Connect a PIR sensor to your Pico to detect intruders and sound the alarm

Your phone’s camera app doesn’t show this, so it’s easy to miss.

The fix was buried in one tiny toggle.

The best features aren’t the ones being advertised.

My iPhone does it all, but I still need my dumb phone.

Unlock a world of entertainment possibilities with this clever TV hack.

Technology Explained

PC & Mobile