Skip to content

Project 3.1.8: Handwash Counter

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


Overview

The Pico counts each new hand-detection event at a wash station and reports the running total.

Usage data helps teams estimate demand, refill schedules, and whether hygiene stations are actually being used.

A Pico 2 W prototype with an IR sensor and LED that counts handwash visits with lockout protection.

Event detection, false-count reduction, and basic operational data interpretation.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
IR sensor
IR sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
IR sensor VCC 3.3V output Physical pin 36 Check that the module supports 3.3V
IR sensor GND GND Physical pin 38 Common ground
IR sensor OUT GPIO 14 GPIO 14 / physical pin 19 Active-low event 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

handwash counter

  3.3V                 -> IR sensor VCC
  GND                  -> IR sensor GND and LED cathode
  IR sensor OUT        -> GPIO 14
  GPIO 15              -> 220 ohm resistor -> LED anode

Step-by-Step Assembly

  1. Connect the IR sensor to Pico 3.3V, GND, and GPIO 14.
  2. Connect the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
  3. Aim the sensor so a hand passes through the beam once during a normal wash action.

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 sensor state and move a hand through the target area to confirm the active-low event pattern.
  3. Test the output device: Blink the LED from a short script so the output path is verified before the counting logic runs.
  4. Test the decision logic: Confirm that one handwash event produces one count even if the hand stays in place briefly.
  5. Run the full system: Run a series of repeated hand entries and confirm the total count matches your manual tally.
  6. Validate the prototype: Change sensor angle or distance and observe how the count accuracy 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 Pin
import time

IR_PIN = 14
LED_PIN = 15
LOCKOUT_MS = 3000

ir = Pin(IR_PIN, Pin.IN, Pin.PULL_UP)
led = Pin(LED_PIN, Pin.OUT)
last_state = 1
last_count_ms = -LOCKOUT_MS
count = 0

print("Handwash counter ready")

while True:
    now = time.ticks_ms()
    current_state = ir.value()
    new_detection = last_state == 1 and current_state == 0

    if new_detection and time.ticks_diff(now, last_count_ms) >= LOCKOUT_MS:
        count += 1
        last_count_ms = now
        led.on()
        time.sleep_ms(200)
        led.off()
        print("Handwash count:", count)

    last_state = current_state
    time.sleep_ms(50)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Input and output setup Defines the IR sensor input and the LED output This connects one event source to one clear visible response Confirm whether your sensor idles high or low before changing the logic
Edge detection Counts only a new active-low detection event Edge detection prevents one long detection from becoming many counts Do not replace edge logic with a simple level check unless you want repeated counts
Lockout timing Ignores new counts during a short cooldown period A lockout helps reduce over-counting when users linger in front of the sensor Tune LOCKOUT_MS after watching real user behavior
Count feedback Flashes the LED and prints the new total Immediate feedback makes validation easier during live testing Change the LED flash duration only after the logic is stable

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
Count increases too fast Lockout is too short or the sensor sees repeated motion Increase LOCKOUT_MS and adjust the sensor angle
No counts appear Sensor output is not reaching GPIO 14 Print the raw sensor state and recheck the signal wire
LED never flashes LED wiring or resistor path is wrong Test the LED separately from the counter logic
Counts do not match real visits Sensor placement does not match actual hand movement Reposition the sensor so one handwash motion crosses the detection area clearly