Skip to content

Project 3.9.42: Smart Sanitation Alert System

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

Overview

The Pico combines usage count and odor-proxy data to decide when sanitation maintenance is needed.

Maintenance needs are harder to justify when usage and odor conditions are considered separately instead of together.

A Pico 2 W prototype with a PIR sensor, MQ-135 gas sensor, LED, and buzzer.

Multi-factor sanitation logic, threshold design, and prototype alert validation.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Gas sensor
Gas sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Motion sensor
Motion sensor
Buzzer
Buzzer
---

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR OUT GPIO 14 GPIO 14 / physical pin 19 Usage input
MQ-135 AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Odor proxy input
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Warning LED
Buzzer positive GPIO 16 GPIO 16 / physical pin 21 Critical alert
---

Wiring Diagram

Circuit Diagram

  PIR OUT            -> GPIO 14
  MQ-135 AO          -> GPIO 26
  GPIO 15            -> 220 ohm resistor -> LED anode
  GPIO 16            -> buzzer positive
  Common GND         -> PIR, MQ-135, LED, and buzzer

Step-by-Step Assembly

  1. Connect the PIR output to GPIO 14 and allow the sensor to warm up before testing counts.
  2. Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
  3. Wire the LED to GPIO 15 through a 220 ohm resistor and the buzzer to GPIO 16.
  4. Warm up the MQ-135 and measure a baseline before choosing the odor threshold.

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 PIR counter and MQ-135 reading separately before combining them.
  3. Test the output device: Test the LED and buzzer independently before running the full logic.
  4. Test the decision logic: Increase usage and odor separately, then together, to confirm the multi-factor alert works as intended.
  5. Run the full system: Run the full system and compare the printed conditions with the output state.
  6. Validate the prototype: Check whether PIR placement or sensor warm-up could cause false alerts.
  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, Pin
import time

PIR_PIN = 14
GAS_PIN = 26
LED_PIN = 15
BUZZER_PIN = 16
USAGE_WARN = 6
USAGE_MAINTENANCE = 10
GAS_WARN = 18000
GAS_MAINTENANCE = 28000

pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
gas_sensor = ADC(GAS_PIN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
usage_count = 0
occupied = False

print('Sanitation maintenance alert ready')

while True:
    motion = pir.value() == 1
    if motion and not occupied:
        usage_count += 1
        occupied = True
    elif not motion:
        occupied = False

    gas_value = gas_sensor.read_u16()
    if usage_count >= USAGE_MAINTENANCE and gas_value >= GAS_MAINTENANCE:
        state = 'MAINTENANCE'
    elif usage_count >= USAGE_WARN or gas_value >= GAS_WARN:
        state = 'WATCH'
    else:
        state = 'NORMAL'

    led.value(1 if state in ('WATCH', 'MAINTENANCE') else 0)
    buzzer.value(1 if state == 'MAINTENANCE' else 0)
    print('Usage:{} Gas:{} State:{}'.format(usage_count, gas_value, state))
    time.sleep(0.5)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Usage counting Tracks visits with edge-based PIR logic The alert should depend on real usage rather than a continuously high PIR signal Count several real passes to validate the event logic
Rule-based AI-style logic Combines use count and odor thresholds into one maintenance state This makes the decision more realistic than a single-sensor trigger Explain clearly that this is threshold scoring, not trained machine learning
Alert outputs Uses the LED for watch state and the buzzer for maintenance state Escalating outputs make the severity easier to interpret Test the LED path first before enabling the buzzer

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 can be used for local warning demonstrations and early field trials
  • Before deployment, it needs enclosure protection, calibration records, and a clear response procedure
  • Alert systems should always be treated as decision-support tools unless they are certified for the application

Troubleshooting

Problem Possible Cause Solution
The alert appears too soon Thresholds are too low or the MQ-135 is still warming up Observe the baseline first and then adjust the thresholds
Usage count never increases The PIR is not detecting motion or is wired incorrectly Test the PIR on GPIO 14 with a simple print script
Buzzer never sounds Both maintenance conditions are not being met Create a clearer test case and confirm the logic thresholds in the code
Gas readings are unstable The sensor is warming up or the analog path is noisy Allow more warm-up time and keep the ADC wiring short