Skip to content

Project 3.1.19: Nighttime Alert

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


Overview

The Pico checks whether it is dark and whether motion is present, then raises an alert only when both conditions occur together.

Sanitation pathways or wash points may need extra warning only at night when movement happens in low visibility.

A Pico 2 W prototype with an LDR, PIR sensor, LED, and buzzer that uses combined logic instead of a single threshold.

A working AND-condition alert, LDR calibration, and validation of false-trigger behavior.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LDR
LDR
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Buzzer
Buzzer
PIR motion sensor
PIR motion sensor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
LDR divider midpoint GPIO 26 ADC0 GPIO 26 / physical pin 31 Analog light reading
LDR divider top 3.3V output Physical pin 36 LDR side of divider
10k resistor GND Physical pin 38 Forms the light-sensor divider
PIR VCC VBUS or module-rated supply Physical pin 40 if 5V is required Check your PIR module label
PIR GND GND Physical pin 38 Common ground
PIR OUT GPIO 14 GPIO 14 / physical pin 19 Digital motion input
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Alert LED
Buzzer positive GPIO 16 GPIO 16 / physical pin 21 Alert buzzer

Wiring Diagram

Circuit Diagram

  3.3V                 -> LDR -> divider midpoint -> GPIO 26 -> 10k resistor -> GND
  PIR OUT              -> GPIO 14
  GPIO 15              -> 220 ohm resistor -> LED anode
  GPIO 16              -> buzzer positive
  Common GND           -> PIR, LED, buzzer

Step-by-Step Assembly

  1. Build the LDR voltage divider and connect its midpoint to GPIO 26.
  2. Connect the PIR sensor power, ground, and output pin to the Pico and GPIO 14.
  3. Wire the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to ground.
  4. Connect the buzzer positive pin to GPIO 16 and its negative pin to ground.
  5. Allow the PIR sensor to settle for about thirty seconds after power-on before trusting its readings.

Testing Individual Components

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

  1. Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
  2. Test the input sensor: Print light values and motion values separately so you can find a realistic dark threshold for your room before combining the logic.
  3. Test the output device: Toggle the LED and buzzer with a quick test script to confirm both outputs work before joining them to the PIR and LDR inputs.
  4. Test the decision logic: Confirm that motion alone in bright light does not trigger the alert and that darkness alone without motion also stays clear.
  5. Run the full system: Run the full system with lights on and off and walk past the PIR to confirm the alert only occurs in dark conditions.
  6. Validate the prototype: Test from different directions and distances to observe PIR blind spots and LDR sensitivity changes.
  7. 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 ADC, Pin
import time

LDR_PIN = 26
PIR_PIN = 14
LED_PIN = 15
BUZZER_PIN = 16
DARK_THRESHOLD = 18000
SAMPLE_COUNT = 8

ldr = ADC(LDR_PIN)
pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)


def read_light():
    total = 0
    for _ in range(SAMPLE_COUNT):
        total += ldr.read_u16()
        time.sleep_ms(10)
    return total // SAMPLE_COUNT


print("Nighttime alert ready")

while True:
    light_level = read_light()
    motion = pir.value() == 1
    is_dark = light_level < DARK_THRESHOLD
    alert = is_dark and motion

    led.value(1 if alert else 0)
    buzzer.value(1 if alert else 0)

    print("Light: {} | Dark: {} | Motion: {} | Alert: {}".format(light_level, is_dark, motion, alert))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Sensor setup Defines the LDR analog input, PIR motion input, and alert outputs This project depends on two different sensor types working together Recheck power requirements if your PIR module needs 5V instead of 3.3V
read_light Averages several light samples to reduce noise Stable light values make threshold testing more trustworthy Adjust SAMPLE_COUNT if the reading still jumps too much
Dark and motion logic Turns the raw inputs into a combined alert condition The AND rule is the core engineering idea in this project Change DARK_THRESHOLD after measuring your room in bright and dark conditions
Output and reporting Updates the LED and buzzer and prints the full logic state Seeing each part of the rule helps students debug false triggers If the buzzer is too distracting, test with the LED first

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: keep the room bright and confirm that motion does not trigger the alert
  • Warning condition test: dim the room and verify that the code changes the dark flag before motion is added
  • Critical condition test: make the room dark, walk past the PIR, and confirm the LED and buzzer turn on together
  • Calibration test: record LDR readings in bright light, room light, and darkness before choosing DARK_THRESHOLD
  • False trigger test: point the PIR away from fans and windows and compare its behavior in different mounting positions

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
Alert triggers in daylight Dark threshold is too high Measure bright-room values and lower DARK_THRESHOLD until daylight is clearly above the threshold
Motion is never detected PIR has not warmed up or is wired incorrectly Wait for PIR warm-up and recheck the OUT pin connection to GPIO 14
Light readings do not change LDR divider is wired incorrectly Rebuild the LDR and resistor divider so the midpoint really feeds ADC0
Alert chatters on and off Sensor values are near the threshold Average more samples or calibrate the dark threshold farther away from room-light values