Skip to content

Project 2.6.13: Smart Handwash Station Counter

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


Overview

This project builds a handwash station counter that separates short trigger events from valid wash sessions.

Students will use a digital presence or soil moisture sensor, measure session duration, count valid washes, and flag very short incomplete uses.

The final system should count valid wash sessions, record the average wash time, and sound a short warning when the detected use was too brief to count as a proper wash.

Required Components

Ultrasonic distance sensor
Ultrasonic distance sensor
Breadboard and jumper wires
Breadboard and jumper wires
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Push button
Push button
Status LED and 220Ω resistor
Status LED and 220Ω resistor
Active buzzer
Active buzzer

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Ultrasonic VCC 5V or module-rated VCC External/module supply Use a safe Echo level for the Pico
Ultrasonic GND GND Any GND pin Common ground
Ultrasonic TRIG GPIO 4 GPIO 4 / physical pin 6 Trigger output
Ultrasonic ECHO GPIO 5 GPIO 5 / physical pin 7 Echo input; level-shift if needed
Reset button one side GPIO 6 GPIO 6 / physical pin 9 Uses internal pull-up
Reset button other side GND Any GND pin Pressing pulls the input low
Status LED anode GPIO 16 through 220Ω resistor GPIO 16 / physical pin 21 Session indicator
Buzzer positive GPIO 18 GPIO 18 / physical pin 24 Short-use alert
LED cathode and buzzer negative GND Any GND pin Return path

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 4  ───────────┼──── Ultrasonic TRIG
│  GPIO 5  ───────────┼──── Ultrasonic ECHO (via 3.3V-safe interface)
│  GPIO 6  ───────────┼──── Reset button ──┐
│  GPIO 16 ───────────┼──── Resistor (220Ω) ── Status LED anode (+)
│  GPIO 18 ───────────┼──── Buzzer positive (+)
│  GND     ───────────┼──── Ultrasonic GND / Button / LED cathode / Buzzer negative (-)
│                     │
└─────────────────────┘

Ultrasonic Sensor
VCC  ─────────────── Safe module supply
GND  ─────────────── GND
TRIG ─────────────── GPIO 4
ECHO ─────────────── GPIO 5 (via voltage divider if 5V)

Reset Button
One side ─────────── GPIO 6
Other side ───────── GND

Status LED
Anode (+) ──[220Ω]── GPIO 16
Cathode (-) ───────── GND

Buzzer
Positive (+) ─────────── GPIO 18
Negative (-) ─────────── GND

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 Ultrasonic Distance Sensor

Mount the ultrasonic distance sensor where a real wash interaction is detected more reliably than simple pass-by movement. Identify VCC, GND, TRIG, and ECHO before wiring.

Step 3: Connect the Ultrasonic Distance Sensor

Connect sensor VCC to the safe low-voltage supply required by the module label. Connect sensor GND to GND. Connect sensor TRIG to GPIO 4. Connect sensor ECHO to GPIO 5 through a voltage divider if the ECHO signal is 5V.

Step 4: Place the Reset Button

Place the reset push button across the breadboard center gap. Connect one side of the reset button to GPIO 6. Connect the opposite side of the button to GND.

Step 5: Place and Connect the Status LED

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

