Projects / Pico melody box

Pico melody box

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. PICO

    Wire the buzzer

    GP15 through 330 ohms, minus to GND.

  2. PICO

    Wire the button

    GP14 to GND.

  3. PICO

    Save the program as main.py

    On the Pico.

  4. PICO

    Press the button briefly

    Tune one plays.

  5. PICO

    Hold the button for a second

    Tune two plays.

  6. PICO

    Add your own tune

    Write a new list and call play with it.

In short

How do you build a pico melody box?

A Pico plays short tunes on a passive buzzer with PWM, choosing between two tunes with a short or long button press. You need Raspberry Pi Pico H, Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Passive buzzer plus330 ohm resistor to Pico GP15 pin 20Tone output
Passive buzzer minusPico GNDReturn
ButtonPico GP14 pin 19 and GNDShort press for tune one, long press for tune two

Code

A tune is a list of pitches and lengths. Each note here is a pair, a frequency in hertz and a time in milliseconds, kept in a list, and the player walks through the list sounding each note for most of its time with a short gap after it. A passive buzzer has no tone circuit of its own, so the Pico provides one: a PWM output set to the note frequency and a duty of 50 percent makes a square wave, and a duty of zero is silence. Doubling a frequency raises the pitch by an octave, and the notes here lie between middle C and the A above it. To add your own tune, write the notes as pairs and call the player with the list. The program measures how long the button is held, and a press of 0.8 seconds or more plays the second tune. The 330 ohm resistor limits the pin current. Keep the volume modest and the buzzer away from your ear.

import time
from machine import Pin, PWM

buzzer = PWM(Pin(15))
button = Pin(14, Pin.IN, Pin.PULL_UP)

C4, D4, E4, F4, G4, A4 = 262, 294, 330, 349, 392, 440

# Each note is (frequency in hertz, length in milliseconds)
ODE = [(E4,400),(E4,400),(F4,400),(G4,400),(G4,400),(F4,400),(E4,400),(D4,400),
       (C4,400),(C4,400),(D4,400),(E4,400),(E4,600),(D4,200),(D4,800)]
TWINKLE = [(C4,400),(C4,400),(G4,400),(G4,400),(A4,400),(A4,400),(G4,800),
           (F4,400),(F4,400),(E4,400),(E4,400),(D4,400),(D4,400),(C4,800)]

def play(tune):
    for freq, ms in tune:
        buzzer.freq(freq)
        buzzer.duty_u16(32768)                   # 50 percent duty makes a square wave
        time.sleep_ms(ms * 9 // 10)              # sound for 90 percent of the note
        buzzer.duty_u16(0)
        time.sleep_ms(ms // 10)                  # short gap so repeated notes are heard

buzzer.duty_u16(0)
while True:
    if button.value() == 0:
        pressed_at = time.ticks_ms()
        while button.value() == 0:
            pass                                 # wait for release
        time.sleep_ms(40)
        if time.ticks_diff(time.ticks_ms(), pressed_at) >= 800:
            play(TWINKLE)
        else:
            play(ODE)

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Short pressTune one plays in full
Long pressTune two plays
Repeated notesHeard as separate notes
PitchRises and falls as in the tune
No pressSilence

If it does not work

No sound

Check the buzzer is passive and wired to GP15.

Notes run together

Increase the gap fraction in play.

Out of tune

Check the frequency numbers in the list.

Long press not recognised

Hold the button for a full second.

Program does not start on power-up

Save it on the Pico as main.py.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pico melody box?

Raspberry Pi Pico H, Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the pico melody box take to build?

About 120 minutes for a intermediate maker, in 6 steps.

How do I know the pico melody box works?

Short press: Tune one plays in full. Long press: Tune two plays. Repeated notes: Heard as separate notes.

No sound: what should I check?

Check the buzzer is passive and wired to GP15.

Notes run together: what should I check?

Increase the gap fraction in play.

Out of tune: what should I check?

Check the frequency numbers in the list.

Long press not recognised: what should I check?

Hold the button for a full second.