Skip to content

Project 3.9.50: Smart Sanitation Innovation System

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

Overview

The Pico detects rising sanitation air-quality risk, escalates local alerts, and allows a user to acknowledge the buzzer while keeping visible warning states active.

Alert systems are less useful when they either stay silent until conditions are severe or continue buzzing even after a user has seen the warning.

A Pico 2 W prototype with an MQ-135 gas sensor, acknowledgement button, two LEDs, and a buzzer.

Multi-level alerting, acknowledgement behavior, and innovation grounded in practical sanitation response design.


Required Components

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

Circuit Connections

Component terminal Connect to Pico physical pin Notes
MQ-135 AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Odor proxy input
Button leg 1 GPIO 14 GPIO 14 / physical pin 19 Acknowledge button input
Button leg 2 3.3V output Physical pin 36 High when pressed
Green LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Normal-state LED
Red LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Warning-state LED
Buzzer positive GPIO 17 GPIO 17 / physical pin 22 Audible alert output
---

Wiring Diagram

Circuit Diagram

MQ-135 AO -> GPIO 26
Button -> GPIO 14 and 3.3V
GPIO 15 -> 220 ohm resistor -> green LED anode
GPIO 16 -> 220 ohm resistor -> red LED anode
GPIO 17 -> buzzer positive
Common GND -> MQ-135, LEDs, and buzzer

Diagram Placeholder [Insert wiring diagram or breadboard image here]


Step-by-Step Assembly

  1. Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
  2. Wire the acknowledgement button between GPIO 14 and 3.3V so the input goes high when pressed.
  3. Connect the green LED to GPIO 15 through a 220 ohm resistor and the red LED to GPIO 16 through another 220 ohm resistor.
  4. Connect the buzzer positive pin to GPIO 17 and its negative pin to ground.
  5. Warm up the MQ-135 fully before choosing the environmental thresholds.

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 gas reading and acknowledgement button separately so the alert logic has trustworthy inputs.
  3. Test the output device: Test each LED and the buzzer separately before running the multi-level alert logic.
  4. Test the decision logic: Create warning and critical conditions and confirm the visual alerts and acknowledgement behavior work as designed.
  5. Run the full system: Run the full alert system and compare the serial status with the visible and audible outputs.
  6. Validate the prototype: Discuss whether the acknowledgement behavior improves usability without hiding important information.
  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.

Upload and run this code after the individual tests work correctly.

from machine import ADC, Pin
import time

GAS_PIN = 26
BUTTON_PIN = 14
GREEN_LED_PIN = 15
RED_LED_PIN = 16
BUZZER_PIN = 17
WARN_THRESHOLD = 18000
CRITICAL_THRESHOLD = 30000

mq135 = ADC(GAS_PIN)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
red_led = Pin(RED_LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
acknowledged = False
last_button = 0

print('Smart sanitation innovation system ready')

while True:
    gas_value = mq135.read_u16()
    button_now = button.value()

    if gas_value >= CRITICAL_THRESHOLD:
        state = 'CRITICAL'
    elif gas_value >= WARN_THRESHOLD:
        state = 'WARNING'
    else:
        state = 'NORMAL'
        acknowledged = False

    if button_now == 1 and last_button == 0 and state != 'NORMAL':
        acknowledged = True
        print('Alert acknowledged')
    last_button = button_now

    green_led.value(1 if state == 'NORMAL' else 0)
    red_led.value(1 if state in ('WARNING', 'CRITICAL') else 0)
    buzzer.value(1 if state in ('WARNING', 'CRITICAL') and not acknowledged else 0)

    print('Gas:{} State:{} Ack:{}'.format(gas_value, state, acknowledged))
    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Alert classification Maps the gas reading into NORMAL, WARNING, or CRITICAL. Clear levels are easier for users to understand than raw sensor values alone. Set the thresholds only after sensor warm-up and baseline measurement.
Acknowledgement logic Lets the user silence the buzzer while keeping the visible warning active. This is the main innovation idea of the project and makes the alert more practical to use. Check the button state during testing if acknowledgement seems unreliable.
Output control Drives the green LED, red LED, and buzzer according to the current state. Separate visible and audible behavior gives a more realistic local response design. If a buzzer stays on unexpectedly, print the current state and acknowledgement flag.

Expected Result

The green LED stays on during normal conditions and the red LED stays off.

When the gas reading rises above the warning threshold, the red LED and buzzer activate.

Pressing the button silences the buzzer while the red LED continues to show that the warning condition still exists.

Validation Checks

  • Normal condition test: confirm the green LED is on and the red LED and buzzer are off after warm-up.
  • Warning condition test: raise the gas reading above the warning threshold and confirm the red LED and buzzer turn on.
  • Critical condition test: raise the gas reading further and confirm the system reports CRITICAL.
  • Acknowledgement test: press the button during a warning or critical condition and confirm only the buzzer turns off.
  • Reset test: let the reading return to normal and confirm acknowledgement clears automatically for the next alert event.

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 buzzer never sounds The buzzer wiring is wrong or the acknowledgement flag was set already Test the buzzer directly from a short script and confirm acknowledgement resets when the state returns to normal.
The buzzer never stops The button wiring is wrong or the state never leaves warning Print the button value during testing and verify the alert threshold against the warmed-up baseline.
The red LED is always on The warning threshold is too low or the MQ-135 is still warming up Allow full warm-up and raise the threshold after measuring the room baseline.
The button does not acknowledge the alert The button input never goes high Test the button alone and verify it is wired between GPIO 14 and 3.3V.