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.

A small circuit board on a breadboard with three round buttons, yellow, orange and teal, and a round grey buzzer beside them. Three musical notes float above it.

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?

  1. Connect the buzzer's positive leg (usually marked +) to GP15.
  2. Connect the buzzer's other leg to any GND pin on the Pico.
  3. Put the three buttons on the breadboard across the middle gap.
  4. Connect one side of the first button to GP10, the second to GP11 and the third to GP12.
  5. 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?

  • NOTES holds 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 NOTES and 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

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.

See the programme