Skip to content

Project 3.1.21: Pollution Indicator Board

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


Overview

The Pico monitors an air-quality proxy reading, then shows whether the local environment looks normal, watch-level, or poor.

Air-quality problems are harder to discuss when learners only see a raw sensor value and not a simple interpreted status.

A Pico 2 W prototype with an MQ-135 gas sensor, OLED display, and status LED.

Air-quality proxy monitoring, threshold interpretation, and basic validation of low-cost gas sensing.

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
OLED
OLED

Circuit Connections

Component terminal Connect to Pico physical pin Notes
MQ-135 AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Air-quality proxy input
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL
LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Warning LED

Wiring Diagram

Circuit Diagram

  MQ-135 AO                   -> GPIO 26
  GPIO 20                     -> OLED SDA
  GPIO 21                     -> OLED SCL
  GPIO 16 -> 220 ohm resistor -> LED anode
  Common GND                  -> MQ-135, OLED, and LED

Step-by-Step Assembly

  1. Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
  2. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  3. Wire the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to ground.
  4. Upload ssd1306.py and confirm it appears in os.listdir() before running the monitor.
  5. Warm up the MQ-135 and capture a room baseline before finalizing the 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 MQ-135 reading separately so you understand the room baseline before adding status logic.
  3. Test the output device: Run an OLED hello-world test and a simple LED blink test before integrating the full monitor.
  4. Test the decision logic: Change the local air condition and confirm the status changes only when the threshold is crossed.
  5. Run the full system: Run the full monitor and compare the OLED label with the printed raw readings.
  6. Validate the prototype: Discuss what could cause misleading air-quality proxy readings in this room.
  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

SENSOR_PIN = 26
LED_PIN = 16
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
WATCH_THRESHOLD = 18000
POOR_THRESHOLD = 28000

sensor = ADC(SENSOR_PIN)
led = Pin(LED_PIN, Pin.OUT)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
    oled = SSD1306_I2C(128, 64, i2c)

print('Air quality proxy monitor ready')

while True:
    reading = sensor.read_u16()
    if reading >= POOR_THRESHOLD:
        status = 'POOR'
    elif reading >= WATCH_THRESHOLD:
        status = 'WATCH'
    else:
        status = 'NORMAL'

    led.value(1 if status != 'NORMAL' else 0)
    print('Reading:{} Status:{}'.format(reading, status))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Air Proxy', 0, 0)
        oled.text('Raw:{}'.format(reading), 0, 20)
        oled.text(status, 0, 42)
        oled.show()

    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Threshold bands Maps the air-proxy reading into NORMAL, WATCH, or POOR A status label is easier to interpret than the raw number alone Adjust the thresholds only after measuring a stable baseline
OLED output Displays the raw reading and current label The screen helps learners compare the evidence with the conclusion Verify ssd1306.py and I2C wiring if the display is blank
LED warning Turns on when the status is not normal A quick visual warning is useful even without reading the display If the LED does not respond, test the pin with a simple blink script

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 status is always poor The sensor is still warming up or the thresholds are too low Allow more warm-up time and increase the thresholds after observing the baseline
OLED is blank The display library or I2C wiring is wrong Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21
LED never changes The pin or resistor wiring is wrong Run a short LED blink test on GPIO 16 and recheck the wiring
Readings barely change The room conditions are too stable for a visible test Test in a different room or after more warm-up time