Step 6: 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 18. Connect the buzzer negative pin to GND.


Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Ultrasonic TRIG connects to GPIO 4
  • [x] Ultrasonic ECHO connects to GPIO 5
  • [x] ECHO is voltage-divided or confirmed 3.3V-safe before connecting to the Pico
  • [x] Reset button connects between GPIO 6 and GND
  • [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Status LED short leg connects to GND
  • [x] Buzzer positive pin connects to GPIO 18
  • [x] Buzzer negative pin connects to GND
  • [x] No loose jumper wires

Intermediate Note

Test short and long wash sessions so the timing thresholds match the classroom demonstration. Adjust sensor placement if pass-by movement creates false sessions.

Safety Note

Keep the Pico, breadboard, USB cable, and jumper wires away from water splash. Mount only the sensor in the wet area if it is designed for that environment.


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 digital sensor first and test its trigger position before building the LED and buzzer outputs
  • Keep the Pico and breadboard protected from water splash

Test the input sensor

  • Trigger a short interaction and then a longer interaction to compare the measured durations
  • Press the reset button and confirm the counters clear correctly

Test the output device

  • Confirm the status LED stays on during an active session
  • Check that the buzzer only sounds when the session ends too quickly to count as valid

Test communication

  • Watch the Thonny Shell and confirm it prints session duration, valid wash count, short-use count, and average duration
  • Use the printed durations to choose a realistic minimum valid wash time

Run the full system

  • Perform several short and long interactions and confirm only the longer ones increase the valid wash count
  • Check that the average duration changes as more valid sessions are recorded

Save the project

  • Save the final code and record the minimum valid wash time used in your test
  • Write down whether the sensor position helped count real washes instead of pass-bys

Quick testing checklist

  • ☐ Ultrasonic sensor responds to presence correctly
  • ☐ Short sessions are not counted as valid washes
  • ☐ Long sessions increase the valid wash count
  • ☐ Status LED stays on during active sessions
  • ☐ Buzzer sounds only for short invalid sessions
  • ☐ Reset button clears all counters
  • ☐ Average duration updates after valid washes

Full Project Code

After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below 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, time_pulse_us
import time

trig = Pin(4, Pin.OUT)
echo = Pin(5, Pin.IN)
reset_button = Pin(6, Pin.IN, Pin.PULL_UP)
status_led = Pin(16, Pin.OUT)
buzzer = Pin(18, Pin.OUT)

PRESENCE_CM = 18
MIN_VALID_SECONDS = 3
DEBOUNCE_MS = 250

session_active = False
session_started = 0
valid_washes = 0
short_uses = 0
total_valid_time = 0
last_button_ms = 0

def distance_cm():
    trig.value(0)
    time.sleep_us(2)
    trig.value(1)
    time.sleep_us(10)
    trig.value(0)
    pulse = time_pulse_us(echo, 1, 30000)
    if pulse < 0:
        return 999
    return (pulse / 2) / 29.1

def hand_present():
    return distance_cm() <= PRESENCE_CM

def button_pressed():
    global last_button_ms
    now_ms = time.ticks_ms()
    if reset_button.value() == 0 and time.ticks_diff(now_ms, last_button_ms) > DEBOUNCE_MS:
        while reset_button.value() == 0:
            time.sleep(0.02)
        last_button_ms = now_ms
        return True
    return False

def average_duration():
    if valid_washes == 0:
        return 0
    return total_valid_time / valid_washes

def short_beep():
    buzzer.value(1)
    time.sleep(0.15)
    buzzer.value(0)

print('=== Smart Handwash Station Counter ===')

while True:
    now = time.time()
    active = hand_present()

    if button_pressed():
        session_active = False
        valid_washes = 0
        short_uses = 0
        total_valid_time = 0
        status_led.value(0)
        print('Observation counters reset.')

    if active and not session_active:
        session_active = True
        session_started = now
        status_led.value(1)
        print('Handwash session started.')

    if (not active) and session_active:
        session_active = False
        status_led.value(0)
        duration = now - session_started
        if duration >= MIN_VALID_SECONDS:
            valid_washes += 1
            total_valid_time += duration
            print('Valid wash recorded: {:.1f} second(s).'.format(duration))
        else:
            short_uses += 1
            short_beep()
            print('Short wash event ignored: {:.1f} second(s).'.format(duration))

    current_duration = (now - session_started) if session_active else 0
    print('Valid washes: {} | Short uses: {} | Current session: {:.1f}s | Average valid duration: {:.1f}s'.format(valid_washes, short_uses, current_duration, average_duration()))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters
Session start and end detection Recognises when a wash interaction begins and ends The project must measure duration, not only count triggers
Minimum valid time rule Counts only sessions long enough to represent a meaningful wash This makes the counter more useful than a simple motion counter
Average duration Tracks how long valid washes last on average Students can compare quality of use as well as count
Short-use buzzer Warns when a detected use was too short This turns the project into a behavioural feedback tool, not only a counter

Expected Result

The status LED should stay on during an active wash session. A session longer than the minimum valid time should increase the valid wash count. A shorter session should not increase the valid count and should trigger a short buzzer warning instead.


Troubleshooting

Problem Possible cause Solution
Every short trigger becomes a valid wash The minimum valid time is too low Increase MIN_VALID_SECONDS to better represent a real wash session.
The count never increases The sensor is not staying active long enough or the logic level is reversed Check the sensor position and confirm whether your module is active-low or active-high.
The buzzer sounds at the wrong time The session is ending too quickly because the sensor alignment is poor Reposition the sensor so real wash actions are detected continuously.
The reset button does nothing The button is miswired Check the GPIO 6 connection and confirm the input pulls low when pressed.