Hardik's Notes · Build
Build it this weekend: a three-button piano
Three buttons, one buzzer and 20 lines of MicroPython on a Raspberry Pi Pico. Each button plays a different note.

What will you make?
A three-key instrument. Hold a button and the buzzer plays a note. Let go and it goes quiet. Press two at once and the first button wins.
What do you need?
- A Raspberry Pi Pico (or Pico W) with MicroPython installed
- One small passive buzzer (not an active one: see the catch below)
- Three push buttons
- A breadboard and about eight jumper wires
- A USB cable and a computer with Thonny or another MicroPython editor
How do you wire it?
- Connect the buzzer's positive leg (usually marked +) to GP15.
- Connect the buzzer's other leg to any GND pin on the Pico.
- Put the three buttons on the breadboard across the middle gap.
- Connect one side of the first button to GP10, the second to GP11 and the third to GP12.
- Connect the other side of every button to GND.
The buttons need no resistors. The code turns on the Pico’s own pull-up resistor (a tiny resistor inside the chip). A pin reads 1 when its button is up and 0 while it is pressed.
How does a Pico make a sound?
A buzzer makes sound when it is pushed in and out. The Pico does this with PWM (pulse width modulation: a pin switching between on and off, many times a second).
- Frequency is how many times a second the pin switches. A higher number gives a higher note.
- Duty is how long the pin stays on in each switch. MicroPython takes it as a number from 0 to 65535. Half of that, 32768, is on half the time.
Set the duty to 0 and the pin stays off, so the buzzer is silent.
What is the code?
Save this as main.py on the Pico so it runs when you plug it in.
from machine import Pin, PWM
import time
NOTES = [262, 330, 392] # C4, E4, G4 in hertz
buzzer = PWM(Pin(15), freq=262, duty_u16=0) # starts silent
buttons = [Pin(10, Pin.IN, Pin.PULL_UP),
Pin(11, Pin.IN, Pin.PULL_UP),
Pin(12, Pin.IN, Pin.PULL_UP)]
while True:
playing = False
for i in range(len(buttons)):
if buttons[i].value() == 0: # 0 means pressed
buzzer.freq(NOTES[i])
buzzer.duty_u16(32768) # on half the time
playing = True
break
if not playing:
buzzer.duty_u16(0)
time.sleep_ms(10)
How does it work?
NOTESholds one frequency per button, in hertz (times a second).- The loop checks each button in turn. The first pressed one sets the pitch and the duty.
- If no button is pressed, the duty goes to 0 and the sound stops.
time.sleep_ms(10)pauses for 10 milliseconds so the loop does not run flat out.
If it doesn’t work
- No sound at all. Check the buzzer’s + leg goes to GP15. Check the file is saved as
main.py, then unplug and replug the Pico. - One flat click, the same for every button. Your buzzer is probably an active one. It makes one fixed tone and ignores the frequency. Swap it for a passive one.
- A button does nothing. One side must go to its GP pin and the other to GND.
- A note plays by itself. A button wire is loose or on the wrong row.
Make it yours
- Change the numbers in
NOTESand play your own tune. - Add a fourth and fifth button for a whole scale.
- Add an LED that lights while a note plays.
- Make a tune that plays on its own when you press a fourth button.
What’s the catch?
A buzzer is a small, thin-sounding speaker. It is a good way to learn how pitch works. It will not sound like a piano. The note numbers here are rounded, so a tuner app may read them a little off.
Sources
- machine.PWM, MicroPython documentation
- Quick reference for the RP2, MicroPython documentation
- machine.Pin, MicroPython documentation
Keep reading
Three more from the notebook.
17 November 2026
An AI that picks an answer instead of writing one
A new kind of AI model does not write text. It picks from options you give it and says how sure it is. Here is why that is fast, and where it fails.Read it15 November 2026
An open AI rain map of the whole Earth
Google DeepMind released PARDIG, a free global rain dataset made by AI. It beat other rain maps in the authors' tests.Read it14 November 2026
An AI copilot helps design stronger structures
MIT researchers made a design program that asks a person where to change a bracket. In one test it held 39% more load.Read it
Beyond the notebook
This is what we do in schools.
A year-long programme for classes 6 to 12. Creativity, taught as a skill, inside the classroom you already have, led by your teachers.