MicroPython alarm on a Pico: a siren on a passive buzzer
MicroPython on a Raspberry Pi Pico: while the button on GP16 is held, PWM on GP15 sweeps a passive buzzer between 1 and 2 kHz like a siren. The whole build, a Raspberry Pi Pico and 6 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.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4.75. On Mokxi they're free.
What you'll learn
- Switching a buzzer with a transistor
- A pull-up resistor on a button
- PWM frequency as pitch
- Using a sine wave for a siren sweep
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Raspberry Pi Pico
- Parts: 9
How it works
A Pico pin cannot power a buzzer well, so GP15 goes through a 1k resistor to the base of a transistor. The buzzer sits between 5 V and the transistor's collector, and the emitter goes to ground. The pin switches the transistor, and the transistor switches the buzzer.
The button on GP16 has a 10k pull-up to 3.3 V, so the pin reads 1 until you press it and 0 while it is held. While held, the pitch swings along a sine wave between 1000 and 2000 Hz at half duty. Let go and the buzzer goes quiet. The code prints "alarm" and "quiet" when that changes.
Parts list
9 parts, plus the jumper wires. They retail for about $4.75; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 2 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Power, 5 V
- 1 × Power, 3.3 V
Kits that can build it
These 2 starter kits have 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 pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 1 | Raspberry Pi Pico pin GP15 |
| Resistor, 10k Ω (2) pin 1 | Raspberry Pi Pico pin GP16 |
| Transistor (NPN or PNP) pin B | Resistor, 1k Ω pin 2 |
| Piezo speaker pin 2 | Transistor (NPN or PNP) pin C |
| Transistor (NPN or PNP) pin E | Ground |
| Piezo speaker pin 1 | 5 V |
| Resistor, 10k Ω (1) pin 1 | 3.3 V |
| Pushbutton pin 1a | Resistor, 10k Ω (1) pin 2 |
| Resistor, 10k Ω (2) pin 2 | Resistor, 10k Ω (1) pin 2 |
| Pushbutton pin 2a | Ground |
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.
# An alarm: while the button on GP16 is held, the passive buzzer behind
# the transistor on GP15 wails, its pitch swinging along a sine wave
# between 1000 Hz and 2000 Hz.
from machine import Pin, PWM
import math
import time
buzzer = PWM(Pin(15))
buzzer.duty_u16(0)
button = Pin(16, Pin.IN)
x = 0
sounding = False
while True:
if button.value() == 0:
x = (x + 10) % 360
buzzer.freq(1500 + int(500 * math.sin(math.radians(x))))
buzzer.duty_u16(32768)
if not sounding:
print("alarm")
sounding = True
else:
buzzer.duty_u16(0)
if sounding:
print("quiet")
sounding = False
time.sleep_ms(10)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The buzzer sounds all the time or never This project
The button reads 0 when pressed. Check the 10k pull-up to 3.3 V and the button leg to ground. - No sound when the button is held This project
Check the transistor: base through the 1k to GP15, emitter to ground, collector to the buzzer. - 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. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.