Skip to content

Project 3.1.5: Door Open Alert

Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

The Pico uses a push button to simulate whether a door is open and raises an alert only if the button stays pressed for too long.

Doors that remain open can create security, privacy, or sanitation problems, so the alert should depend on duration rather than a brief opening.

This Pico 2 W prototype uses a push button, LED, and buzzer for timed door-open alerts.

Button-based door-state simulation, timing-based alerts, and simple occupancy-security logic.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Push button
Push button
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Buzzer
Buzzer

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Push-button leg 1 GPIO 14 GPIO 14 / physical pin 19 Input reads high when pressed
Push-button leg 2 3.3V Physical pin 36 Door-open simulation input
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Warning LED
LED cathode GND Physical pin 23 or 38 Ground return
Buzzer positive GPIO 16 GPIO 16 / physical pin 21 Audible alert
Buzzer negative GND Physical pin 23 or 38 Ground return

Wiring Diagram

contaminated water alert

Push button      -> GPIO 14 and 3.3V
GPIO 15          -> 220 ohm resistor -> LED anode
GPIO 16          -> Buzzer positive
Common GND       -> LED cathode and buzzer negative

Step-by-Step Assembly

  1. Wire the push button between GPIO 14 and 3.3V so the pin reads high when the button is pressed. Holding the button simulates an open door.
  2. Connect the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
  3. Connect the buzzer positive pin to GPIO 16 and the negative pin to GND.
  4. Hold and release the button while watching the raw button state before enabling the timed alert.

Testing Individual Components

Before running the full project, test each subsystem separately. This makes it easier to find wiring or logic problems before full integration.

  1. Hardware setup: Assemble the Pico, push button, LED, and buzzer exactly as shown in the connection table before applying power.
  2. Test the input button: Print the button state while pressing and releasing it to confirm that GPIO 14 reads high when pressed.
  3. Test the output devices: Test the LED and buzzer outputs separately before linking them to the door-open timer.
  4. Test the decision logic: Hold the button for less than and longer than the allowed duration to confirm that only a prolonged open state raises the alert.
  5. Run the full system: Run repeated short and long button-hold tests and compare the timing with the printed states.
  6. Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.

Full Project Code

After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

from machine import Pin
import time

BUTTON_PIN = 14
LED_PIN = 15
BUZZER_PIN = 16
OPEN_ALERT_MS = 10000

button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
open_since_ms = None

print('Door open alert ready')
print('Hold the button to simulate an open door')

while True:
    door_open = button.value() == 1
    now = time.ticks_ms()

    if door_open:
        if open_since_ms is None:
            open_since_ms = now
        open_duration = time.ticks_diff(now, open_since_ms)

        if open_duration >= OPEN_ALERT_MS:
            status = 'ALERT'
            led.on()
            buzzer.on()
        else:
            status = 'OPEN TIMING'
            led.off()
            buzzer.off()
    else:
        open_since_ms = None
        status = 'CLOSED'
        led.off()
        buzzer.off()

    print('Status:', status)
    time.sleep_ms(500)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Button setup Defines the push-button input with an internal pull-down Holding the button provides a clear door-open simulation Verify that GPIO 14 reads high when pressed
Open timer Starts timing when the button is held and resets when released A timing threshold avoids nuisance alarms for brief openings Adjust OPEN_ALERT_MS to match the test scenario
Alert threshold logic Separates OPEN TIMING from full ALERT mode Students can validate the difference between brief and prolonged-open conditions Change the threshold only after testing button-hold behavior
Outputs Drives the LED and buzzer only during the prolonged-open alert This keeps the system quiet during normal operation but visible during faults Add an LED-only warning stage if desired

Expected Result

The serial monitor reports the current reading or state clearly.

The hardware output responds when the decision logic changes state.

Subsystem behavior matches the thresholds, timing, or rules described in the document.

Validation Checks

  • Normal condition test: confirm the system stays in its safe or idle state under baseline conditions
  • Warning condition test: hold the button for just under the chosen threshold and confirm no alert occurs
  • Critical condition test: hold the button longer than the threshold and confirm the LED and buzzer activate
  • Timing test: compare the alert delay with a stopwatch or repeated trial
  • Limitation test: note that the push button simulates a door state; it does not detect a physical door

Deployment and Limitations

  • This prototype can be used for local warning demonstrations and early field trials
  • Before deployment, it needs enclosure protection, calibration records, and a clear response procedure
  • Alert systems should always be treated as decision-support tools unless they are certified for the application

Troubleshooting

Problem Possible Cause Solution
Button press is not detected Button wiring is incorrect Check that the button drives GPIO 14 high when pressed
Alert never starts The threshold is too long or the button is not held long enough Lower OPEN_ALERT_MS for a quick validation test
Alert starts immediately GPIO 14 is held high by incorrect wiring Check that the button connects only between GPIO 14 and 3.3V
Buzzer never sounds Buzzer wiring is wrong or the alert duration is never reached Test the buzzer directly and then hold the button beyond the threshold