Project 3.9.28: Smart Environmental Forecasting System
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico watches rain-sensor and climate trends, then labels the near-term environmental condition as STABLE, WATCH, or HIGH RISK.
Early warning is more useful when a system looks at trends rather than only the current sensor value.
A Pico 2 W prototype with a rain sensor, BME280, and three status LEDs for trend-based local forecasting.
History-based interpretation, honest forecasting language, and local environmental status signaling.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Rain sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LED |
![]() Resistor |
![]() BME280 |
|
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Rain sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Analog wetness 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 |
| Green LED anode | 220 ohm resistor to GPIO 16 | GPIO 16 / physical pin 21 | STABLE |
| Yellow LED anode | 220 ohm resistor to GPIO 17 | GPIO 17 / physical pin 22 | WATCH |
| Red LED anode | 220 ohm resistor to GPIO 18 | GPIO 18 / physical pin 24 | HIGH RISK |
| --- |
Wiring Diagram

Rain sensor AO -> GPIO 26
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 16 -> green LED (STABLE)
GPIO 17 -> yellow LED (WATCH)
GPIO 18 -> red LED (HIGH RISK)
Common GND -> sensors and LEDs
BME280 VCC -> 3.3V, GND -> GND
Step-by-Step Assembly
- Connect the rain sensor analog output to GPIO 26 and keep the control board dry.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Connect the green, yellow, and red LEDs to GPIO 16, GPIO 17, and GPIO 18 through 220 ohm resistors.
- Prepare a safe way to dampen the rain plate during testing without splashing 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.
- 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 the rain sensor and BME280 separately so you understand the dry baseline and the response to moisture or humidity changes.
- Test the output device: Test each LED output before relying on the status display.
- Test the decision logic: Change the rain plate and humidity gradually and confirm the status moves from STABLE to WATCH to HIGH RISK.
- Run the full system: Run the full system long enough to fill the history window and compare the latest reading with the forecast label.
- Validate the prototype: Try different sampling intervals and note how faster or slower sampling changes the trend detection.
- 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
RAIN_PIN = 26
BME_SDA_PIN = 20
BME_SCL_PIN = 21
GREEN_PIN = 16
YELLOW_PIN = 17
RED_PIN = 18
HISTORY_LENGTH = 6
WET_THRESHOLD = 28000
HUMIDITY_WATCH = 75
HUMIDITY_HIGH = 85
rain = ADC(RAIN_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
green = Pin(GREEN_PIN, Pin.OUT)
yellow = Pin(YELLOW_PIN, Pin.OUT)
red = Pin(RED_PIN, Pin.OUT)
history = []
def rising(values, minimum_change):
if len(values) < 2:
return False
return values[-1] - values[0] >= minimum_change
print('Environmental forecasting prototype ready')
while True:
rain_value = rain.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
history.append((rain_value, humidity))
history = history[-HISTORY_LENGTH:]
rain_history = [item[0] for item in history]
humidity_history = [item[1] for item in history]
if rain_value >= WET_THRESHOLD and humidity >= HUMIDITY_HIGH:
status = 'HIGH RISK'
elif rising(rain_history, 4000) or rising(humidity_history, 8) or humidity >= HUMIDITY_WATCH:
status = 'WATCH'
else:
status = 'STABLE'
green.value(1 if status == 'STABLE' else 0)
yellow.value(1 if status == 'WATCH' else 0)
red.value(1 if status == 'HIGH RISK' else 0)
print('Rain:{} Temp:{}C Hum:{}% Status:{} History:{}'.format(
rain_value,
temperature,
humidity,
status,
len(history),
))
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| History storage | Keeps a short rolling window of rain and humidity readings | Forecasting requires memory of recent conditions, not just one moment | Adjust HISTORY_LENGTH after testing how fast your environment changes |
| rising | Checks whether the newest reading is meaningfully higher than the oldest reading in the window | This is the trend detector behind the forecast | Tune the minimum change values after watching real data |
| Status logic | Maps current and rising conditions to STABLE, WATCH, or HIGH RISK | The forecast remains honest because it is based on visible rules rather than hidden claims | If the system warns too early or too late, tune the thresholds here |
| LED outputs | Shows the current forecast state with color-coded indicators | Status LEDs make the trend easy to understand during field tests | Keep the same color meaning if you extend the system later |
Expected Result
The green LED stays on during stable dry conditions.
The yellow LED turns on when humidity or rain wetness trends upward into a watch condition.
The red LED turns on when the rain surface is already wet and humidity is high enough to justify a high-risk warning.
Validation Checks
- Normal condition test: keep the rain plate dry and confirm the system remains in STABLE
- Warning condition test: dampen the rain plate slightly or raise humidity and confirm the system enters WATCH after the history window begins to rise
- Critical condition test: wet the rain sensor clearly and confirm that high humidity drives the system to HIGH RISK
- Sensor reading validation: compare BME280 humidity with a known reference and note how the rain sensor changes from dry to wet
- Calibration test: collect several dry and wet readings so WET_THRESHOLD matches your actual module
- Limitation test: explain why short local trends cannot replace real weather forecasting data
Deployment and Limitations
- This prototype is useful for classroom early-warning demonstrations and short local field trials
- Before deployment, the sensor housing, environmental durability, and calibration process need improvement
- Real forecasting use would also need more data sources, longer histories, and tested site-specific thresholds
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Status never leaves stable | The wet threshold is too high or the history window is too short | Print the rain values during a wet test and reduce WET_THRESHOLD or increase HISTORY_LENGTH |
| Status stays on watch forever | Humidity remains above the watch threshold or the rain sensor is contaminated | Dry the sensor plate, inspect for residue, and confirm the baseline humidity |
| Red LED never turns on | The high-risk conditions are never both met | Test the wet plate and humidity conditions separately and confirm the logic requirements |
| Humidity reads zero | The BME280 read failed | Retest the climate sensor wiring before changing the forecast logic |






