I got a little side tracked while adding an extra feature to a project I've been working on.
The plan was to "quickly add" a little buzzer to sound when the timer hits zero.
However, being the interpreted language it is, Python kicked my butt again while I tried to add it to the main project, and all I got was an annoying never-ending nasty beep. I did not like that. The cat did not like that. The cat gave me a dirty look and left.
I know better than to keep hacking at it like that, so I did what I would recommend any good, not-stubborn, programmer should do in such cases, and that is to spin off the feature into a separate app.
Isolating the code to a test app was a good idea - it very quickly became very clear to me that I was calling the "sleep" function the wrong way. The examples on the line in the webs said "sleep(..)", when what the Pi Pico's microPython really wants is "time.sleep(..)".
But hey! If it wasn't for that bit of annoyance I would not have thought of creating a separate little example, and the world would be one blog post the poorer for it.
So this one is REALLY easy, btw. Assuming you don't make the same typos I made.
All you need it a Raspberry Pi Pico, and a little piezo speaker. Other speaker type things will probably work too. Maybe even an old set of headphones with the plug cut off if you happen to be hoarding one of those.
element14's stores have them:
The Raspberry Pi Pico: https://canada.newark.com/raspberry-pi/raspberry-pi-pico/raspberry-pi-board-arm-cortex/dp/22AJ1097
Piezo speakers: they have a whole line up of them at various price points, but here's one that's just a dollar: https://canada.newark.com/multicomp-pro/abt-414-rc/audio-transducer-2-4khz-85db/dp/93T7245
I also used a breadboard and some header pins to bring it all together, but those details are up to whatever you prefer. You could just solder it on directly as well.
Then simply connect the Piezo speaker between GP6 and the GND pin that's right beside it. Super easy!
The piezo speaker probably has a + marked on, so make sure you put that pin to GP6.
Mine has TWO plusses marked on it, so I can only assume this is important. I'm too scared to try it the other way around. Don't do it. They wouldn't be wasting ink like that if it wasn't life threatening, would they?
Ignore the extra wires in the picture - that's the other project still in progress. Focus on the piezo speaker.
The sound is actually pretty decent considering the speaker and how little effort went into the coding.
The test code I used just plays a few notes, but if you want to get fancy with it, there are some good examples online that include little melodies.
You would probably have to update the notes array to be a 2-dimensional array that holds the frequency as well as the duration of each note.
# # Play a note on a buzzer # from machine import Pin, PWM import time BUZZER_PIN = 6 # Piezo buzzer + is connected to GP6, - is connected to the GND right beside GP6 buzzer = PWM(Pin(BUZZER_PIN, Pin.OUT)) def playNote(frequency, duration, pause) : global buzzer buzzer.duty_u16(5000) # adjust loudness: smaller number is quieter. buzzer.freq(frequency) time.sleep(duration) buzzer.duty_u16(0) # loudness set to 0 = sound off time.sleep(pause) #notes = [440, 494, 523, 587, 659, 698, 784] notes = [440, 523, 659, 784] for note in notes : playNote(note, 0.1, 0.01)
Enjoy!
-Nico