Skip to content

Project 3.9.58: Smart Sanitation Sustainability Tracker

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

Overview

The Pico tracks sanitation visits and odor events, then estimates a simple sustainability score for the current service cycle.

Sustainability discussions remain vague when usage and odor problems are not linked to one simple service-cycle metric.

A Pico 2 W prototype with a PIR sensor, MQ-135 gas sensor, reset button, and OLED sustainability dashboard.

Service-cycle tracking, odor-event ratios, and local sustainability scoring.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Gas sensor
Gas sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
PIR Sensor
PIR Sensor
OLED
OLED
Push button
Push button
---

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR OUT GPIO 14 GPIO 14 / physical pin 19 Visit input
MQ-135 AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Odor proxy input
Reset button leg 1 GPIO 16 GPIO 16 / physical pin 21 Service-cycle reset input
Reset button leg 2 3.3V output Physical pin 36 High when pressed
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL
---

Wiring Diagram

Circuit Diagram

  PIR OUT -> GPIO 14
  MQ-135 AO -> GPIO 26
  Button -> GPIO 16 and 3.3V
  GPIO 20 -> OLED SDA
  GPIO 21 -> OLED SCL

Step-by-Step Assembly

  1. Connect the PIR output to GPIO 14 and allow the sensor to warm up.
  2. Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
  3. Wire the reset button between GPIO 16 and 3.3V so the pin goes high when pressed.
  4. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  5. Upload ssd1306.py and confirm it appears on the Pico before running the tracker.

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: Test the visit edge logic and odor-event threshold separately before using the score.
  3. Test the output device: Test the reset button and OLED separately before running the full cycle tracker.
  4. Test the decision logic: Confirm that visits and odor events increment on new events only and that reset clears the cycle.
  5. Run the full system: Run the full tracker and compare the score with your manual understanding of the service cycle.
  6. Validate the prototype: Discuss which real-world sustainability factors are still missing from the score.
  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 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
BUTTON_PIN = 16
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
ODOR_EVENT = 22000

pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
gas = ADC(GAS_PIN)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
    oled = SSD1306_I2C(128, 64, i2c)

visits = 0
odor_events = 0
last_pir = 0
gas_in_event = False


def sustainability_label(visit_count, odor_count):
    if visit_count == 0:
        return 'NO DATA'
    ratio = odor_count / visit_count
    if ratio >= 0.4:
        return 'LOW'
    if ratio >= 0.15:
        return 'WATCH'
    return 'GOOD'


print('Sanitation sustainability tracker ready')

while True:
    if button.value() == 1:
        visits = 0
        odor_events = 0
        gas_in_event = False
        print('Cycle reset')
        time.sleep_ms(300)

    pir_value = pir.value()
    if pir_value == 1 and last_pir == 0:
        visits += 1
    last_pir = pir_value

    gas_value = gas.read_u16()
    if gas_value >= ODOR_EVENT and not gas_in_event:
        odor_events += 1
        gas_in_event = True
    elif gas_value < ODOR_EVENT - 2000:
        gas_in_event = False

    label = sustainability_label(visits, odor_events)
    print('Visits:{} OdorEvents:{} Label:{}'.format(visits, odor_events, label))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Sustainability', 0, 0)
        oled.text('V:{} O:{}'.format(visits, odor_events), 0, 18)
        oled.text(label, 0, 40)
        oled.show()

    time.sleep_ms(150)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Event counting Counts visits and odor events on new edges only This keeps the score meaningful instead of letting one long event dominate the cycle Check both edge conditions before trusting the score
Cycle reset Clears the current service-cycle values when the reset button is pressed Service-cycle thinking is important for maintenance workflows Debounce the button more if repeated resets occur
sustainability_label Turns the event ratio into GOOD, WATCH, or LOW sustainability The label helps students connect data with service planning decisions Adjust the ratio thresholds after observing real test behavior
OLED tracker Shows the current cycle metrics locally The dashboard makes the cycle easier to review than serial output alone Confirm ssd1306.py and 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 score never improves Odor events are counted too often or the threshold is too low Raise the odor threshold and confirm one event is counted only once per actual condition
Reset does nothing The button wiring is wrong Print the button value in a simple test and verify it goes high when pressed
Visits rise too fast The PIR sees too much motion or the edge logic is not being isolated Narrow the PIR coverage and retest with simple movement
OLED is blank The display library or I2C wiring is wrong Confirm ssd1306.py is present and recheck GPIO 20 and GPIO 21