Introduction
Let's play tones and melodies on a PiicoDev® Buzzer Module with a Micro:bit. This guide will cover connecting the hardware, running an example to make sure everything works and playing simple melodies.
Of course, the Micro:bit V2 already features a buzzer - this guide is still useful if you wish to connect your buzzer to a long PiicoDev Cable and create sounds remotely.
To follow along, it's best to have:
- A Micro:bit V2
- A PiicoDev Buzzer Module
- A PiicoDev Adapter for Micro:bit
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
Contents
- Connect the Buzzer
- Download MicroPython Modules
- Example Code - Read RGB data
- Remix - Fruit Sorting Machine
- Conclusion
- Resources
Connect the PiicoDev Buzzer Module to your Micro:bit
Plug your Micro:bit into the adapter, connect your Buzzer Module to the adapter via the PiicoDev cable, and finally connect your Micro:bit to your computer with a USB lead.
This article and video features the older v10 buzzer. The new v20 Buzzer features four ID switches - so there's no need to check the back side of the v20 design (no solder jumpers).
Ensure both ID switches are OFF, and both ID jumpers (on the back side) are OPEN.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download MicroPython modules
We will need three files to easily drive the Buzzer Module:
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_Buzzer.py (right-click, "save link as")
- Download the example script: main.py (right-click, "save link as")
It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev
Example Code - Play Tones
We'll be working in Thonny - if you're unfamiliar working with Thonny see our guide for Thonny and Micro:bit
Open Thonny, connect to your Micro:bit and navigate to where you stored the files from the last step. Right-click each file and select "Upload to /" to upload them to your Pico (Hint: View the files menu with View > Files)
Restart your Micro:bit (Keyboard shortcut, Ctrl+D) and you should hear some tones play from your Buzzer Module.
This script initialises the Buzzer at the loudest volume and plays two tones. Then, the volume is reduced to the lowest setting, and the two tones repeat. tone() accepts up to two arguments - the first is frequency in Hz, and the second is tone-duration in milliseconds. If no duration argument is given, the tone will sound continuously. This is the case for the second pair of tones, which omit the duration argument: In this case the tone will play continuously until buzz.noTone() is called. Here, the duration is set by the delay, sleep_ms(). buzz.volume() accepts a 0, 1 or 2 - 0 is the quietest and 2 is the loudest.
Melodies
It's possible to play simple melodies on a Buzzer Module. In the following example, a Python Dictionary notes defines musical-note names with their corresponding frequencies (Hz). Then, a 2-dimensional list melody defines the sequence of notes to play in note-duration pairs. For example, the first note is E, played for 500ms. The 10th note is an F, played for 250ms.
Finally, a loop steps through the melody list, extracts the note/duration data, and plays that tone.
Take this example for a spin! Replace the code in main.py with the following and run the script.
# PiicoDev Buzzer melody example. Define a melody as a combination of notes and durations. from PiicoDev_Buzzer import PiicoDev_Buzzer from PiicoDev_Unified import sleep_ms # Define some note-frequency pairs notes = {'C4':262, 'Db':277, 'D' :294, 'Eb':311, 'E' :330, 'F' :349, 'Gb':370, 'G' :392, 'Ab':415, 'A' :440, 'Bb':466, 'B' :494, 'C5':523, 'rest':0, # zero Hertz is the same as no tone at all } # define a melody - two-dimensional list of notes and note-duration (ms) melody = [['E', 500], ['D', 500], ['C4', 500], ['rest', 500], ['E', 500], ['D', 500], ['C4', 500], ['rest', 500], ['G', 500], ['F', 250], ['F', 250], ['E', 500], ['rest', 500], ['G', 500], ['F', 250], ['F', 250], ['E', 500], ['rest', 500], ] buzz = PiicoDev_Buzzer(volume=2) # play the melody for x in melody: noteName = x[0] # extract the note name duration = x[1] # extract the duration buzz.tone(notes[noteName], duration) sleep_ms(duration)
Conclusion
We can now play tones and melodies at different volumes on our PiicoDev Buzzer Module! If you're looking for some inspiration, perhaps pair the buzzer module with a PiicoDev Capacitive Touch Sensor to create a simple 3-key piano.
If you make something cool with this starter project we'd love for you to share it on our forums! And if you'd like some help with this guide, start the conversation below - We're full-time makers and here to help!