Project 3.1.17: Multi-sensor Smart City Environmental Monitor
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico combines gas, light, temperature, and humidity readings into one local environmental status display.
Urban environmental monitoring needs more than one reading because air quality, heat stress, and low-light conditions can interact in public spaces.
A Pico 2 W prototype with an MQ-135 sensor, LDR, BME280, and OLED used as a local environmental monitor.
Multi-sensor interpretation, local dashboard design, and honest use of non-certified gas sensing.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() BME280 |
![]() Breadboard |
![]() Jumper wires |
![]() OLED |
![]() LDR |
![]() Gas Sensor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| MQ-135 AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Use a safe 3.3V ADC connection |
| LDR divider midpoint | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Ambient light 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 |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL shared with OLED |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
Wiring Diagram

MQ-135 AO -> GPIO 26 through a safe divider if required
LDR divider midpoint -> GPIO 27
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
BME280 VCC -> 3.3V
BME280 GND -> GND
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> all sensors and OLED
Step-by-Step Assembly
- Connect the MQ-135 analog output to GPIO 26 through a 3.3V-safe signal path.
- Build the LDR divider and connect its midpoint to GPIO 27.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
- Warm up the MQ-135 before calibration and upload ssd1306.py before the display test.
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: Read gas, light, and BME280 values separately so you know the baseline for each subsystem.
- Test the output device: Run a short OLED test before combining the readings into one dashboard.
- Test the decision logic: Trigger one factor at a time and confirm the state changes logically from NORMAL to WARNING or CRITICAL.
- Run the full system: Run the full monitor and compare the raw values with the dashboard label.
- Validate the prototype: Repeat the test at different times of day or in different rooms to see how much the baseline moves.
- 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 bme280
import time
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
GAS_PIN = 26
LIGHT_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
GAS_WARNING = 18000
GAS_CRITICAL = 30000
mq135 = ADC(GAS_PIN)
light = ADC(LIGHT_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
def status_from_score(score, gas_value):
if gas_value >= GAS_CRITICAL or score >= 3:
return 'CRITICAL'
if score >= 1:
return 'WARNING'
return 'NORMAL'
print('Smart city monitor ready')
while True:
gas_value = mq135.read_u16()
light_value = light.read_u16()
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
humidity = float(values[2].replace('%', ''))
except Exception:
temperature = 0
humidity = 0
score = 0
if gas_value >= GAS_WARNING:
score += 1
if temperature >= 32:
score += 1
if humidity >= 80:
score += 1
if light_value <= 8000:
score += 1
status = status_from_score(score, gas_value)
print('Gas:{} Light:{} Temp:{}C Hum:{}% Status:{}'.format(
gas_value,
light_value,
temperature,
humidity,
status,
))
if HAS_OLED:
oled.fill(0)
oled.text('City Monitor', 0, 0)
oled.text('Gas:{}'.format(gas_value), 0, 14)
oled.text('T:{} H:{}%'.format(temperature, humidity), 0, 28)
oled.text('L:{} {}'.format(light_value, 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 |
|---|---|---|---|
| Multi-sensor input layer | Initializes gas, light, and climate sensing for one local node | Public environmental monitoring becomes more meaningful when several factors are read together | Validate each sensor separately before changing the combined logic |
| Environmental score | Adds score points when readings move outside the normal prototype range | A simple score is an honest way to demonstrate multi-factor decision logic without claiming machine learning | Adjust the thresholds after collecting warm-up and baseline data |
| status_from_score | Turns the combined score into NORMAL, WARNING, or CRITICAL | A clear label helps users interpret the dashboard quickly | If your site needs different risk bands, change the score rules here |
| OLED dashboard | Shows raw values and the final local state on the display | The dashboard helps students compare data collection with data interpretation | If the display fails, check the I2C wiring and library before changing the rest of the code |
Expected Result
The serial monitor and OLED update with gas, light, temperature, humidity, and a combined status label.
Raising any one risk factor moves the system toward WARNING, while stronger combined stress or high gas levels move it to CRITICAL.
The monitor behaves as a local node and does not pretend to be a full city dashboard or certified alarm.
Validation Checks
- Normal condition test: allow the MQ-135 to warm up in clean air and confirm the system reaches a stable baseline
- Warning condition test: darken the LDR or gently raise humidity and confirm the status changes to WARNING
- Critical condition test: use a safe classroom gas-source demonstration only if approved and confirm the gas threshold logic changes state as expected
- Sensor reading validation: compare BME280 values with a known thermometer and observe LDR response in bright and dim light
- Calibration test: record MQ-135 baseline readings after warm-up before choosing warning and critical thresholds
- Limitation test: explain why this single local node cannot by itself represent citywide environmental conditions
Deployment and Limitations
- This prototype is suitable for classroom demonstrations and local pilot monitoring at one site
- Before deployment, the gas sensor, enclosure, maintenance schedule, and placement strategy need careful review
- A real smart-city system would need many validated nodes, data networking, and a maintenance plan for calibration drift
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Gas values stay unstable | The MQ-135 is not warmed up or airflow keeps changing | Allow more warm-up time and collect several baseline readings before choosing thresholds |
| Status is always critical | Thresholds are too strict or one sensor path is saturated | Print each raw reading and tune the threshold that is dominating the score |
| 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 |
| Light reading never changes | The LDR divider is wired incorrectly | Measure the midpoint voltage and rebuild the resistor divider if needed |






