Skip to content

Project 3.7.2: Rain Impact Analyzer

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


Overview

The Pico averages rain-sensor readings over time and maps them to an impact or risk category.

Rain intensity is easier to discuss when learners can see a trend and not only a single noisy sensor reading.

A Pico 2 W prototype with an analog rain sensor and OLED display.

Moving averages, threshold interpretation, and early risk framing.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Rain sensor
Rain sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
OLED
OLED

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Rain sensor AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Analog rain input
OLED SDA GPIO 20 GPIO 20 / physical pin 26 I2C0 SDA
OLED SCL GPIO 21 GPIO 21 / physical pin 27 I2C0 SCL

Wiring Diagram

Circuit Diagram

  Rain sensor AO        -> GPIO 26
  GPIO 20               -> OLED SDA
  GPIO 21               -> OLED SCL
  Common GND            -> rain sensor and OLED

Step-by-Step Assembly

  1. Connect the rain sensor analog output to GPIO 26.
  2. Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
  3. Upload ssd1306.py and confirm it is present on the Pico.
  4. Keep the Pico and display dry while exposing only the rain plate to water.

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: Measure dry and wet rain-sensor values separately before enabling the moving average logic.
  3. Test the output device: Test the OLED with a simple display script.
  4. Test the decision logic: Apply different water amounts to the rain plate and confirm the impact label changes sensibly.
  5. Run the full system: Run the full analyzer and compare the raw value with the displayed average.
  6. Validate the prototype: Discuss how wind, dirt, or splash patterns could distort outdoor readings.
  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 ADC, I2C, Pin
import time

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

RAIN_PIN = 26
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
HISTORY = 10
LOW_THRESHOLD = 20000
HIGH_THRESHOLD = 40000

sensor = ADC(RAIN_PIN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
    oled = SSD1306_I2C(128, 64, i2c)
values = []

print('Rain impact analyzer ready')

while True:
    current = sensor.read_u16()
    values.append(current)
    if len(values) > HISTORY:
        values.pop(0)
    average = sum(values) / len(values)

    if average >= HIGH_THRESHOLD:
        label = 'HIGH'
    elif average >= LOW_THRESHOLD:
        label = 'MODERATE'
    else:
        label = 'LOW'

    print('Current:{} Average:{:.0f} Label:{}'.format(current, average, label))

    if HAS_OLED:
        oled.fill(0)
        oled.text('Rain Impact', 0, 0)
        oled.text('Now:{}'.format(current), 0, 18)
        oled.text('Avg:{:.0f}'.format(average), 0, 34)
        oled.text(label, 0, 50)
        oled.show()

    time.sleep(1)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Rolling history Stores recent rain readings for averaging A short history makes the system less sensitive to one noisy reading Adjust the history length if the display changes too quickly or too slowly
Average-based risk Maps the moving average into LOW, MODERATE, or HIGH impact This makes the classification more stable and explainable Tune thresholds only after measuring dry and wet baseline values
OLED output Shows the current reading, average, and impact label together Users can see both the evidence and the decision Verify ssd1306.py and 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 label never changes The thresholds do not match the sensor range Measure real dry and wet values and adjust the thresholds
Average changes too slowly The history window is too large Reduce HISTORY so the system responds faster
OLED is blank The display library or I2C wiring is wrong Confirm ssd1306.py is on the Pico and verify GPIO 20 and GPIO 21
Readings are erratic Water droplets or wiring are unstable Keep the wiring secure and apply water more consistently during tests