Skip to content

Project 3.1.2: Auto Rain Cover System

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


Overview

The Pico reacts to rain detection by driving a relay-controlled cover motor or demo actuator state.

Outdoor equipment, pathways, or sanitation zones may need a quick cover response when rain starts unexpectedly.

A Pico 2 W prototype with a rain sensor, relay output, and status LEDs.

Rain-triggered actuation, timed hold behavior, and safe relay testing.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Relay module
Relay module
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor
Rain sensor
Rain sensor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Rain sensor OUT GPIO 14 GPIO 14 / physical pin 19 Digital rain input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Cover actuator control
Green LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Waiting or open state
Red LED anode 220 ohm resistor to GPIO 17 GPIO 17 / physical pin 22 Rain-active state

Wiring Diagram

Circuit Diagram

  Rain sensor OUT      -> GPIO 14
  Relay IN             -> GPIO 15
  GPIO 16              -> 220 ohm resistor -> green LED anode
  GPIO 17              -> 220 ohm resistor -> red LED anode
  Common GND           -> rain sensor, relay, and LEDs

Step-by-Step Assembly

  1. Connect the rain sensor output to GPIO 14.
  2. Connect the relay control input to GPIO 15 and leave the real motor disconnected for the first tests.
  3. Wire the green and red LEDs to GPIO 16 and GPIO 17 through 220 ohm resistors.
  4. Keep the rain sensor plate exposed but keep the Pico and relay module dry and elevated.

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 rain sensor input by wetting and drying only the sensor plate.
  3. Test the output device: Test the relay and LEDs separately before connecting any real actuator.
  4. Test the decision logic: Trigger rain and confirm the relay turns on immediately and then stays on for the hold period.
  5. Run the full system: Run the full controller and compare the printed status with the LEDs and relay state.
  6. Validate the prototype: Discuss what mechanical limit switches or feedback would be needed before using a real moving cover.
  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 Pin
import time

RAIN_PIN = 14
RELAY_PIN = 15
GREEN_LED_PIN = 16
RED_LED_PIN = 17
RAIN_ACTIVE_STATE = 0
HOLD_SECONDS = 5

rain_sensor = Pin(RAIN_PIN, Pin.IN, Pin.PULL_UP)
relay = Pin(RELAY_PIN, Pin.OUT, value=1)
green_led = Pin(GREEN_LED_PIN, Pin.OUT)
red_led = Pin(RED_LED_PIN, Pin.OUT)
last_rain_time = -HOLD_SECONDS

print('Auto rain cover ready')

while True:
    raining = rain_sensor.value() == RAIN_ACTIVE_STATE
    now = time.time()
    if raining:
        last_rain_time = now

    cover_active = raining or (now - last_rain_time) < HOLD_SECONDS
    relay.value(0 if cover_active else 1)
    green_led.value(0 if cover_active else 1)
    red_led.value(1 if cover_active else 0)
    print('Raining:{} CoverActive:{}'.format(raining, cover_active))
    time.sleep(0.2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Rain detection Checks whether the rain sensor plate is currently wet This is the trigger that starts the cover action Confirm the active logic of your rain module before trusting the output
Hold timer Keeps the cover active briefly after rain stops A hold period prevents the actuator from switching too quickly during brief drops or splashes Adjust the hold time during testing if the cover releases too soon
Relay and LEDs Uses the relay for actuation and LEDs for clear local state feedback Visible states make the automation easier to validate before any real motor is connected Dry-run the entire sequence with LEDs before attaching a load

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 could be used in school, farm, or sanitation demonstrations with safe low-voltage loads
  • Before deployment, the load wiring, enclosure, and power design need careful review
  • Actuator systems need maintenance planning because relay wear and sensor drift affect reliability

Troubleshooting

Problem Possible Cause Solution
The relay is always active The rain logic is inverted or the sensor plate is still wet Dry the plate fully and confirm the active-state constant
The cover drops out too quickly The hold time is too short Increase HOLD_SECONDS and retest with short wet-dry transitions
The LEDs do not match the state The LED wiring is wrong Test GPIO 16 and GPIO 17 separately with a simple blink script
The rain sensor seems noisy Drops or dirt are causing unstable detection Clean the plate and test with more controlled wetting