Skip to content

Project 3.1.32: Weather Station

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


Overview

The Pico reads temperature, humidity, and pressure from a BME280 sensor and shows the data on a local OLED weather display.

Outdoor or shed conditions are easier to discuss when weather variables are measured together instead of one at a time.

A Pico 2 W prototype with a BME280 sensor, OLED display, and temperature alert LED.

Multi-variable environmental sensing, library setup, and display validation.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280
BME280
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
OLED
OLED

Circuit Connections

Component terminal Connect to Pico physical pin Notes
BME280 SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
BME280 SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL
OLED SDA GPIO 20 GPIO 20 / physical pin 26 Shares I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 Shares I2C0 SCL
LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 High-temperature alert LED

Wiring Diagram

Circuit Diagram

  GPIO 20             -> BME280 SDA and OLED SDA
  GPIO 21             -> BME280 SCL and OLED SCL
  GPIO 16             -> 220 ohm resistor -> LED anode
  Common GND          -> BME280, OLED, and LED

Step-by-Step Assembly

  1. Connect both the BME280 and OLED to the same I2C bus on GPIO 20 and GPIO 21.
  2. Wire the LED to GPIO 16 through a 220 ohm resistor.
  3. Upload bme280.py and ssd1306.py to the Pico and confirm both files are present.
  4. Confirm the BME280 module is a 3.3V-safe version before powering it from the Pico.

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 BME280 sensor and print the values before adding the display logic.
  3. Test the output device: Test the OLED and alert LED separately.
  4. Test the decision logic: Warm the sensor slightly with your hand and confirm the temperature changes while the alert threshold remains sensible.
  5. Run the full system: Run the full station and compare the OLED values with the serial output.
  6. Validate the prototype: Discuss how enclosure design or direct sunlight could distort field measurements.
  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 I2C, Pin
import time

try:
    from bme280 import BME280
    HAS_BME = True
except ImportError:
    HAS_BME = False

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

I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
LED_PIN = 16
TEMP_ALERT = 35

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

print('Weather station ready')

while True:
    if HAS_BME:
        temp, pressure, humidity = sensor.values
        temp_c = float(temp[:-1])
    else:
        temp = '0C'
        pressure = '0hPa'
        humidity = '0%'
        temp_c = 0

    led.value(1 if temp_c >= TEMP_ALERT else 0)
    print('Temp:{} Hum:{} Press:{}'.format(temp, humidity, pressure))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Weather', 0, 0)
        oled.text('T:{}'.format(temp), 0, 18)
        oled.text('H:{}'.format(humidity), 0, 34)
        oled.text('P:{}'.format(pressure), 0, 50)
        oled.show()

    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Library imports Loads the BME280 and OLED libraries if they are present Library setup is a critical part of this station build Use os.listdir() to verify both files before troubleshooting hardware
Weather readings Collects temperature, humidity, and pressure values from the BME280 Multi-variable sensing is what makes this a weather station rather than a single-sensor demo Check the value format used by your chosen BME280 library if parsing differs
OLED station view Displays all three weather values on one screen A local display helps with quick validation and demo use Verify the shared I2C wiring if either the sensor or display fails

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 BME280 values do not appear The library file is missing or the I2C wiring is wrong Confirm bme280.py is on the Pico and verify GPIO 20 and GPIO 21
The OLED is blank The display library is missing or the bus wiring is incorrect Confirm ssd1306.py is present and check the shared I2C bus
Temperature looks unrealistic The sensor is being touched or heated directly Allow the sensor to stabilize in ambient air before trusting the reading
The alert LED never changes The threshold is too high or the LED wiring is wrong Lower TEMP_ALERT temporarily and test GPIO 16 with a blink script