Project 2.1.13: Livestock Area Motion Monitor
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project monitors movement in a livestock area and summarizes activity over a timed observation window.
Students will build a PIR-based event logger that counts motion and classifies the space as QUIET, LOW ACTIVITY, MODERATE ACTIVITY, or HIGH ACTIVITY.
The final system should flash an LED when movement is logged, print a report at the end of each window, and prevent one continuous motion event from being counted too many times.
Required Components
![]() PIR motion sensor |
![]() Raspberry Pi Pico 2 W |
![]() LED and 220 Ω resistor |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| PIR OUT | GPIO 4 | GPIO 4 / physical pin 6 | Digital motion input |
| Activity LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Briefly lights when motion is logged |
| Activity LED cathode | GND | Any GND pin | LED return |
Wiring 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 can face the livestock test area without moving. 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: Place and Connect the Activity LED
Place the activity LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the activity LED long leg through a 220Ω resistor to GPIO 16. Connect the activity 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] Activity LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Activity LED short leg connects to GND
- [x] No loose jumper wires
Intermediate Note
Allow the PIR sensor to warm up before assuming the readings are wrong. Keep the sensor fixed so motion logs are caused by movement, not by the sensor shifting.
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
- Mount the PIR sensor so it watches the chosen movement path instead of the whole room
- Allow the PIR module to warm up before trying to classify movement patterns
Test the input sensor
- Walk slowly and then quickly through the detection area to verify that motion is seen consistently
- Check that the event counter does not rise rapidly from one continuous movement
Test the output device
- Confirm that the LED flashes only when a motion event is logged
- Watch the Shell for the activity summary at the end of each observation window
Run the full system
- Let the monitor run through several windows and compare the reported activity level with the actual movements in the area
- Change the sensor angle if you want to compare narrow and wide monitoring coverage
Save the project
- Save the final code and note the window length used for your activity summaries
- Record whether the area usually looked quiet, moderate, or highly active during your test
Full Project Code
from machine import Pin
import time
pir = Pin(4, Pin.IN)
activity_led = Pin(16, Pin.OUT)
motion_count = 0
window_s = 30
window_start = time.time()
last_motion = 0
def activity_state(count):
if count >= 6:
return "HIGH ACTIVITY"
if count >= 3:
return "MODERATE ACTIVITY"
if count >= 1:
return "LOW ACTIVITY"
return "QUIET"
print("Livestock area motion monitor ready")
while True:
now = time.time()
motion = pir.value() == 1
if motion and (now - last_motion) > 1:
motion_count += 1
last_motion = now
activity_led.on()
print("Motion event logged")
else:
activity_led.off()
if (now - window_start) >= window_s:
state = activity_state(motion_count)
print("Window summary: {} events -> {}".format(motion_count, state))
motion_count = 0
window_start = now
time.sleep(0.2)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
motion_count |
Stores how many events happened in the current window | A single summary is more useful than isolated motion blips |
last_motion timing |
Stops continuous movement from being counted too many times | Simple debounce improves activity data quality |
activity_state() |
Converts the count into a behavior label | Labels are easier to interpret than raw numbers alone |
window_start and window_s |
Define the reporting interval | Students can compare short and long observation windows |
Expected Result
The LED should flash when motion is logged, and the Shell should print a summary after each window showing how many motion events occurred and whether the area was QUIET, LOW ACTIVITY, MODERATE ACTIVITY, or HIGH ACTIVITY.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The count is always zero | The PIR is not warmed up or is aimed badly | Wait for warm-up and aim the sensor at the movement path. |
| The count rises too quickly | The PIR stays high for long motion events | Increase the event gap or move the sensor farther from the movement path. |
| The LED never turns on | The LED wiring is wrong or no motion is logged | Test the LED separately and confirm the PIR output changes. |
| The window summary seems meaningless | The observation window is too short or too long | Adjust window_s to match the behavior you want to study. |



