Door alarm
A door alarm turns one input and one output into something useful: a push button plays the door contact, and an active buzzer sounds while it is pressed. The twist is the output side. The active buzzer is the one shop part that cannot hang straight off a GPIO — at 3.3 V it draws about 19 mA, over the model's 16 mA safe pin current, so the sandbox raises an overcurrent warning. This build drives it the way real hardware would: a relay switches the buzzer's 5 V supply, and the GPIO only drives the relay's 2.5 mA input.
Cheap version — wire it by hand:
- Push button — $0.50
- 10 kΩ resistor for the pull-down — $0.25
- Active buzzer — $1.50
- Relay module — $4
Fast version — build it from kits:
- Button module — $1.50: the button with its pull-down fitted
- Active buzzer — $1.50 and relay module — $4, still hand-wired: the shop has no buzzer kit, and the relay is the point.
Both versions run the same program on GPIO27 (door) and GPIO17 (relay signal).
The cheap version
Pressed means the door is open: the button ties GPIO27 to 3.3 V, and the 10 kΩ resistor holds it at 0 V while the door is shut. The relay is wired high-trigger, so GPIO17 high closes its contacts and puts the full 5 V across the buzzer.
from wiring import *
pi = Pi()
door = Button()
buzzer = Buzzer()
relay = Relay("high") # IN high = contacts closed
pi.GPIO27 >> door >> pi.V3_3 # pressed: GPIO27 sees 3.3 V
pi.GPIO27 >> Resistor(10000) >> pi.GND
pi.V5 >> relay.dc_plus # coil and switched supply: both 5 V
pi.V5 >> relay.com
relay.no >> buzzer.plus # contacts close = buzzer across the 5 V rail
buzzer.minus >> relay.dc_minus
relay.dc_minus >> pi.GND
pi.GPIO17 >> relay.signalfrom gpiozero import Button, OutputDevice
from time import sleep
door = Button(27, pull_up=False, bounce_time=0.02) # 1 while the door is open
alarm = OutputDevice(17, active_high=True, initial_value=False)
while True:
if door.is_pressed:
alarm.on()
else:
alarm.off()
print(f"door {'open' if door.is_pressed else 'shut'} buzzer {'sounding' if alarm.value else 'quiet'}")
sleep(0.1)The fast version
The button module replaces the button and its pull-down — OUT reads 1 while pressed, so the wiring is three connections. The relay and buzzer stay as they are, because no kit covers them; the program does not change.
from wiring import *
pi = Pi()
door = kits.ButtonModule()
buzzer = Buzzer()
relay = Relay("high")
pi.V3_3 >> door.vcc
pi.GPIO27 >> door.out
door.gnd >> pi.GND
pi.V5 >> relay.dc_plus
pi.V5 >> relay.com
relay.no >> buzzer.plus
buzzer.minus >> relay.dc_minus
relay.dc_minus >> pi.GND
pi.GPIO17 >> relay.signalfrom gpiozero import Button, OutputDevice
from time import sleep
door = Button(27, pull_up=False, bounce_time=0.02) # 1 while the door is open
alarm = OutputDevice(17, active_high=True, initial_value=False)
while True:
if door.is_pressed:
alarm.on()
else:
alarm.off()
print(f"door {'open' if door.is_pressed else 'shut'} buzzer {'sounding' if alarm.value else 'quiet'}")
sleep(0.1)How it works
- The button is a bare contact, so it needs a defined level. With the pull-down fitted, GPIO27 reads 0 V while the door is shut and 3.3 V while it is open;
Button(27, pull_up=False)tells gpiozero that is the sense it should expect.bounce_time=0.02discards the few milliseconds of contact chatter on each change. - Polling at 0.1 s is plenty for a door: a human cannot open and close it between two looks at that rate.
door.when_pressedandwhen_releasedcallbacks withpause()are the event-driven alternative. - Why the relay: the buzzer models a 132 Ω load, so a direct 3.3 V GPIO drive puts about 19 mA through the pin — over the 16 mA safe limit, which the simulator flags as an overcurrent warning. Through the relay's contacts the buzzer runs from 5 V (its rated maximum) at full volume, and the GPIO only supplies the relay input stage's 2.5 mA. The contacts close about 10 ms after GPIO17 rises.
- The active buzzer makes its own tone — about 2.3 kHz, fixed — so the program only ever turns it on and off. A passive buzzer would instead need PWM for the note.
Try it
Build the board and press Run. While the door is shut the console shows door shut buzzer quiet; click and hold the button on the canvas and within a poll or two it prints door open buzzer sounding. Unmute the toolbar's audio to hear the 2.3 kHz tone (the sound layer appears as the volume rises); release the button and it stops.
If you are curious about the warning the safe wiring avoids, temporarily wire Buzzer() straight to GPIO17 and hold the button — the sandbox shows the overcurrent warning on the pin. Put the relay back afterwards; the active buzzer page explains the electrical why in full.
As a contest submission, an objective would simulate the press rather than expect a finger: a stimulus holds the button from 1 s to 2 s, and checks that GPIO17 — or the buzzer's toneHz state — is high only during that window, plus the damage check. The built-in Tilt alarm and Parking sensor objectives are that same shape. Note the judge's default damage check does not tolerate the 19 mA direct drive; this wiring keeps it strict.
Variations
- Motion instead of contact: a PIR sensor on its OUT pin goes high when someone moves, which is a different kind of door alarm — no button to hold, and it can see an intruder who never touches the door. The built-in Motion light objective shows the read pattern.
- A latching contact: a slide switch stays where it is put, so it models a window or door left ajar rather than a finger on a button.
- A softer siren: swap the active buzzer for a passive one and drive it with
TonalBuzzerfor a two-tone wail, or alternate the buzzer with a relay + lamp flasher.
Going deeper
- Primers: pull-ups, debouncing and dividers for the input side, relays and switching loads for the output side.
- Parts: push button, slide switch, PIR, active buzzer, relay.
- Kit: button module.
- Libraries: gpiozero —
Button,OutputDeviceandBuzzer. - The safe-drive discussion: active buzzer and relay module.