Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsEach musical note is a frequency: A above middle C is 440 Hz. The Pico's PWM sets the frequency and the piezo turns it into sound. The song is just a list of note names and lengths, so children can type in a new tune, which makes it a gentle way into lists and loops.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico melody player on a piezo (MicroPython)
from machine import Pin, PWM
import time
buzzer = PWM(Pin(15))
button = Pin(16, Pin.IN, Pin.PULL_UP)
NOTES = {"C4": 262, "D4": 294, "E4": 330, "F4": 349, "G4": 392, "A4": 440, "B4": 494, "C5": 523, "-": 0}
# Twinkle Twinkle Little Star: (note, beats)
SONG = [("C4", 1), ("C4", 1), ("G4", 1), ("G4", 1), ("A4", 1), ("A4", 1), ("G4", 2),
("F4", 1), ("F4", 1), ("E4", 1), ("E4", 1), ("D4", 1), ("D4", 1), ("C4", 2)]
BEAT_MS = 350
def play(name, beats):
f = NOTES[name]
if f:
buzzer.freq(f)
buzzer.duty_u16(32768) # half on, half off = loudest square wave
time.sleep_ms(int(beats * BEAT_MS * 0.9))
buzzer.duty_u16(0) # short gap so repeated notes sound separate
time.sleep_ms(int(beats * BEAT_MS * 0.1))
print("Press the button to play")
while True:
if button.value() == 0:
for name, beats in SONG:
play(name, beats)
time.sleep_ms(20)
Normal for a bare piezo; mount it on a small box to resonate.
The button is rotated 90°; legs on the same side are already connected. Turn it.
5 parts: Piezo Buzzer (passive), Tactile Push Button Switch, Raspberry Pi Pico, Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 45 minutes. The build on this page has 4 steps.
No. This page has the complete, tested code (main.py).
About ₹487 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Generating musical notes with tone(); Pull-ups and debouncing.
Ideas that reuse this build: Musical gift box and Keypad mini piano. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.