Project 2.6.28: Smart Sanitation Facility Monitor
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a sanitation facility monitor that combines user activity and an air-quality proxy into one maintenance score.
Students will combine a motion or entry sensor, an analog air-quality proxy sensor, a cleaning-reset button, and warning outputs into one rule-based maintenance system.
The final system should count visits, estimate a service score, raise ATTENTION and SERVICE NEEDED states, and let staff reset the service score after cleaning.
Required Components
![]() Red LED and 220Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() Push button |
![]() Yellow LED and 220Ω resistor |
![]() Active buzzer |
![]() Breadboard and jumper wires |
![]() Air-quality / gas sensor |
![]() Motion sensor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Motion sensor VCC | Module dependent | Follow sensor label | Use a low-voltage compatible output |
| Motion sensor GND | GND | Any GND pin | Common ground |
| Motion sensor output | GPIO 4 | GPIO 4 / physical pin 6 | Digital visit input |
| Air-quality proxy sensor analog output | GPIO 26 | GPIO 26 / physical pin 31 | ADC service-condition proxy input |
| Cleaning reset button one side | GPIO 5 | GPIO 5 / physical pin 7 | Uses internal pull-up |
| Cleaning reset button other side | GND | Any GND pin | Pressing pulls the input low |
| Yellow LED anode | GPIO 16 through 220Ω resistor | GPIO 16 / physical pin 21 | Attention indicator |
| Red LED anode | GPIO 17 through 220Ω resistor | GPIO 17 / physical pin 22 | Service-needed indicator |
| Buzzer positive | GPIO 18 | GPIO 18 / physical pin 24 | Service-needed buzzer |
| LED cathodes and buzzer negative | GND | Any GND pin | Return path |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 4 ───────────┼──── Motion sensor output
│ GPIO 26 ───────────┼──── Air-quality proxy sensor analog output
│ GPIO 5 ───────────┼──── Cleaning reset button ──┐
│ GPIO 16 ───────────┼──── Resistor (220Ω) ── Yellow LED anode (+)
│ GPIO 17 ───────────┼──── Resistor (220Ω) ── Red LED anode (+)
│ GPIO 18 ───────────┼──── Buzzer positive (+)
│ GND ───────────┼──── Motion GND / Proxy GND / Both LED cathodes / Buzzer negative (-)
│ │
└─────────────────────┘
Motion Sensor
VCC ─────────────── Safe module supply
GND ─────────────── GND
OUT ─────────────── GPIO 4
Air-Quality Proxy Sensor
VCC ─────────────── Safe module supply
GND ─────────────── GND
AOUT ─────────────── GPIO 26 (3.3V-safe signal)
Cleaning Reset Button
One side ─────────── GPIO 5
Other side ───────── GND
Yellow LED (Attention)
Anode (+) ──[220Ω]── GPIO 16
Cathode (-) ───────── GND
Red LED (Service Needed)
Anode (+) ──[220Ω]── GPIO 17
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 Motion or Entry Sensor
Place the motion or entry sensor where it can represent facility activity without being splashed. Identify VCC, GND, and output before wiring.
Step 3: Connect the Motion or Entry Sensor
Connect sensor VCC to the safe low-voltage supply required by the module label. Connect sensor GND to GND. Connect sensor output to GPIO 4.
Step 4: Place and Connect the Air-Quality Proxy Sensor
Place the air-quality proxy sensor where it can sense a safe test condition. Identify VCC, GND, and analog output before wiring. Connect sensor VCC to the safe supply required by the module label. Connect sensor GND to GND. Connect sensor analog output to GPIO 26. Confirm the analog signal reaching GPIO 26 stays within 0V to 3.3V.
Step 5: Place the Cleaning Reset Button
Place the cleaning reset push button across the breadboard center gap. Connect one side of the button to GPIO 5. Connect the opposite side of the button to GND.
Step 6: Place and Connect the Service Outputs
Place the yellow attention LED and red service LED on the breadboard. Connect the yellow LED long leg through a 220Ω resistor to GPIO 16. Connect the red LED long leg through a 220Ω resistor to GPIO 17. Connect both LED short legs to GND. 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] Motion or entry sensor output connects to GPIO 4
- [x] Air-quality proxy sensor analog output connects to GPIO 26
- [x] Proxy sensor output is confirmed 3.3V-safe before connecting to the Pico
- [x] Cleaning reset button connects between GPIO 5 and GND
- [x] Yellow LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Red LED long leg connects through a 220Ω resistor to GPIO 17
- [x] Buzzer positive pin connects to GPIO 18
- [x] Buzzer negative pin connects to GND
- [x] No loose jumper wires
Intermediate Note
Test the digital activity sensor and analog proxy sensor separately before combining them into a service score. Press the reset button only after a simulated cleaning event.
Safety Note
This project is a maintenance aid only and does not replace real hygiene inspection or ventilation checks. Keep electronics away from water and avoid exposing students to harmful fumes.
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
- Build the motion sensor, proxy sensor, and reset button first, then add the LED and buzzer outputs
- Check that the analog proxy output is safe for the Pico before applying power
Test the input sensor
- Trigger one motion event and confirm the visit count increases only once per visit window
- Observe the air-quality proxy value under normal conditions so you know the typical baseline
Test the output device
- Force ATTENTION and SERVICE NEEDED states briefly and confirm the LEDs and buzzer behave correctly
- Confirm the buzzer sounds only in the stronger service-needed state
Test communication
- Watch the Thonny Shell and confirm it prints the visit count, proxy value, service score, and current state
- Use the printed score to tune how strongly visits and proxy changes affect maintenance urgency
Run the full system
- Generate several visit events and observe how the score rises even if the air proxy stays low
- Then add a stronger proxy change and confirm the system reaches SERVICE NEEDED more quickly
Save the project
- Save the final code and record the visit and proxy thresholds used in your test
- Write down how a real cleaning team would use the service-reset button
Quick testing checklist
- ☐ Motion sensor counts visits correctly
- ☐ Air-quality proxy value is stable and within range
- ☐ Yellow LED shows ATTENTION state
- ☐ Red LED and buzzer show SERVICE NEEDED state
- ☐ Reset button clears the service score after cleaning
- ☐ Shell shows visit count, proxy value, score, and state
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 ADC, Pin
import time
visit_sensor = Pin(4, Pin.IN, Pin.PULL_UP)
proxy_sensor = ADC(26)
reset_button = Pin(5, Pin.IN, Pin.PULL_UP)
attention_led = Pin(16, Pin.OUT)
service_led = Pin(17, Pin.OUT)
buzzer = Pin(18, Pin.OUT)
VISIT_ACTIVE_LEVEL = 0
VISIT_LOCKOUT_SECONDS = 8
ATTENTION_SCORE = 40
SERVICE_SCORE = 70
PROXY_HIGH = 65
DEBOUNCE_MS = 250
visit_count = 0
last_visit_time = 0
last_button_ms = 0
odor_hits = 0
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 proxy_percent():
return int((proxy_sensor.read_u16() / 65535) * 100)
def service_score(visits, proxy):
score = min(100, (visits * 3) + max(0, proxy - 30))
return score
def beep():
for _ in range(2):
buzzer.value(1)
time.sleep(0.12)
buzzer.value(0)
time.sleep(0.12)
print('=== Smart Sanitation Facility Monitor ===')
print('Press the reset button after cleaning to clear the service score.\n')
while True:
now = time.time()
proxy = proxy_percent()
if visit_sensor.value() == VISIT_ACTIVE_LEVEL and (now - last_visit_time) >= VISIT_LOCKOUT_SECONDS:
visit_count += 1
last_visit_time = now
print('Visit detected. Count = {}'.format(visit_count))
if button_pressed():
visit_count = 0
odor_hits = 0
print('Cleaning reset completed. Service score cleared.')
if proxy >= PROXY_HIGH:
odor_hits += 1
else:
odor_hits = max(0, odor_hits - 1)
score = service_score(visit_count, proxy)
if score >= SERVICE_SCORE or odor_hits >= 3:
state = 'SERVICE NEEDED'
attention_led.value(0)
service_led.value(1)
beep()
elif score >= ATTENTION_SCORE:
state = 'ATTENTION'
attention_led.value(1)
service_led.value(0)
buzzer.value(0)
else:
state = 'OK'
attention_led.value(0)
service_led.value(0)
buzzer.value(0)
print('Visits: {} | Proxy: {}% | Odor hits: {} | Score: {} | State: {}'.format(
visit_count, proxy, odor_hits, score, state
))
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Visit lockout | Prevents one person from being counted many times in a few seconds | This makes the usage counter more realistic |
| Proxy percentage | Turns the analog service-condition sensor into a simple 0 to 100 scale | The service score can combine different inputs more easily |
| Service score | Combines visit count and condition change into one maintenance value | This is the rule-based logic that makes the monitor more useful than a single threshold |
| Cleaning reset | Lets staff clear the score after maintenance is performed | Real facility monitoring needs a clear maintenance workflow |
Expected Result
As visits increase, the service score should rise even if the proxy value stays modest. If the proxy sensor also reports a high condition value, the system should reach ATTENTION or SERVICE NEEDED more quickly. Pressing the reset button after cleaning should clear the service score and return the system to normal monitoring.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The visit count rises too fast | The lockout is too short or the motion sensor is retriggering too often | Increase VISIT_LOCKOUT_SECONDS or reposition the sensor. |
| The score is always high | The proxy value is too large all the time or the score formula is too aggressive | Check the analog sensor wiring and reduce the score contribution if needed. |
| The reset button does not clear the state | The button wiring is wrong or the state is immediately re-triggered by a high proxy value | Check the button wiring and verify the proxy sensor is actually back in a lower range. |
| The buzzer sounds constantly | The system is always in SERVICE NEEDED or the buzzer output is stuck high | Check the service score logic and verify the GPIO 18 wiring. |






