Built-in project · Raspberry Pi Pico W

Pico W passive buzzer plays notes with PWM

MicroPython on a Pico W: PWM on GP15 drives a passive buzzer through an S8050 and plays 440, 494 and 523 Hz, a quarter second each. The whole build, a Raspberry Pi Pico W and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $6.55. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A passive buzzer needs a changing signal
  • Frequency sets pitch
  • Using a transistor as a switch
  • Looping over a list of notes
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 5

How it works

A passive buzzer makes no sound by itself. It needs a signal that switches on and off quickly, and the speed of the switching sets the pitch. GP15 sends PWM through a 1k resistor to the base of a transistor, which switches the buzzer on and off.

The program plays three notes in turn, 440, 494 and 523 Hz, each for a quarter second with a quarter second of quiet. Then it waits one second and plays them again.

Parts list

5 parts, plus the jumper wires. They retail for about $6.55; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 1k Ω pin 1Raspberry Pi Pico W pin GP15
Piezo speaker pin 13.3 V
Transistor (NPN or PNP) pin CPiezo speaker pin 2
Resistor, 1k Ω pin 2Transistor (NPN or PNP) pin B
Transistor (NPN or PNP) pin EGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Custom tone: a passive buzzer needs a square wave, so GP15 is a PWM
# pin whose frequency is the note: A, B and C, a quarter second each.
from machine import Pin, PWM
import time

buzzer = PWM(Pin(15))

def tone(hz, ms):
    buzzer.freq(hz)
    buzzer.duty_u16(30000)
    time.sleep_ms(ms)
    buzzer.duty_u16(0)

while True:
    for hz in (440, 494, 523):
        print("note", hz)
        tone(hz, 250)
        time.sleep_ms(250)
    time.sleep(1)

Code walkthrough

The key functions of the sketch, and what each one does.

tone main.py

Plays one note. It sets the PWM frequency to hz, sets the duty to 30000 so the buzzer sounds, waits ms milliseconds, then sets the duty to 0 to go silent. A duty of 0 means the pin stays low.

def tone(hz, ms):
    buzzer.freq(hz)
    buzzer.duty_u16(30000)
    time.sleep_ms(ms)
    buzzer.duty_u16(0)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • No sound. This project
    Check the transistor: the collector (C) goes to the buzzer, the base (B) to the 1k resistor and the emitter (E) to ground.
  • The sound stays on or is one tone. This project
    Make sure the loop calls tone for each note and that buzzer.duty_u16(0) runs after each one.
  • A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
    A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
  • Sound is very quiet or the pin seems overloaded. Piezo speaker
    Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
  • The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
    Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong.
  • The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
    Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.