Skip to content

Project 2.1.6: Emergency Notifier

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


Overview

This project builds a BLE emergency notifier that sends a clear alert message when a button is held and flashes an LED as local feedback.

The long-press requirement reduces accidental triggers and the cooldown prevents alert spam.

A similar design could be the early prototype for a panic button, help desk notifier, or emergency call station interface.

Required Components

Push button
Push button
Phone with BLE app
Phone with BLE app
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LED
LED
220Ω resistor
220Ω resistor
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Button leg 1 GPIO 4 GPIO 4 / physical pin 6 Use internal pull-up in code
Button opposite leg GND Physical pin 38 Active-low input
LED anode (+) 220Ω resistor then GPIO 16 GPIO 16 / physical pin 21 Local visual alert output
LED cathode (-) GND Physical pin 38 Common ground

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 4  ───────────┤──── Button (internal pull-up)
│  GND     ───────────┤──── Button opposite leg
│  GPIO 16 ───────────┤──── 220Ω resistor → LED anode
│  GND     ───────────┤──── LED cathode
│                     │
└─────────────────────┘

Button (across breadboard center gap)
LED with resistor (visible location)

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Step 2: Place the Push Button

Place the push button across the breadboard center gap.

This keeps each side of the button on separate breadboard rows.

Step 3: Connect One Button Leg

Connect one button leg to GPIO 4.

Step 4: Connect the Opposite Button Leg

Connect the opposite button leg to GND.

The code uses the Pico internal pull-up, so the button reads LOW when pressed.

Step 5: Place the LED

Place the LED on the breadboard.

Identify the long leg as the anode (+) and the short leg as the cathode (-).

Step 6: Connect the LED Long Leg

Connect the LED long leg to one end of a 220Ω resistor.

Connect the other end of the resistor to GPIO 16.

Step 7: Connect the LED Short Leg

Connect the LED short leg to GND.

Step 8: Check the Button Position

Mount the button where it can be pressed deliberately during the test.


Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Push button sits across the breadboard center gap
  • [x] One button leg connects to GPIO 4
  • [x] Opposite button leg connects to GND
  • [x] LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] LED short leg connects to GND
  • [x] No loose jumper wires

Intermediate Note

This project uses BLE on the Raspberry Pi Pico 2W. Use a BLE app such as nRF Connect, LightBlue, or a BLE UART terminal to connect to AIDER_Notifier and send commands. The alert triggers only after the button is held long enough, and the cooldown prevents repeated alerts from one press.


Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.

Hardware setup

  • Build the button and LED circuit
  • Keep the LED visible so the flash pattern can be observed from a distance

Test the input sensor

  • Run a short button test script and confirm the active-low input works
  • Decide how long the long press should feel for a deliberate emergency trigger

Test the output device

  • Run a short LED blink test
  • Check the resistor path and LED polarity before testing the full notifier logic

Test communication

  • Connect to AIDER_Notifier from the BLE app
  • Send status and test to confirm the command interface works

Run the full system

  • Hold the button for the required time
  • Confirm the LED flashes and the BLE app receives the emergency message
  • Try to trigger it again immediately and confirm the cooldown blocks the repeat alert

Save the project

  • Save the working code
  • Compare this notifier with Project 116 and decide which alert style is better for the intended user

Full Project Code

from machine import Pin
import bluetooth
import time
from ble_uart import BLEUART

DEVICE_NAME = "AIDER_Notifier"
HOLD_MS = 1000
COOLDOWN_MS = 5000

button = Pin(4, Pin.IN, Pin.PULL_UP)
led = Pin(16, Pin.OUT)

ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name=DEVICE_NAME)

press_start_ms = None
waiting_for_release = False
last_alert_ms = time.ticks_add(time.ticks_ms(), -COOLDOWN_MS)
command_queue = []


def send_line(message):
    print(message)
    uart.write((message + "\n").encode())


def on_rx(data):
    command = data.decode("utf-8").strip().lower()
    if command:
        command_queue.append(command)


def flash_led(count):
    for _ in range(count):
        led.on()
        time.sleep_ms(150)
        led.off()
        time.sleep_ms(150)


def remaining_cooldown(now_ms):
    return max(0, time.ticks_diff(time.ticks_add(last_alert_ms, COOLDOWN_MS), now_ms) // 1000)


uart.on_rx(on_rx)
led.off()
send_line("Bluetooth emergency notifier ready")

while True:
    now = time.ticks_ms()

    if button.value() == 0:
        if press_start_ms is None:
            press_start_ms = now
        elif (
            not waiting_for_release
            and time.ticks_diff(now, press_start_ms) >= HOLD_MS
            and time.ticks_diff(now, last_alert_ms) >= COOLDOWN_MS
        ):
            flash_led(4)
            send_line("EMERGENCY ALERT TRIGGERED")
            last_alert_ms = now
            waiting_for_release = True
    else:
        press_start_ms = None
        waiting_for_release = False

    if command_queue:
        command = command_queue.pop(0)
        if command == "status":
            send_line("Ready:{} Cooldown:{}s".format(remaining_cooldown(now) == 0, remaining_cooldown(now)))
        elif command == "test":
            flash_led(2)
            send_line("Notifier LED test complete")
        elif command == "help":
            send_line("Commands: status, test, help")
        else:
            send_line("Unknown command. Send help.")

    time.sleep_ms(80)

How the Code Works

Code Section What It Does Why It Matters
Long-press detection Confirms the button was held intentionally Reduces false alarms from accidental touches
LED flash pattern Provides visible local feedback during an alert Shows that the notifier has triggered even before checking the phone
Cooldown timer Prevents immediate retriggering Keeps the notifier from flooding the phone with messages
BLE status command Reports whether the system is ready or still cooling down Helps students test the notifier state without triggering an alert

Expected Result

A deliberate long press causes the LED to flash and sends an emergency message over BLE. The system then enters a short cooldown period before another alert can be triggered.


Troubleshooting

Problem Possible cause Solution
LED does not flash The LED polarity or resistor path is wrong Recheck GPIO 16, the resistor, and the LED orientation
Alert triggers too easily The hold time is too short or the button is bouncing Increase HOLD_MS and recheck the button wiring
Status always shows cooldown The last alert time was triggered recently or the button remains held Release the button fully and wait for the cooldown to expire before testing again
Phone cannot find the BLE device The Pico is not advertising or the BLE helper files are missing Confirm ble_uart.py and ble_advertising.py are saved on the Pico, restart the board, and scan again from nRF Connect or LightBlue
Commands do not change the system state The phone is connected to the wrong service or the command text is different from the expected command Reconnect to the BLE UART service and send the exact command shown in the test section