Project 3.9.18: Smart Environment Digital Observatory
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico works as a local observation station that tracks environmental readings and their recent averages over time.
Observation is weak when users see only one momentary reading instead of how conditions are changing over several samples.
A Pico 2 W prototype with an MQ-135 gas sensor, BME Sensor, light sensor, and OLED observatory dashboard.
Observation-station design, rolling averages, and longer-view environmental interpretation.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Gas sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LDR |
![]() BME280 |
![]() 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 |
| Light sensor output | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | 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
Light sensor output -> 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
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the MQ-135 analog output to GPIO 26 through a safe ADC path.
- Build the light sensor analog path and connect it 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 and observe several baseline cycles before trusting the observatory status.
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 all sensors separately so the observatory history starts from trusted values.
- Test the output device: Run an OLED hello-world test before adding the history logic.
- Test the decision logic: Change one condition gradually and confirm the averages and status respond over time.
- Run the full system: Run the full observatory and compare current values with recent averages.
- Validate the prototype: Discuss how a longer or shorter history would change the observatory behavior.
- 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
LIGHT_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
HISTORY_LENGTH = 6
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)
gas_history = []
temp_history = []
def average(values):
return sum(values) / len(values) if values else 0
print('Smart environment digital observatory ready')
while True:
gas_value = mq135.read_u16()
light_raw = light.read_u16()
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
humidity = float(values[2].replace('%', ''))
except Exception:
temperature = 0
humidity = 0
gas_history.append(gas_value)
temp_history.append(temperature)
gas_history = gas_history[-HISTORY_LENGTH:]
temp_history = temp_history[-HISTORY_LENGTH:]
avg_gas = average(gas_history)
avg_temp = average(temp_history)
if gas_value > avg_gas + 2500 or temperature > avg_temp + 2:
status = 'DRIFT'
else:
status = 'STABLE'
print('Gas:{} AvgG:{:.0f} Temp:{} AvgT:{:.1f} Light:{} Hum:{} Status:{}'.format(gas_value, avg_gas, temperature, avg_temp, light_raw, humidity, status))
if HAS_OLED:
oled.fill(0)
oled.text('Observatory', 0, 0)
oled.text('G:{} A:{:.0f}'.format(gas_value, avg_gas), 0, 16)
oled.text('T:{} L:{}'.format(temperature, light_raw), 0, 32)
oled.text(status[:16], 0, 50)
oled.show()
time.sleep(3)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Short history storage | Keeps recent observations for comparison. | An observatory should reveal change over time, not only one instant. | Adjust HISTORY_LENGTH to change how quickly the averages react. |
| Average comparison | Checks whether current conditions are pulling away from the recent baseline. | This creates a useful observation status without needing complex analytics. | Tune the drift thresholds after repeated trials. |
| OLED observatory view | Shows the current reading, average, and status on one screen. | The user can compare live values with their recent context. | Verify ssd1306.py and the I2C wiring if the display is blank. |
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 drift | The thresholds are too sensitive or the gas sensor is still warming up | Allow more warm-up time and increase the drift thresholds after observing a stable baseline. |
| Averages never change | The history is not updating or the room conditions are too static | Print the history values and vary the test conditions more clearly. |
| Climate values stay at zero | The BME280 read failed | Test the BME280 separately and check the I2C wiring. |
| OLED is blank | The display library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21. |






