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. We'll connect this with a Micro:bit v2, get some example tones running, and I'll even show you how to create some melodies. Let's get started.

Now of course the Micro:bit v2 already has a buzzer on board, but this guide will still be useful if you want to separate your Micro:bit from the source of the sound.

To follow along you'll of course need a Micro:bit v2, a PiicoDev buzzer and adapter for Micro:Bit, and a PiicoDev cable to connect them together. Start by plugging your Micro:bit into the adapter, making sure the buttons are facing up. Connect your PiicoDev cable to the adapter and connect the other end to your buzzer.

Just before we get started, make sure these two switches are in the off position. These are the id switches so you can connect to multiple buzzers. Also make sure that these two id jumpers are unsoldered or open. To learn more about PiicoDev modules with these kind of features, check out our guide for smart modules. And I've mounted everything to a PiicoDev platform to keep it nice and stable.

Finally connect your Micro:bit to your computer with a USB lead. In the article for this tutorial find the download section and right click each link and save link as. I'm saving these to a PiicoDev directory in my documents. We're going to work with Thonny in this tutorial. If you haven't used Thonny with the Micro:bit before then check out our guide for that, and if you prefer to use a programming environment that doesn't require installing, we have a guide for python.MicroBit.org.

Open Thonny, connect to your Micro:Bit, select the first file, hold shift, select the last file, right click and upload to Micro:bit to upload all the files. And here they are. These two are drivers. We're going to work with main.py.

This file is ready to run so we can just press the green run button. This script is playing a high tone then a low tone.

Taking a look at the script, we first import the driver for the buzzer and import a function to create a delay, sleepms.

We initialize the buzzer and call it buzz, and we initialize it with volume equal to two, which is the highest volume setting.

Next, we call buzz.tone and pass in 500, which is the frequency in hertz. The tone will play for 500 milliseconds. After that, we play a lower tone of 400 Hz for 500 milliseconds.

We then call buzz.volume and pass in zero for the lowest volume possible. We play the same two tones continuously, passing in just the frequency. The tones will play until we call no tone 500 milliseconds later after the sleep function. We do the same with the 400 Hz tone and then stop it with no tone.

To remix this code, we can change the frequencies to go from 300 to 600 Hz and 300 and 600 here as well.

Returning to the article, we can then grab the melody example. We'll paste it into Thonny, overwriting the main. This is ready to run, so we can just click the run button. The melody played is 'Three Blind Mice'.

Looking at the script for the melody example, we import the driver for the buzzer. We also have a Python dictionary called notes that associates a key like the note name with a frequency value.Octave.

Next up, we define a Python list melody, and this is a note duration pair. So each note in this list has a duration associated with it. This "e" will be played for 500 milliseconds, then "d" gets played for 500 milliseconds. All the way down, you can see we have an "f" that's played for a shorter amount of time, 250 milliseconds.

After defining our melody, we set up the buzzer, and then we call a for loop, and that steps through every entry in melody, extracting the note name which is say "e" and extracting the duration which is that number on the right there. And then we just call buzz.tone with those two values. We take the note name and use that as the key for our notes dictionary and we play the note for that duration.

In music, silence is called a rest, and you can see in our notes dictionary that rest defines zero hertz. What that means is if we call tone with zero hertz, that will play silence. It will rest for that duration.

There you have it, a simple melody. Have a go at changing these note duration pairs to something else to create your own melody.

There you have it, creating tones and playing melodies on a PiicoDev buzzer. If you make something cool from these starter projects or if you just have some questions let us know on our forums. We're full-time makers and happy 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.