Skip to content

Project 3.2.1: Automated Urinal Flusher

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


Overview

The Pico detects occupancy with a PIR sensor and triggers one timed flush only after the user leaves the fixture area.

Timed flush control reduces water waste and creates more consistent sanitation behavior than manual flushing alone.

A Pico 2 W prototype with a PIR sensor, relay module, solenoid valve output, and LED activity indicator.

Occupancy timing, delayed actuation, cooldown protection, and safe relay control around water systems.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Relay module
Relay module
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Water pump
Water pump
PIR motionsensor
PIR motionsensor

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 Occupancy input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Flush control signal
Relay VCC External 5V or module-rated supply Not a Pico GPIO pin Use the proper module supply
Relay GND GND Physical pin 38 Share ground unless fully isolated
LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Flush activity LED
LED cathode GND Physical pin 23 or 38 Ground return

Wiring Diagram

Automated Urinal Flusher wiring diagram

  PIR OUT -> GPIO 14
  GPIO 15 -> relay IN
  GPIO 16 -> 220 ohm resistor -> LED anode
  Common GND -> PIR, relay, LED
  External supply -> relay-switched valve or demo load

Step-by-Step Assembly

  1. Connect the PIR sensor output to GPIO 14 and wire its power pins according to the module label.
  2. Connect the relay input to GPIO 15 and power the relay module from its proper supply.
  3. Wire the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to GND.
  4. Dry-test the relay logic before connecting the real valve or water-related load.
  5. Allow the PIR to warm up fully before validating the occupancy and flush timing.

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 PIR state and move in and out of the monitored area so you understand the occupancy timing first.
  3. Test the output device: Test the relay and LED with a simple timed script before using the occupancy logic.
  4. Test the decision logic: Confirm that one visit creates one flush after the person leaves, not while the person is still present.
  5. Run the full system: Run repeated entry and exit tests and compare the printed state changes with the hardware action.
  6. Validate the prototype: Check whether nearby movement causes false occupancy detections or extra flush cycles.
  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
RELAY_PIN = 15
LED_PIN = 16
VACANT_DELAY_MS = 5000
FLUSH_TIME_MS = 2000
COOLDOWN_MS = 15000

pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)

relay.off()
led.off()
occupied = False
last_motion_ms = 0
last_flush_ms = -COOLDOWN_MS
flush_done_for_visit = False

print("Automated urinal flusher ready")

while True:
    now = time.ticks_ms()
    motion = pir.value() == 1

    if motion:
        occupied = True
        last_motion_ms = now
        flush_done_for_visit = False
        print("Status: OCCUPIED")
    elif occupied and not flush_done_for_visit:
        vacant_time = time.ticks_diff(now, last_motion_ms)
        if vacant_time >= VACANT_DELAY_MS and time.ticks_diff(now, last_flush_ms) >= COOLDOWN_MS:
            print("Status: FLUSHING")
            relay.on()
            led.on()
            time.sleep_ms(FLUSH_TIME_MS)
            relay.off()
            led.off()
            last_flush_ms = time.ticks_ms()
            flush_done_for_visit = True
            occupied = False
        else:
            print("Status: WAITING TO FLUSH")
    else:
        print("Status: IDLE")

    time.sleep_ms(500)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
State variables Tracks occupancy, the last motion time, and whether the visit has already been flushed A timed actuator system needs memory of what just happened, not only a live sensor value Adjust the vacancy and cooldown timings only after real motion tests
Occupancy detection Marks the fixture as occupied whenever the PIR sees motion This separates presence detection from flushing so the valve does not trigger too early If false motion occurs, improve sensor placement before changing the code
Vacant-delay logic Waits until the area stays empty long enough before starting the flush A delayed flush is more realistic than immediate actuation Tune VACANT_DELAY_MS to match how long the user normally remains near the fixture
Flush cycle Runs the relay and LED for a fixed flush time and then applies cooldown protection This prevents repeated flushes and helps validate actuator timing safely Measure the actual valve open time before changing FLUSH_TIME_MS

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 could be used in school, farm, or sanitation demonstrations with safe low-voltage loads
  • Before deployment, the load wiring, enclosure, and power design need careful review
  • Actuator systems need maintenance planning because relay wear and sensor drift affect reliability

Troubleshooting

Problem Possible Cause Solution
Flushing starts too early Vacant delay is too short or the PIR loses the user too quickly Increase VACANT_DELAY_MS and adjust PIR positioning
Fixture flushes repeatedly Cooldown is too short or nearby motion keeps creating new visits Increase COOLDOWN_MS and narrow the PIR coverage area
Relay clicks but nothing happens Load wiring or external valve power is wrong Dry-test with a safe low-power load and then recheck the valve power path
PIR never detects a visit PIR warm-up or wiring is wrong Allow warm-up and verify the OUT pin reaches GPIO 14