Project 2.1.21: Rain Prediction Alert System
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project uses local humidity and temperature trends to create a simple rule-based rain alert heuristic.
Students will collect several BME280 readings over time, compare older and newer samples, and generate CLEAR, WATCH, or RAIN LIKELY states.
The final system should explain why it thinks rain is becoming more likely instead of simply flashing an unexplained alert.
Required Components
![]() BME280 sensor module |
![]() Raspberry Pi Pico 2 W |
![]() LED and 220 Ω resistor |
![]() Buzzer |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| BME280 sensor module | Requires bme280.py on the Pico |
||
| Alert LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Forecast indicator |
| Buzzer positive | GPIO 17 / physical pin 22 | Rain-likely alarm | |
| BME280 VCC | 3.3V | Physical pin 36 | Sensor power |
| BME280 GND | GND | Any GND pin | Common ground |
| BME280 SDA | GPIO 2 | GPIO 2 / physical pin 4 | I2C data |
| BME280 SCL | GPIO 3 | GPIO 3 / physical pin 5 | I2C clock |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 2 ───────────┤──── BME280 SDA
│ GPIO 3 ───────────┤──── BME280 SCL
│ │
│ GPIO 16 ────220Ω───┤──── Alert LED (+)
│ │
│ GPIO 17 ───────────┤──── Buzzer (+)
│ │
│ 3.3V ───────────┤──── BME280 VCC
│ │
│ GND ───────────┤──── BME280 GND
│ GND ───────────┤──── Alert LED (-)
│ GND ───────────┤──── Buzzer (-)
│ │
└─────────────────────┘
Step-by-Step Assembly
Step 1: Place the Raspberry Pi Pico 2W
Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.
Step 2: Place the BME280 Sensor Module
Place the BME280 module where it can sense the surrounding air. Identify VCC, GND, and DATA before wiring.
Step 3: Connect the BME280 Sensor Module
Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3. If you are using a bare BME280 sensor, use the module pins labeled VCC, GND, SDA, and SCL.
Step 4: Place and Connect the Alert LED
Place the alert LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the alert LED long leg through a 220Ω resistor to GPIO 16. Connect the alert LED short leg to GND.
Step 5: Place and Connect the Buzzer
Place the buzzer on the breadboard and identify its positive (+) and negative (-) pins. Connect the buzzer positive pin to GPIO 17. Connect the buzzer negative pin to GND.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
- [x] Alert LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Alert LED short leg connects to GND
- [x] Buzzer positive pin connects to GPIO 17
- [x] Buzzer negative pin connects to GND
- [x] No loose jumper wires
Intermediate Note The BME280 needs a short delay between readings. This project is a learning forecast demo, so treat the rain-likely state as a pattern indicator, not a real weather guarantee.
Testing Individual Components
Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.
Hardware Setup
- Build the BME280, LED, and buzzer wiring first
- Keep the BME280 away from immediate heat or airflow disturbances that would distort the trend
Test the Input Sensor
- Run a short BME280 test and confirm that humidity and temperature values update reliably
- Wait for several readings because this project depends on trends, not just one value
Test the Output Device
- Test the LED and buzzer separately
- Confirm that the LED can stay on for WATCH while the buzzer is reserved for RAIN LIKELY
Run the Full System
- Collect enough samples for the state to move beyond COLLECTING
- Create a controlled rise in humidity or a small temperature fall and observe how the forecast changes
Save the Project
- Save the final code and record the trend thresholds that worked during testing
- Write down whether the WATCH and RAIN LIKELY states felt meaningfully different
Additional Testing and Calibration Checks
Calibration and tuning notes
- Remember that this is a simple rule-based prediction heuristic, not a real weather forecast service
- You need multiple readings before the system can move beyond the COLLECTING state
- If the forecast never changes, adjust the humidity or temperature trend thresholds after observing real readings
- If the buzzer alerts too often, increase the alert repeat interval
Quick testing checklist
- [ ] BME280 humidity and temperature readings appear correctly
- [ ] The system moves from COLLECTING to a forecast state after enough samples
- [ ] WATCH and RAIN LIKELY are visually distinguishable
- [ ] The buzzer sounds only for the stronger alert level
- [ ] The reasons list explains why the state changed
Full Project Code
After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below 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 bme280
import time
i2c_bme = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
bme = bme280.BME280(i2c=i2c_bme)
def number_from_bme(text_value):
cleaned = "".join(ch for ch in str(text_value) if ch.isdigit() or ch in ".-")
return float(cleaned)
def read_bme280():
temp_text, pressure_text, humidity_text = bme.values
return number_from_bme(temp_text), number_from_bme(humidity_text), number_from_bme(pressure_text)
alert_led = Pin(16, Pin.OUT)
buzzer = Pin(17, Pin.OUT)
humidity_history = []
temperature_history = []
history_size = 8
alert_repeat_s = 30
last_alert = time.time() - alert_repeat_s
def alert_burst():
for _ in range(2):
alert_led.on()
buzzer.on()
time.sleep(0.15)
alert_led.off()
buzzer.off()
time.sleep(0.15)
def forecast_state(humidity_values, temperature_values):
if len(humidity_values) < 6 or len(temperature_values) < 6:
return "COLLECTING", []
recent_h = sum(humidity_values[-3:]) / 3
older_h = sum(humidity_values[-6:-3]) / 3
recent_t = sum(temperature_values[-3:]) / 3
older_t = sum(temperature_values[-6:-3]) / 3
reasons = []
if recent_h - older_h >= 6:
reasons.append("humidity rising quickly")
if older_t - recent_t >= 1.5:
reasons.append("temperature falling")
if len(reasons) >= 2:
return "RAIN LIKELY", reasons
if len(reasons) == 1:
return "WATCH", reasons
return "CLEAR", ["no strong rain pattern"]
print("Rain prediction alert system ready")
time.sleep(2)
while True:
try:
temp_c, humidity, pressure_hpa = read_bme280()
except Exception as error:
print("Sensor error:", error)
time.sleep(2)
continue
humidity_history.append(humidity)
temperature_history.append(temp_c)
if len(humidity_history) > history_size:
humidity_history.pop(0)
temperature_history.pop(0)
state, reasons = forecast_state(humidity_history, temperature_history)
print("Humidity:{}% Temp:{}C Forecast:{} Reasons:{}".format(
humidity,
temp_c,
state,
", ".join(reasons),
))
if state == "RAIN LIKELY":
alert_led.on()
if time.time() - last_alert >= alert_repeat_s:
alert_burst()
last_alert = time.time()
elif state == "WATCH":
alert_led.on()
else:
alert_led.off()
buzzer.off()
time.sleep(10)
How the Code Works
| Code Section | 1 | Why It Matters |
|---|---|---|
| History buffers | 1 | Trend logic needs multiple readings to compare older and newer conditions |
forecast_state() |
1 | Students learn that forecasts can be built from weighted rules |
| Reasons list | 1 | A useful alert system should show its reasoning |
| Alert separation | 1 | Graduated alert levels are more practical than one single alarm state |
Expected Result
After enough samples are collected, the system should print a forecast state and the reasons behind it. The LED should show heightened concern during WATCH and RAIN LIKELY states, while the buzzer should sound only when the stronger RAIN LIKELY condition is reached.
Troubleshooting
| Problem | 1 | Solution |
|---|---|---|
| The state never leaves COLLECTING | 1 | Wait for more readings or reduce the number of samples required |
| RAIN LIKELY appears too easily | 1 | Increase the required humidity rise or temperature drop values |
| The LED is always on | The state is stuck in WATCH or RAIN LIKELY | Print the recent trend values and tune the thresholds |
| The buzzer never sounds | 1 | Check the reasons list and adjust the stronger alert rule if appropriate |




