Skip to content

Project 2.1.9: Farm Night Security Motion Alert

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


Overview

This project builds a farm security motion alert that activates only in low light.

Students will combine a PIR motion sensor, an LDR ambient-light sensor, a buzzer, and a visual alert LED to create a night-only intrusion warning.

The system ignores normal daytime motion, arms itself when it gets dark, and triggers both sound and light when motion is detected at night.

Required Components

PIR motion sensor
PIR motion sensor
Breadboard and jumper wires
Breadboard and jumper wires
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LDR module with analog output
LDR module with analog output
Buzzer
Buzzer
LED and 220 Ω resistor
LED and 220 Ω resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR OUT GPIO 4 GPIO 4 / physical pin 6 Digital motion input
LDR module AOUT GPIO 26 GPIO 26 / physical pin 31 ADC light input from module
Buzzer positive GPIO 15 GPIO 15 / physical pin 20 Audible alert
Alert LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Visual alert
Alert LED cathode and buzzer negative GND Any GND pin Common return

Wiring Diagram

Circuit Diagram


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: Position the PIR Motion Sensor

Place the PIR motion sensor so it faces the area you want to monitor at night. Check the printed pin labels and identify VCC, GND, and OUT before wiring.

Step 3: Connect the PIR Sensor

Connect PIR VCC to the supply voltage required by the module label. Connect PIR GND to GND. Connect PIR OUT to GPIO 4. If the PIR is powered from 5V, confirm that OUT is still safe for 3.3V Pico GPIO.

Step 4: Connect the LDR Module

Place the LDR module where it can sense the light level. Connect LDR module VCC to 3.3V. Connect LDR module AOUT to GPIO 26. Connect LDR module GND to GND.

Step 5: Place and Connect the Buzzer

Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 15. Connect the buzzer negative pin to GND.

Step 6: Place and Connect the Alert LED

Place the alert LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the alert LED long leg through a 220Ω resistor to GPIO 16. Connect the alert LED short leg to GND.


Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] PIR OUT connects to GPIO 4
  • [x] PIR GND connects to GND
  • [x] PIR OUT is confirmed 3.3V-safe before connecting to the Pico
  • [x] LDR divider node connects to GPIO 26
  • [x] Buzzer positive pin connects to GPIO 15
  • [x] Alert LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Alert LED short leg connects to GND
  • [x] No loose jumper wires

Intermediate Note

Allow the PIR sensor to warm up before testing motion. Test the LDR in bright light and shade so the night-only threshold makes sense.

Safety Note

Raspberry Pi Pico GPIO pins are 3.3V only. If the PIR module is powered from 5V, verify that its OUT pin does not send more than 3.3V into GPIO 4.


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 LDR and PIR input stage first so you can test the arming conditions before adding the buzzer.
  • Allow the PIR sensor to warm up for its normal stabilisation period before judging the first readings.

Test the input sensors

  • Cover and uncover the LDR to confirm the light percentage changes enough to separate day and night conditions.
  • Test the PIR sensor after warm-up and verify that motion is detected only when someone moves in front of it.

Test the output devices

  • Test the LED and buzzer separately before trusting the full alert logic.
  • Confirm that the buzzer stays silent during daytime tests.

Run the full system

  • Dim the LDR or cover it to force night mode, then trigger the PIR with motion.
  • Confirm that the alert triggers only when both the low-light and motion conditions are true.

Save the project

  • Save the final code and note the light threshold that worked for your room or test area.
  • Record whether the cooldown prevented repeated alarms effectively.

Full Project Code

from machine import ADC, Pin
import time

pir = Pin(4, Pin.IN)
ldr = ADC(26)
buzzer = Pin(15, Pin.OUT)
alert_led = Pin(16, Pin.OUT)

night_threshold = 30
alert_hold_s = 8
cooldown_s = 15
last_alert = time.time() - cooldown_s

buzzer.off()
alert_led.off()


def light_percent():
    raw = ldr.read_u16()
    return int((raw / 65535) * 100)


def alarm_burst():
    for _ in range(3):
        buzzer.on()
        alert_led.on()
        time.sleep(0.2)
        buzzer.off()
        alert_led.off()
        time.sleep(0.2)


print("Farm night security motion alert ready")
while True:
    ambient = light_percent()
    night_mode = ambient < night_threshold
    motion = pir.value() == 1
    now = time.time()

    if night_mode and motion and (now - last_alert) >= cooldown_s:
        print("Night alert: motion detected in low light")
        alarm_burst()
        alert_led.on()
        time.sleep(alert_hold_s)
        alert_led.off()
        last_alert = time.time()
    else:
        print("Light:{}% Night:{} Motion:{}".format(ambient, night_mode, motion))
    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters
light_percent() Converts the LDR reading into a rough brightness percentage The security system needs a night/day condition, not just raw ADC values
night_mode Arms the alert only when ambient light is low enough Motion alone is not a good night-security rule
cooldown_s Adds a pause between alarm cycles Cooldown prevents a burst of repeated alarms from one movement event
alarm_burst() Creates the combined audio and visual alert The outputs should be obvious when the system detects night motion

Expected Result

When the LDR indicates bright conditions, the system should ignore motion. When the area becomes dark enough and motion is detected by the PIR sensor, the buzzer should sound, the LED should flash, and the Shell should report a night motion alert.


Troubleshooting

Problem Possible cause Solution
The alarm triggers in daylight The night threshold is too high or the LDR module is wired incorrectly Recheck the LDR module and lower the night threshold after testing.
The PIR never triggers The PIR is still warming up or the output pin is not connected correctly Wait for warm-up and recheck the PIR OUT connection to GPIO 4.
The buzzer sounds repeatedly The cooldown is too short or the PIR sees repeated motion Increase cooldown_s and reduce movement in the detection area during testing.
The LDR value never changes The module analog output is not built correctly Confirm one LDR leg goes to 3.3V and the resistor goes from the ADC node to GND.