Skip to content

Project 3.6.1: Public Bin Traffic Counter

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


Overview

The Pico counts visits near a public waste bin so students can estimate usage patterns and servicing demand.

Without usage data, bins may overflow simply because collection schedules do not match real traffic.

A Pico 2 W prototype with a PIR sensor and LED that counts motion-based bin visits.

Event counting, lockout timing, and interpretation of traffic counts as operational data.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
PIR
PIR Sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR VCC VBUS or module-rated supply Physical pin 40 if 5V is required Check your module label
PIR GND GND Physical pin 38 Common ground
PIR OUT GPIO 14 GPIO 14 / physical pin 19 Motion input
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Count flash LED
LED cathode GND Physical pin 18 or 38 Ground return

Wiring Diagram

Public Bin Traffic Counter wiring diagram

  PIR OUT -> GPIO 14
  GND -> PIR GND and LED cathode
  GPIO 15 -> 220 ohm resistor -> LED anode

Step-by-Step Assembly

  1. Connect the PIR sensor power, ground, and output to the Pico and GPIO 14.
  2. Connect the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
  3. Allow the PIR to warm up before you start validating counts.
  4. Aim the PIR toward the approach path to the bin rather than the whole room.

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 the raw PIR state and walk toward the bin area to confirm motion detection coverage.
  3. Test the output device: Blink the LED from a short script so the count feedback path works before the PIR logic is added.
  4. Test the decision logic: Confirm that one approach produces one count and that standing still does not keep increasing the total.
  5. Run the full system: Run a sequence of controlled approaches and compare the recorded count with your manual tally.
  6. Validate the prototype: Change the PIR angle and note how placement changes the traffic estimate.
  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 Pin
import time

PIR_PIN = 14
LED_PIN = 15
LOCKOUT_MS = 3000

pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
led = Pin(LED_PIN, Pin.OUT)
last_motion_ms = -LOCKOUT_MS
last_state = 0
count = 0

print("Public bin traffic counter ready")

while True:
    now = time.ticks_ms()
    state = pir.value()
    new_motion = last_state == 0 and state == 1

    if new_motion and time.ticks_diff(now, last_motion_ms) >= LOCKOUT_MS:
        count += 1
        last_motion_ms = now
        led.on()
        time.sleep_ms(200)
        led.off()
        print("Traffic count:", count)

    last_state = state
    time.sleep_ms(50)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
PIR setup Defines the motion input and LED output A narrow event definition is important for usable traffic data Confirm the PIR output polarity before changing the logic
Edge detection Counts only a new motion edge This stops one approach from becoming a stream of repeated counts Keep edge logic in place even if you later change the lockout duration
Lockout timing Applies a delay before another visit can be counted A lockout makes the counter behave more like a visit counter than a motion chatter counter Tune LOCKOUT_MS after observing real bin use
Feedback Flashes the LED and prints the updated total Immediate output helps validate whether the count matches the real motion event Keep the flash short so it does not slow the main loop too much

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: move the input close to the chosen threshold and confirm the transition is sensible
  • Critical condition test: trigger the strongest alert or control state and confirm the output response is correct
  • Calibration test: compare the sensor or timing against a known reference or repeated trial
  • Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves

Deployment and Limitations

  • This prototype is useful for classroom analytics and local operational studies
  • Before deployment, the sensor location and counting logic need validation against real traffic patterns
  • Counts are only useful when users know the conditions that produce under-counting or over-counting

Troubleshooting

Problem Possible Cause Solution
Counts increase when nobody uses the bin PIR sees motion from the wider room Aim the sensor only at the bin approach path and reduce its field of view
Counts are too low The lockout is too long or the PIR misses fast approaches Shorten LOCKOUT_MS carefully or improve the sensor angle
LED never flashes LED wiring is wrong Test the LED separately before blaming the PIR logic
PIR seems dead The module has not warmed up or power is incorrect Wait for PIR warm-up and confirm the power supply matches the module requirements