Built-in project · Raspberry Pi Pico

MicroPython doorbell on a Pico: button and buzzer

MicroPython on a Raspberry Pi Pico: while the button on GP16 is held, GP15 turns on an S8050 transistor and an active buzzer on 5 V sounds. 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.

Beginner 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Switching an active buzzer with a transistor
  • Reading a button with a pull-up resistor
  • Responding only to a change in state
  • Base resistor for a transistor
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Raspberry Pi Pico
  • Parts: 9

How it works

The button on GP16 has a 10k resistor to 3.3 V, so the pin reads 1 until you press and 0 while held. An active buzzer makes its own tone, so it only needs power. A transistor switches that power, and GP15 drives its base through a 1k resistor.

The program checks the button every 10 ms. While it is held, GP15 goes high, the transistor turns on and the buzzer sounds from the 5 V supply. When you let go, the sound stops. The code prints "ding" or "quiet" only when the state changes.

Parts list

9 parts, plus the jumper wires. They retail for about $4.75; 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 pin GP15
Resistor, 10k Ω (2) pin 1Raspberry Pi Pico pin GP16
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
Buzzer pin 2Transistor (NPN or PNP) pin C
Transistor (NPN or PNP) pin EGround
Buzzer pin 15 V
Resistor, 10k Ω (1) pin 13.3 V
Pushbutton pin 1aResistor, 10k Ω (1) pin 2
Resistor, 10k Ω (2) pin 2Resistor, 10k Ω (1) pin 2
Pushbutton pin 2aGround

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
# A doorbell: while the button on GP16 is held, GP15 switches the S8050
# transistor on, and the active buzzer on VBUS sounds.
from machine import Pin
import time

buzzer = Pin(15, Pin.OUT, value=0)
button = Pin(16, Pin.IN)

while True:
    held = button.value() == 0
    if held != buzzer.value():
        buzzer.value(held)
        print("ding" if held else "quiet")
    time.sleep_ms(10)

Troubleshooting

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

  • The buzzer stays silent This project
    Check the transistor: GP15 through the 1k resistor to the base, the buzzer to the collector, and the emitter to ground.
  • The buzzer sounds with no press This project
    Check the 10k resistor from the button line to 3.3 V, and the button's other side to ground.
  • 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.
  • The buzzer makes a constant tone, or you cannot change its pitch. Buzzer
    An active buzzer has its own oscillator, so it only beeps on and off. A passive buzzer needs tone() to set pitch. Check which type you have.
  • The buzzer is silent or very quiet. Buzzer
    Check polarity: the plus mark goes to the pin or supply. For loud buzzers, drive them through a transistor instead of straight from a pin.

Change it and keep it

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