Project 3.9.19: Smart Environment Early Warning Ai
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico watches for rapid changes in temperature and LPG/methane gas proxy readings, then escalates a local warning before conditions become extreme.
Slow threshold alarms can miss situations where the environment is changing quickly and action should begin earlier.
A Pico 2 W prototype with an MQ-5 Liquefied Petroleum/Methane Gas Sensor, BME280, LED, and buzzer for local early warning.
Rate-of-change monitoring, multi-level warning states, and honest AI-style alert logic.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Soil sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LED |
![]() Resistor |
![]() Buzzer |
![]() BME280 |
| --- |
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 |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| LED anode | 220 ohm resistor to GPIO 16 | GPIO 16 / physical pin 21 | Visual alert output |
| Buzzer positive | GPIO 17 | GPIO 17 / physical pin 22 | Audible alert output |
| --- |
Wiring Diagram

MQ-5 AO -> GPIO 26
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 16 -> 220 ohm resistor -> LED anode
GPIO 17 -> buzzer positive
Common GND -> MQ-5, LED, buzzer, and BME280
BME280 VCC -> 3.3V
BME280 GND -> GND
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the MQ-5 analog output to GPIO 26 through a safe ADC path.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Wire the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to ground.
- Connect the buzzer positive pin to GPIO 17 and its negative pin to ground.
- Warm up the MQ-5 and observe stable baseline readings before testing change detection.
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.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Test the gas and BME280 readings separately so the early-warning logic starts from trusted inputs.
- Test the output device: Test the LED and buzzer separately before running the warning logic.
- Test the decision logic: Change gas or temperature gradually and then sharply to compare WATCH and WARNING behavior.
- Run the full system: Run the full warning system and compare the printed rate-of-change values with the output state.
- Validate the prototype: Discuss whether the rules would still react in time for a real early-warning application.
- 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
GAS_PIN = 26
BME_SDA_PIN = 20
BME_SCL_PIN = 21
LED_PIN = 16
BUZZER_PIN = 17
mq5 = ADC(GAS_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
led = Pin(LED_PIN, Pin.OUT)
buzzer = Pin(BUZZER_PIN, Pin.OUT)
prev_gas = None
prev_temp = None
print('Smart environment early warning AI 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 OSError:
temperature = 0
gas_change = 0 if prev_gas is None else gas_value - prev_gas
temp_change = 0 if prev_temp is None else temperature - prev_temp
if gas_value >= 32000 or temperature >= 36 or gas_change >= 5000 or temp_change >= 4:
level = 'EMERGENCY'
elif gas_value >= 24000 or temperature >= 32 or gas_change >= 2500 or temp_change >= 2:
level = 'WARNING'
elif gas_value >= 18000 or temperature >= 29 or gas_change >= 1000 or temp_change >= 1:
level = 'WATCH'
else:
level = 'NORMAL'
led.value(1 if level != 'NORMAL' else 0)
buzzer.value(1 if level in ('WARNING', 'EMERGENCY') else 0)
print('Gas:{} dG:{} Temp:{}C dT:{} Level:{}'.format(gas_value, gas_change, temperature, temp_change, level))
prev_gas = gas_value
prev_temp = temperature
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Rate-of-change check | Compares each new reading with the previous reading. | Early warning depends on noticing fast change, not just high absolute values. | Tune the change thresholds after observing real baseline behavior. |
| Alert escalation | Maps fast change or high readings into WATCH, WARNING, or EMERGENCY. | Escalation helps the system respond earlier than a single hard limit. | Adjust absolute and change thresholds separately so their roles stay clear. |
| LED and buzzer outputs | Uses the LED for all alerts and the buzzer for stronger alerts. | Different output intensity helps users distinguish warning levels. | If the buzzer is too disruptive, test the LED logic first. |
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 system is always in warning | The change thresholds are too low or the MQ-5 is still warming up | Allow more warm-up time and increase the change thresholds after observing stable readings. |
| The buzzer never sounds | The buzzer wiring is wrong or the level never reaches warning | Test the buzzer directly and create a clearer warning condition. |
| Temperature stays at zero | The BME280 read failed | Test the BME280 separately and shorten the I2C wires if needed. |
| Warnings feel too slow | The rate thresholds are too high for your test setup | Create repeatable test changes and lower the rate thresholds carefully. |







