Skip to content

Project 3.9.21: Smart Environment Management Suite

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

Overview

The Pico monitors air quality, temperature, and humidity, then manages a ventilation response while showing a local summary on an OLED.

Environmental monitoring is less useful when the system measures several conditions but never turns them into a clear management response.

A Pico 2 W prototype with an MQ-5 Liquefied Petroleum/Methane Gas Sensor, BME280, relay-driven fan, status LED, and OLED management dashboard.

Integrated environmental monitoring, multi-parameter decision logic, and safe actuator management.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Gas sensor
Soil sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
BME280
BME280
Relay
Relay
DC Fan
DC Fan
OLED
OLED
LED
LED
220 ohm resistor
220 ohm resistor
---

Circuit Connections

Component terminal Connect to Pico physical pin Notes
MQ-5 AO GPIO 26 ADC0 GPIO 26 / physical pin 31 LPG/methane gas proxy input
BME280 VCC 3.3V output Physical pin 36 Sensor power
BME280 GND GND Physical pin 38 Common ground
BME280 SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA shared with OLED if present
BME280 SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL shared with OLED if present
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL
Relay IN GPIO 16 GPIO 16 / physical pin 21 Fan control output
LED anode 220 ohm resistor to GPIO 17 GPIO 17 / physical pin 22 Ventilation status LED
---

Wiring Diagram

Circuit Diagram

MQ-5 AO -> GPIO 26
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
GPIO 16 -> relay IN
GPIO 17 -> 220 ohm resistor -> LED anode
External supply -> relay-switched fan
BME280 VCC -> 3.3V
BME280 GND -> GND

Diagram Placeholder [Insert wiring diagram or breadboard image here]


Step-by-Step Assembly

  1. Connect the MQ-5 analog output to GPIO 26 through a safe ADC path.
  2. Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
  3. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  4. Connect the relay input to GPIO 16 and power the relay module correctly.
  5. Wire the LED to GPIO 17 through a 220 ohm resistor and connect the LED cathode to ground.
  6. Attach the fan only after the relay logic has been validated with no load or a safe test load.

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-5 and BME280 separately so the environmental baseline is believable before management logic starts.
  3. Test the output device: Test the relay, LED, and OLED separately before running the full suite.
  4. Test the decision logic: Create heat, humidity, or odor stress one factor at a time and confirm the management state changes logically.
  5. Run the full system: Run the full system and compare the OLED summary with the live serial values and relay state.
  6. Validate the prototype: Discuss whether the management thresholds are too sensitive or too weak for the room being tested.
  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, I2C, Pin
import bme280
import time

try:
    from ssd1306 import SSD1306_I2C
    HAS_OLED = True
except ImportError:
    HAS_OLED = False

GAS_PIN = 26
BME_SDA_PIN = 20
BME_SCL_PIN = 21
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
RELAY_PIN = 16
LED_PIN = 17
GAS_MANAGE = 22000
GAS_CLEAR = 17000
TEMP_MANAGE = 31
TEMP_CLEAR = 28
HUMIDITY_MANAGE = 78
HUMIDITY_CLEAR = 70

mq5 = ADC(GAS_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
if HAS_OLED:
    oled = SSD1306_I2C(128, 64, i2c)

fan_on = False


def set_outputs(state):
    relay.value(1 if state else 0)
    led.value(1 if state else 0)


set_outputs(False)
print('Smart environment management suite ready')

while True:
    gas_value = mq5.read_u16()
    try:
        values = climate.values
        temperature = float(values[0].replace('C', ''))
        pressure = float(values[1].replace('hPa', ''))
        humidity = float(values[2].replace('%', ''))
    except Exception:
        temperature = 0
        pressure = 0
        humidity = 0

    if fan_on:
        if gas_value <= GAS_CLEAR and temperature <= TEMP_CLEAR and humidity <= HUMIDITY_CLEAR:
            fan_on = False
    else:
        if gas_value >= GAS_MANAGE or temperature >= TEMP_MANAGE or humidity >= HUMIDITY_MANAGE:
            fan_on = True

    set_outputs(fan_on)

    if fan_on:
        status = 'MANAGE'
    elif gas_value >= 18000 or temperature >= 29 or humidity >= 72:
        status = 'WATCH'
    else:
        status = 'NORMAL'

    print('Gas:{} Temp:{}C Hum:{}% Status:{}'.format(gas_value, temperature, humidity, status))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Env Suite', 0, 0)
        oled.text('G:{} T:{}'.format(gas_value, temperature), 0, 18)
        oled.text('H:{}%'.format(humidity), 0, 34)
        oled.text(status, 0, 50)
        oled.show()

    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Integrated sensor loop Reads gas, temperature, and humidity as one environment state. A management suite must combine several measurements instead of treating each one separately. Validate each sensor separately before tuning the final state thresholds.
Hysteresis management logic Keeps ventilation on until the room clearly recovers below the clear thresholds. This reduces rapid relay switching and makes the controller more realistic. Adjust the manage and clear thresholds only after repeated room tests.
OLED dashboard Shows the live measurements and management state on one screen. The local dashboard helps users explain what the controller is doing. Verify ssd1306.py and I2C wiring if the display remains blank.

Expected Result

The OLED and serial monitor show gas, temperature, humidity, and a management state.

The relay and LED turn on together when the environment crosses the management threshold.

The fan stays active until the readings recover below the lower clear thresholds.

Validation Checks

  • Normal condition test: keep the room stable and confirm the suite reports NORMAL with the fan off.
  • Warning condition test: raise one reading moderately and confirm the suite reports WATCH without unnecessary fan chatter.
  • Management condition test: raise gas, temperature, or humidity above the management threshold and confirm the relay activates.
  • Output response validation: test the relay first without the fan, then reconnect the fan after safe switching is confirmed.
  • Calibration test: warm up the MQ-5 and compare climate readings with known room conditions before finalizing thresholds.

Deployment and Limitations

  • This prototype could be used in school, farm, or sanitation demonstrations with safe low-voltage loads.
  • Before deployment, the load wiring, enclosure, and power design need careful review.
  • Actuator systems need maintenance planning because relay wear and sensor drift affect reliability.

Troubleshooting

Problem Possible Cause Solution
The fan never turns on The relay wiring is wrong or the thresholds are too high Test the relay separately and temporarily lower the management thresholds during debugging.
The fan never turns off The clear thresholds are too strict or the room has not recovered Print the live readings and raise the clear thresholds carefully.
Humidity stays at zero The BME280 read failed Test the BME280 separately and shorten the I2C wires if needed.
OLED is blank The display library or I2C wiring is wrong Confirm ssd1306.py is on the Pico and recheck GPIO 20 and GPIO 21.