Skip to content

Project 3.1.29: Toxic Gas Sanitation Alert

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


Overview

The Pico reads an MQ-135 style gas sensor and raises warning or critical alerts when the analog gas level rises.

Closed sanitation spaces can accumulate unpleasant or unsafe gas, so early warning matters even in a classroom prototype.

A Pico 2 W prototype with an MQ-series gas sensor, LED, buzzer, and analog threshold logic.

Sensor warm-up, threshold calibration, and honest explanation of non-certified gas measurements.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280
BME280
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Buzzer
Buzzer

Circuit Connections

Component terminal Connect to Pico physical pin Notes
MQ sensor VCC Module-rated supply Often external 5V Check the board label before powering
MQ sensor GND GND Physical pin 38 Common ground
MQ sensor AO Voltage-divider midpoint to GPIO 26 GPIO 26 / physical pin 31 Do not connect a 5V analog output directly
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 buzzer

Wiring Diagram

Toxic Gas Sanitation Alert wiring diagram

  MQ AO -> resistor divider -> GPIO 26
  GND -> MQ GND, LED cathode, buzzer negative
  GPIO 15 -> 220 ohm resistor -> LED anode
  GPIO 16 -> buzzer positive
  External supply -> MQ sensor heater power if required

Step-by-Step Assembly

  1. Power the MQ sensor according to its board label and connect its ground to Pico GND.
  2. Build a resistor divider on the analog output before connecting the divider midpoint to GPIO 26.
  3. Connect the LED to GPIO 15 through a 220 ohm resistor and connect the LED cathode to GND.
  4. Connect the buzzer positive pin to GPIO 16 and its negative pin to GND.
  5. Allow the sensor to warm up before recording any calibration values.

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: Print gas values for several minutes after warm-up so you understand the room baseline before setting thresholds.
  3. Test the output device: Test the LED and buzzer outputs separately before using them with the gas sensor.
  4. Test the decision logic: Change the threshold constants only after observing stable baseline and elevated readings.
  5. Run the full system: Run the full system and confirm the LED and buzzer match the printed status transitions.
  6. Validate the prototype: Repeat tests in different rooms or ventilation conditions and note how much the baseline shifts.
  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

GAS_PIN = 26
LED_PIN = 15
BUZZER_PIN = 16
WARNING_LEVEL = 18000
CRITICAL_LEVEL = 30000
WARMUP_SECONDS = 60

gas = ADC(GAS_PIN)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)


def read_level(samples=10):
    total = 0
    for _ in range(samples):
        total += gas.read_u16()
        time.sleep_ms(20)
    return total // samples


print("Warming gas sensor...")
for remaining in range(WARMUP_SECONDS, 0, -1):
    print("Warm-up remaining:", remaining)
    time.sleep(1)

print("Gas alert ready")

while True:
    level = read_level()

    if level >= CRITICAL_LEVEL:
        status = "CRITICAL"
        led.on()
        buzzer.on()
    elif level >= WARNING_LEVEL:
        status = "WARNING"
        led.on()
        buzzer.off()
    else:
        status = "NORMAL"
        led.off()
        buzzer.off()

    print("Gas level: {} | Status: {}".format(level, status))
    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Warm-up phase Waits before trusting the gas sensor output MQ sensors need time to stabilize before calibration is meaningful Increase WARMUP_SECONDS if your module needs a longer initial settle time
read_level Averages several analog readings into one gas-level value Averaging makes it easier to compare room baseline and elevated conditions Increase the sample count if the value is too noisy
Threshold logic Separates NORMAL, WARNING, and CRITICAL gas states Clear states make it easier to validate what the prototype is doing Set WARNING_LEVEL and CRITICAL_LEVEL from real room tests instead of guessing
Alert outputs Turns on the LED for warning and both outputs for critical conditions Students can see graded response instead of a single alarm only Keep the buzzer reserved for the critical state if classroom testing must stay manageable

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: record the stable warm-up baseline in normal room air
  • Warning condition test: expose the sensor to a slightly elevated but controlled condition and confirm the LED turns on first
  • Critical condition test: create a stronger controlled test condition and confirm the buzzer activates
  • Calibration test: compare repeated warm-room baselines before deciding on threshold values
  • Limitation test: explain why the prototype cannot certify health safety or identify specific gases accurately

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
Gas value is always zero or flat Analog output or divider wiring is incorrect Rebuild the AO divider and confirm the midpoint reaches GPIO 26
System is always in alarm Thresholds are too low or the room baseline is high Record the stable warm baseline and raise WARNING_LEVEL and CRITICAL_LEVEL appropriately
Readings drift a lot Sensor has not warmed fully or room airflow is changing rapidly Extend warm-up time and test in a more stable airflow condition
Buzzer never activates Critical threshold is too high or the buzzer wiring is wrong Lower CRITICAL_LEVEL briefly for a safe test and verify the buzzer separately