Pi melody doorbell button
A Raspberry Pi 4 plays a short tune on a passive buzzer when a button is pressed, and a different tune on a long press.
Build it step by step
0 of 7 doneShut the Pi down
Then unplug power.
Wire the buzzer
Pin 12 through 330 ohms, minus to pin 14.
Wire the button
Pin 11 to pin 9.
Boot and run the program
Nothing sounds yet.
Press the button briefly
Tune one plays.
Hold it for a second
Tune two plays.
Add your own tune
Write a new list and call play with it.
How do you build a pi melody doorbell button?
A Raspberry Pi 4 plays a short tune on a passive buzzer when a button is pressed, and a different tune on a long press. You need Raspberry Pi 4 Model B (4 GB), Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 110 minutes.
- Level
- Beginner
- Time
- About 110 min
- Parts
- 5
Parts list
- Single Board ComputersBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- SwitchesBuy
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Passive buzzer plus | 330 ohm resistor to Pi pin 12 (GPIO18) | Tone output |
| Passive buzzer minus | Pi pin 14 (GND) | Return |
| Button | Pi pin 11 (GPIO17) and pin 9 (GND) | Short press for tune one, long press for tune two |
Code
A tune is a list of pitches and lengths. Each note here is a frequency in hertz and a time in seconds, stored in a list, and the player sounds each one for most of its time with a short silent gap so repeated notes can be heard apart. A passive buzzer has no tone circuit of its own, so the Pi must supply the tone, and the gpiozero library does this with a PWM signal on the pin set to the note frequency. The PWM is made in software, so the pitch may wobble a little when the Pi is busy, which is fine for a doorbell. The button uses the Pi internal pull-up resistor, so one side goes to the pin and the other to ground. 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.
from gpiozero import Button, TonalBuzzer
from gpiozero.tones import Tone
from time import sleep, time
buzzer = TonalBuzzer(18)
button = Button(17) # internal pull-up, pressed connects to ground
C4, D4, E4, F4, G4, A4 = 262, 294, 330, 349, 392, 440
# Each note is (frequency in hertz, length in seconds)
ODE = [(E4,.4),(E4,.4),(F4,.4),(G4,.4),(G4,.4),(F4,.4),(E4,.4),(D4,.4),
(C4,.4),(C4,.4),(D4,.4),(E4,.4),(E4,.6),(D4,.2),(D4,.8)]
TWINKLE = [(C4,.4),(C4,.4),(G4,.4),(G4,.4),(A4,.4),(A4,.4),(G4,.8),
(F4,.4),(F4,.4),(E4,.4),(E4,.4),(D4,.4),(D4,.4),(C4,.8)]
def play(tune):
for freq, length in tune:
buzzer.play(Tone(frequency=freq))
sleep(length * 0.9)
buzzer.stop()
sleep(length * 0.1) # short gap so repeated notes are heard
while True:
button.wait_for_press()
start = time()
button.wait_for_release()
held = time() - start
play(TWINKLE if held >= 0.8 else ODE)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Short press | Tune one plays in full |
| Long press | Tune two plays |
| Repeated notes | Heard as separate notes |
| Pitch | Rises and falls as in the tune |
| No press | Silence |
If it does not work
No sound
Check the buzzer is passive and wired to pin 12.
Out of range error
TonalBuzzer limits notes to about C3 to B6, adjust the numbers.
Notes wobble
Close other programs, software PWM depends on the load.
Long press not recognised
Hold for a full second.
Button does nothing
Check pin 11 and ground.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pi melody doorbell button?
Raspberry Pi 4 Model B (4 GB), Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the pi melody doorbell button take to build?
About 110 minutes for a beginner maker, in 7 steps.
How do I know the pi melody doorbell button 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 pin 12.
Out of range error: what should I check?
TonalBuzzer limits notes to about C3 to B6, adjust the numbers.
Notes wobble: what should I check?
Close other programs, software PWM depends on the load.
Long press not recognised: what should I check?
Hold for a full second.
