Project 2.1.20: Rain Aware Irregation
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project adds rain detection to an automatic irrigation controller so watering can be skipped when the environment is already wet.
Students will combine an analog soil sensor, a digital rain sensor, a relay-controlled pump, and a visible rain-status LED.
The final system should show when rain is detected, skip irrigation during rain events, and water only when the soil is dry and the rain sensor is clear.
Required Components
![]() Soil moisture sensor |
![]() External pump power supply |
![]() Raspberry Pi Pico 2 W |
![]() Rain sensor module |
![]() 1-channel relay module |
![]() Small DC water pump |
![]() LED and 220 Ω resistor |
![]() Breadboard, jumper wires, tubing, and water container |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC input |
| Rain sensor DOUT | GPIO 27 | GPIO 27 / physical pin 32 | Digital rain-detection input |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Pump control |
| Rain LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Lights when rain is detected |
| Rain LED cathode | GND | Any GND pin | LED return |
| Pump supply path | Relay COM to NO contacts | Not a Pico GPIO | External power only |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ GPIO 27 ───────────┤──── Rain Sensor DOUT
│ GPIO 15 ───────────┤──── Relay IN
│ GPIO 16 ────220Ω───┤──── Rain LED (+)
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ 3.3V ───────────┤──── Rain Sensor VCC
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Rain Sensor GND
│ GND ───────────┤──── Rain LED (-)
│ GND ───────────┤──── Relay GND
│ │
└─────────────────────┘
Relay Module External Supply
┌──────────┐ ┌────────────┐
│ COM ────┼────────────┤ (+) │
│ NO ────┼──┐ │ │
│ VCC ────┼──┼─────────┤ (+) (if │
│ GND ────┼──┼──┐ │ needed) │
└──────────┘ │ │ │ │
│ │ └────────────┘
│ │ │
│ └───────────┘
│ Pump (+)
│
└────────────── Pump (-) ──── External Supply (-)
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: Position the Soil Moisture Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Identify VCC, GND, and AOUT / AO / Signal on the soil sensor.
Step 3: Connect the Soil Sensor
Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26 (ADC0).
Step 4: Position the Rain Sensor
Place the rain sensor area where test drops can reach it without dripping onto the Pico or relay wiring. Identify VCC, GND, and DOUT before wiring.
Step 5: Connect the Rain Sensor
Connect rain sensor VCC to 3.3V when the module supports 3.3V operation. Connect rain sensor GND to GND. Connect rain sensor DOUT to GPIO 27.
Step 6: Place and Wire the Relay Module
Connect relay IN to GPIO 15. Power the relay module from the supply required by the module label. Connect relay GND to Pico GND and to the external pump supply GND where shared grounding is required.
Step 7: Place the Rain LED
Place the rain LED on the breadboard. Connect its long leg through a 220Ω resistor to GPIO 16. Connect its short leg to GND.
Step 8: Wire the Pump Through the Relay
Connect the external pump supply positive wire to relay COM. Connect relay NO to the pump positive lead. Connect the pump negative lead to the external pump supply negative wire.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Rain sensor DOUT connects to GPIO 27
- [x] Rain sensor output is confirmed 3.3V-safe before connecting to the Pico
- [x] Relay IN connects to GPIO 15
- [x] Rain LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Rain LED short leg connects to GND
- [x] Pump positive path uses relay COM and NO
- [x] Pump negative connects to external pump supply negative
- [x] No loose jumper wires
Intermediate Note The rain input uses a pull-up in the code, so many modules read LOW when rain is detected. Test the rain sensor dry and with a few drops before relying on the irrigation skip logic.
Safety Note Keep water drops away from the Pico, breadboard, relay wiring, USB cable, and jumper wires. Do not power the pump from the Pico, and never let the pump run dry.
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 soil and rain sensor inputs first, then add the relay and LED
- Keep the rain sensor test area separate from the relay and Pico board area
Test the Input Sensors
- Record dry and wet soil readings and verify the moisture percentage calculation
- Touch the rain sensor with a damp finger or a few water drops and confirm that the rain input changes state
Test the Output Device
- Check that the LED turns on only when rain is detected
- Test the relay safely before connecting the real pump
Run the Full System
- Upload the final code and confirm that dry soil alone can request watering
- Then trigger the rain sensor and confirm that irrigation is skipped even when the soil is dry
Save the Project
- Save the final code and record the threshold values that worked in your setup
- Note whether your rain sensor is active-low so you can reuse that information in later projects
Additional Testing and Calibration Checks
Calibration and tuning notes
- Calibrate the soil sensor first, then test rain detection separately
- Use only a small amount of water to trigger the rain sensor during bench testing
- If watering is skipped unexpectedly, inspect the rain-sensor state and confirm whether the module is active-low
- If watering still happens during rain, verify the digital input pin and the rain override branch in the code
Quick testing checklist
- [ ] Soil sensor readings change correctly between dry and wet conditions
- [ ] Rain LED turns on when the rain sensor is wet
- [ ] Relay switches correctly before live pump testing
- [ ] Pump waters when soil is dry and rain is not detected
- [ ] Pump does not water when rain is detected
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 ADC, Pin
import time
RELAY_ON = 0
RELAY_OFF = 1
PUMP_RUN_S = 5
MIN_REST_S = 60
soil = ADC(26)
rain = Pin(27, Pin.IN, Pin.PULL_UP)
relay = Pin(15, Pin.OUT)
rain_led = Pin(16, Pin.OUT)
dry_reading = 52000
wet_reading = 22000
dry_threshold = 35
recovery_threshold = 45
ready_to_water = True
last_cycle = time.time() - MIN_REST_S
relay.value(RELAY_OFF)
rain_led.value(0)
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent(raw_value):
span = dry_reading - wet_reading
if span == 0:
return 0
percent = int(((dry_reading - raw_value) * 100) / span)
return clamp(percent, 0, 100)
def raining_now():
return rain.value() == 0
def water_cycle():
relay.value(RELAY_ON)
print("Pump ON: soil dry and no rain detected")
time.sleep(PUMP_RUN_S)
relay.value(RELAY_OFF)
print("Pump OFF: timed cycle complete")
print("Rain-aware irrigation ready")
while True:
now = time.time()
moisture = moisture_percent(soil.read_u16())
rain_detected = raining_now()
rain_led.value(1 if rain_detected else 0)
if moisture >= recovery_threshold:
ready_to_water = True
if rain_detected:
print("Moisture:{}% Rain:YES -> irrigation skipped".format(moisture))
elif ready_to_water and moisture <= dry_threshold and (now - last_cycle) >= MIN_REST_S:
water_cycle()
last_cycle = time.time()
ready_to_water = False
else:
print("Moisture:{}% Rain:NO Ready:{}".format(moisture, ready_to_water))
time.sleep(5)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
raining_now() |
Reads the rain-sensor digital state | The irrigation decision depends on this override input |
dry_threshold and recovery_threshold |
Define when the soil is dry enough to water and when it has recovered | Threshold spacing helps prevent repeated cycles |
rain_led |
Shows the detected rain state locally | Students can debug the rain sensor even without reading serial output |
water_cycle() with MIN_REST_S |
Runs short watering bursts with a rest gap | Protects the pump and avoids overwatering |
Expected Result
When the rain sensor is dry, the LED should stay off and the controller should water only when the soil moisture drops below the dry threshold. When the rain sensor is wet, the LED should turn on and irrigation should be skipped even if the soil is dry.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The LED is always on | The rain sensor output logic is inverted or the sensor is wet | Dry the sensor and confirm whether the module uses active-low output |
| The pump waters during rain | The digital input is wired incorrectly or the rain logic branch is wrong | Recheck GPIO 27 wiring and print the live rain state during testing |
| The rain sensor never changes state | The sensor is powered incorrectly or the wrong pin is connected | Power the sensor from 3.3V, confirm GND, and test the DOUT pin directly |
| The pump never waters even on dry soil | Rain is being detected all the time or the dry threshold is too low | Inspect the rain input, recalibrate the soil sensor, and compare the moisture percentage with the dry threshold |







