Project 3.9.56: Smart Sanitation Predictive System
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico uses recent visit and odor trends to predict whether a sanitation space is likely to need service soon.
Maintenance stays reactive when staff only notice a problem after the space has already become uncomfortable or overused.
A Pico 2 W prototype with a PIR sensor, MQ-135 gas sensor, and OLED predictive dashboard.
Trend-based sanitation prediction, explainable service scoring, and honest forecasting language.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Gas sensor |
![]() Breadboard |
![]() Jumper wires |
![]() PIR Sensor |
![]() OLED |
||
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| PIR OUT | GPIO 14 | GPIO 14 / physical pin 19 | Usage event input |
| MQ-135 AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Odor proxy input |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| --- |
Wiring Diagram

PIR OUT -> GPIO 14
MQ-135 AO -> GPIO 26
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> PIR, MQ-135, and OLED
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the PIR output to GPIO 14 and allow warm-up before counting events.
- Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
- Upload ssd1306.py and confirm it appears on the Pico before running the predictive dashboard.
- Warm up the MQ-135 and capture a baseline before trusting the odor trend.
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.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Test the PIR and MQ-135 separately so the prediction starts from trusted inputs.
- Test the output device: Run an OLED hello-world test before adding the prediction logic.
- Test the decision logic: Increase visits or odor one factor at a time and confirm the prediction changes only when the trend supports it.
- Run the full system: Run the full system and compare the prediction with the live visit count and odor average.
- Validate the prototype: Discuss whether the trend window is long enough to be useful but short enough to react in time.
- 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.
Upload and run this code after the individual tests work correctly.
from machine import ADC, I2C, Pin
import time
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
PIR_PIN = 14
GAS_PIN = 26
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
HISTORY_LENGTH = 6
pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
gas = ADC(GAS_PIN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
last_pir = 0
visit_count = 0
odor_history = []
def average(values):
return int(sum(values) / len(values)) if values else 0
print('Smart sanitation predictive system ready')
while True:
occupancy = pir.value()
if occupancy == 1 and last_pir == 0:
visit_count += 1
last_pir = occupancy
odor = gas.read_u16()
odor_history.append(odor)
odor_history = odor_history[-HISTORY_LENGTH:]
avg_odor = average(odor_history)
if visit_count >= 6 and avg_odor >= 22000:
prediction = 'SERVICE SOON'
elif visit_count >= 3 or avg_odor >= 18000:
prediction = 'WATCH'
else:
prediction = 'STABLE'
print('Visits:{} Odor:{} Avg:{} Prediction:{}'.format(visit_count, odor, avg_odor, prediction))
if HAS_OLED:
oled.fill(0)
oled.text('San Predict', 0, 0)
oled.text('V:{} O:{}'.format(visit_count, odor), 0, 18)
oled.text('Avg:{}'.format(avg_odor), 0, 34)
oled.text(prediction[:16], 0, 50)
oled.show()
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Event and trend tracking | Counts visits and stores recent odor values. | Prediction only works when both usage and condition trends are visible. | Watch how the history behaves before changing the thresholds. |
| Prediction rules | Maps rising usage and odor trend into STABLE, WATCH, or SERVICE SOON. | This gives the project an honest predictive layer without overclaiming machine learning. | Tune the thresholds only after repeated tests with real room behavior. |
| OLED dashboard | Shows visits, odor average, and the prediction on one screen. | The user can compare the prediction with the evidence that produced it. | Verify ssd1306.py and the I2C wiring if the display is blank. |
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 works well for classroom demonstrations and local monitoring pilots.
- Before deployment, it needs calibration records, protective mounting, and regular validation checks.
- Display-only systems still depend on correct sensor placement and trained users.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| The prediction never changes | The thresholds are too strict or the room conditions are too stable | Create clearer test conditions and lower the thresholds only after observing real values. |
| The visit count rises too fast | The PIR retriggers too often or placement is poor | Reposition the PIR and compare the count with manual observation. |
| Average odor barely changes | The MQ-135 is not warmed up or the room is too stable | Allow more warm-up time and create clearer odor changes during testing. |
| OLED is blank | The display library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21. |





