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

Let's make some music with the PiicoDev buzzer module and a Raspberry Pi. We'll get these two connected together, run some example code to play some tones, and we'll even play some melodies. Let's do it.

I'm using a Raspberry Pi 4 model B today with a PiicoDev buzzer, a PiicoDev adapter for Raspberry Pi, and a PiicoDev cable to connect everything together. Your Pi will need to be set up to run like a desktop computer. We have a getting started guide for that if you need help.

Plug the adapter onto your Pi, making sure that the ethernet arrow points towards the ethernet connector. On a Raspberry Pi 3 these connectors are swapped, so there'll be a USB connector here. Connect your PiicoDev cable to one of the ports and connect the other end to your buzzer. 100mm cables are best for Raspberry Pi projects.

To run these examples it's important that these two switches on the front are in their default state, that's where both switches are off. The two ID jumpers on the back of the board should also be open or unsoldered. For more information on using PiicoDev modules with these kinds of ID switches, check out the PiicoDev connection guide. And I've mounted everything to this PiicoDev platform to keep it nice and stable.

Power up your Pi, connect to a network, and we'll make sure that we have I2C enabled first. Go to preferences, Raspberry Pi configuration, and under the interfaces tab, make sure that I2C is enabled.

In the article for this tutorial, find the download section to download the example script. Right click that link, save link as, and I'm going to save it to a PiicoDev directory in my home directory. I'll call it buzzer, just for some context.

Navigate to where you downloaded that file and open it in Thonny. And just before we run the script, we need to make sure we have PiicoDev installed. Go to tools, manage packages, and search.For PicoDev, that's with two I's. There it is. And just upgrade or install as necessary. This script is ready to run, so we can just press the green run button or control R to run the script.

We had two tones there, followed by the same two tones, a little bit quieter. Let's take a look at the code. We first import the driver for the PiicoDev buzzer, and we also import a sleep MS function to create a delay in milliseconds. We initialize the buzzer as buzz, and we initialize it with a volume equal to two. There are three volume levels, with two being the loudest and zero being the quietest.

Next up, we create our tones. We call buzz.tone, and this takes up to two arguments. The first is the frequency of the tone in hertz. So this first tone has a frequency of 800 hertz. And the second argument is the duration of the tone in milliseconds. So this has a half second duration. We create a delay, and then we create another tone, this time at 400 Hz for 500 milliseconds. So that's our two tones, that high low tone. In the middle, we call buzz volume zero. So that will set the volume to the lowest level.

And we do the same thing all over again. This time, we create our 800 and 400 Hz tones without a duration in buzz.tone. This will play the tone continuously until we tell it to stop. And we tell it to stop by calling no tone. So here we're creating the buzz duration with this sleep function.

Let's remix this code a little bit to create an ascending tone. I'll change that 800 to a 300, and change that 400 to a 600. Why not? Let's do the same thing to the other two. And press control R to run.

Now let's create a musical melody. Jump back into the article for this tutorial and find the melody section, and we'll copy out that whole code listing. Copy that. Back into Thonny, I'll create a new file, paste in all that code and save it.Let's give it a run with control R. Nice, a short melody.

Looking at the code, we perform our usual imports. And next up, we define some note frequency pairs. Notes is a Python dictionary, which is a list of key value pairs. Here, the keys on the left are the names of the notes that we might want to play. So here's a D note, here's an F, here's an A flat. And the values are the frequencies for those notes. So D has a frequency of 294 hertz. That makes it really easy for us to play the frequency of the note we want by accessing it from this key, which has more meaning.

Then we define melody, which is a two-dimensional list in Python. These are the notes that we want to play in our melody and the durations we want to play them for. So our first note will be an E, and we play that for 500 milliseconds. This F down here is played for 250 milliseconds. So this is our entire melody.

We initialize the buzzer as usual. And then finally, we have a for loop, which steps through element in the melody list, extracting the note name and the duration to play that note. Finally, calling buzz.tone with that information.

And so there you have it, a simple way to play a short melody with the PiicoDev buzzer. Of course, you could then change the definition of this melody to be whatever you like.

So there you have it, a couple of tones and even a simple melody using the PiicoDev buzzer. If you make anything cool out of these starter projects, or if you just have some questions, let us know in our forums. We're full-time makers and here to help. Until next time, thanks for watching.

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.