Let's make some music with the PiicoDev Buzzer Module. We'll start off with some example codes to generate basic tones at different volumes. Then kick it up a notch and create a melody making script. Click through to the guide for all the code and useful extras.

Transcript

I'm going to show you how to play tones and even melodies with your PiicoDev buzzer from a Raspberry Pi Pico. We'll get these two connected and get it working. Let's get started.

You'll of course need a Raspberry Pi Pico with the pin soldered facing down, a PiicoDev buzzer and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect everything together. Before we get started, just make sure both these switches are in the off position. That's the farthest to the left. You can see here that the switches are closest to the numbers, not the on. On the back side, make sure these two pads are unsoldered. This will be how your buzzer arrives from the factory.

Plug your Pico into the expansion board, making sure the USB connector is on the same side as the two pin battery connector. Connect the cable and connect your buzzer to the other end. And I've just mounted everything to this PiicoDev platform to keep it nice and stable. Connect to your computer with a USB cable.

In the download section of the article, find the three files we need to download, right click and save link as. I'm saving these to a PiicoDev directory in my documents. We'll be working with Thonny for this tutorial. If you've never programmed a Pico and Thonny before, check out our getting started guide for that.

Open Thonny, navigate to where you saved your files and connect to your Pico. Click the first file, hold shift, click the last file, right click and upload to. This will upload all the files to our Pico. And if we open main, we can see what we're about to run.

The script is ready to go, so press control D to reboot your Pico and listen for the buzzer.

Taking a look at the script, we import the buzzer driver package and we also import a function to create a delay, sleepms. We initialize our PiicoDev buzzer as buzz and we initialize it with the volume.Parameter 2 is the loudest setting. Then, we call buzz.tone, which takes up to two arguments. The first argument is the frequency of the tone in Hz and the second is the duration of the tone in milliseconds. We first create an 800 Hz tone for half a second, followed by a low-tone of 400 Hz for 500 milliseconds. Then, we change the volume to the quieter setting after a longer delay. We set the volume to zero and create those same two tones again for the same durations. This time, we pass only the frequency into tone, and in this case, the tone will sound continuously until we call no tone. By sleeping for 500 milliseconds and calling no tone, we create the same effect as if we had just called buzz.tone with both arguments. We create a high tone and a low tone, with the frequencies set to 300 and 600 hertz, respectively. When we execute the script, an ascending sequence of tones is produced since we are playing a low tone followed by a high tone.

We will now move to the "melodies" example. We can copy all the code and paste it into main. After pasting, we give it a run. This example is clearly intended to play a melody, and we follow our normal imports. We proceed to create a Python dictionary called "notes," which defines name and frequency pairs. Then, we create a two-dimensional list called "melody," which represents the sequence of notes to play for given durations. The first note is an E played for 500 milliseconds, followed by C4 played for 500 milliseconds. The "rest" note is defined as zero hertz, which means that nothing is played for 500 milliseconds.If you pass tone zero for the frequency, it will play silence. So we have our melody defined as all of these notes and durations. We set up our buzzer as usual and then we have a for loop where we loop through the melody list starting at the top, working all the way to the bottom.

We extract the note name, we extract the duration and then we play that note for that duration. So for the first note in the melody, note name is E and so we access our notes dictionary with for the E entry. So we go to our notes dictionary, grab the E entry and that is 330 hertz. So this first argument resolves as 330 Hz and the duration we extracted is just that 500 milliseconds.

And so you'll notice there are some different duration notes here. This F for example only plays for 250 milliseconds. That's how we can create that faster beat. There you have it, some useful examples for getting started with the PiicoDev buzzer.

We can play basic notes and we can even play melodies. If you make something cool from these starter projects or if you just have some questions, let us know on the forums. We're full-time makers and here to help. Catch you next time.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.