Project 2.6.19: Smart Irrigation Rain Integration
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds an irrigation controller that listens to both soil moisture and a rain input before starting a watering cycle.
Students will combine soil sensing, rain detection, relay control, and a rain lockout timer into one practical decision-logic system.
The final system should detect rain, block watering during the rain period and for a lockout time afterwards, then allow irrigation again only when the soil is still dry.
Required Components
![]() Soil moisture sensor (analog output) |
![]() Blue LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() Rain sensor module (digital output) |
![]() Relay module or transistor relay driver |
![]() Green LED and 220 Ω resistor |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | 3.3V | Physical pin 36 | Sensor power |
| Soil sensor GND | GND | Any GND pin | Common ground |
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | Analog soil input |
| Rain sensor VCC | 3.3V | Physical pin 36 | Use a 3.3V-compatible module |
| Rain sensor GND | GND | Any GND pin | Common ground |
| Rain sensor DOUT | GPIO 4 | GPIO 4 / physical pin 6 | Digital rain input |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Irrigation relay control |
| Ready LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Ready-to-water indicator |
| Rain LED anode | GPIO 17 through 220 Ω resistor | GPIO 17 / physical pin 22 | Rain lockout indicator |
| LED cathodes | GND | Any GND pin | Return path |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ GPIO 4 ───────────┤──── Rain Sensor DOUT
│ │
│ GPIO 15 ───────────┤──── Relay IN
│ GPIO 16 ────220Ω───┤──── Ready LED (+)
│ GPIO 17 ────220Ω───┤──── Rain LED (+)
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ 3.3V ───────────┤──── Rain Sensor VCC
│ │
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Rain Sensor GND
│ GND ───────────┤──── Ready LED (-)
│ GND ───────────┤──── Rain LED (-)
│ GND ───────────┤──── Relay GND
│ │
└─────────────────────┘
Relay Module External Supply
┌──────────┐ ┌────────────┐
│ COM ────┼────────────┤ (+) │
│ NO ────┼──┐ │ │
└──────────┘ │ └────────────┘
│ │
│ 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 and Connect the Soil Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26.
Step 3: Position and Connect the Rain Sensor
Place the rain sensor where it can detect water without dripping onto the Pico or breadboard. Connect rain sensor VCC to 3.3V. Connect rain sensor GND to GND. Connect rain sensor DOUT to GPIO 4.
Step 4: Place and Wire the Relay Module
Identify relay VCC, GND, IN, COM, NO, and NC before wiring. Connect relay IN to GPIO 15. Power the relay module according to its label. Connect relay GND to Pico GND and to the external pump or valve supply GND where shared grounding is required.
Step 5: Place and Connect the Ready LED
Place the ready LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the ready LED long leg through a 220Ω resistor to GPIO 16. Connect the ready LED short leg to GND.
Step 6: Place and Connect the Rain LED
Place the rain-lockout LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the rain-lockout LED long leg through a 220Ω resistor to GPIO 17. Connect the rain-lockout LED short leg to GND.
Step 7: Keep the Irrigation Load on the Relay Side
If you connect a pump or valve, route the external supply positive wire through relay COM and NO. Connect the pump or valve negative lead to the external 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 4
- [x] Relay IN connects to GPIO 15
- [x] Ready LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Rain-lockout LED long leg connects through a 220Ω resistor to GPIO 17
- [x] Both LED short legs connect to GND
- [x] Pump or valve positive path uses relay COM and NO if a real load is connected
- [x] No loose jumper wires
Intermediate Note The rain sensor uses an internal pull-up and reads LOW when rain is detected. Test the rain sensor dry and with a few drops before trusting the lockout timer.
Safety Note Keep water away from the Pico, breadboard, USB cable, and jumper wires. Use external load power for any pump or valve, and never let a 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 sensor and LED wiring first, then add the relay control wire
- Keep the rain sensor physically separated from the Pico to reduce water risk
Test the Input Sensor
- Record dry and wet soil values to calibrate the soil percentage properly
- Trigger the rain sensor with a small amount of water and confirm the serial monitor reports rain detected
Test the Output Device
- Run a relay click test with no pump connected and confirm the LEDs show READY or RAIN LOCKOUT correctly
- Check that the relay stays off whenever the rain sensor is active
Test Communication
- Watch the serial monitor and confirm it explains whether irrigation was skipped because of current rain or the post-rain lockout timer
- Use the printed remaining lockout time to verify the decision logic
Run the Full System
- Test dry soil with no rain and confirm the system runs one watering cycle
- Then activate the rain sensor and confirm watering is blocked until the lockout period expires
Save the Project
- Save the final code and record the dry threshold and rain lockout period used in your test
- Write down whether the lockout period should be longer for real outdoor use
Additional Testing and Calibration Checks
- Dry/wet reading test: calibrate the soil sensor before trusting the irrigation threshold
- Normal condition test: confirm the controller waits in READY state when the soil is not dry enough
- Threshold condition test: use a dry sample with no rain and confirm the relay starts one watering cycle
- Rain sensor test: touch or wet the rain sensor and confirm the lockout starts immediately
- Relay click test: perform the first relay test with no pump connected
- No-water dry-run warning: never leave a real pump running dry during repeated testing
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
soil_sensor = ADC(26)
rain_sensor = Pin(4, Pin.IN, Pin.PULL_UP)
relay = Pin(15, Pin.OUT)
ready_led = Pin(16, Pin.OUT)
rain_led = Pin(17, Pin.OUT)
RELAY_ON = 1
RELAY_OFF = 0
SOIL_DRY = 52000
SOIL_WET = 22000
DRY_THRESHOLD = 35
RECOVERY_THRESHOLD = 50
WATER_SECONDS = 4
RAIN_LOCKOUT_SECONDS = 300
CHECK_INTERVAL = 20
last_rain_time = time.time() - RAIN_LOCKOUT_SECONDS
last_watered = time.time() - CHECK_INTERVAL
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def soil_percent():
raw = soil_sensor.read_u16()
span = SOIL_DRY - SOIL_WET
if span <= 0:
return 0
percent = int(((SOIL_DRY - raw) * 100) / span)
return clamp(percent, 0, 100)
def is_raining():
return rain_sensor.value() == 0
def relay_on():
relay.value(RELAY_ON)
def relay_off():
relay.value(RELAY_OFF)
def water_once(reason):
global last_watered
relay_on()
print(reason)
time.sleep(WATER_SECONDS)
relay_off()
last_watered = time.time()
relay_off()
print('=== Smart Irrigation Rain Integration ===')
print('The controller skips irrigation while rain lockout is active.\n')
while True:
moisture = soil_percent()
now = time.time()
if is_raining():
last_rain_time = now
rain_led.value(1)
ready_led.value(0)
relay_off()
print('Rain detected. Irrigation skipped immediately.')
elif (now - last_rain_time) < RAIN_LOCKOUT_SECONDS:
remaining = RAIN_LOCKOUT_SECONDS - int(now - last_rain_time)
rain_led.value(1)
ready_led.value(0)
relay_off()
print('Rain lockout active for {} more second(s).'.format(remaining))
else:
rain_led.value(0)
ready_led.value(1)
if moisture <= DRY_THRESHOLD and (now - last_watered) >= CHECK_INTERVAL:
water_once('Dry soil with no rain lockout. Watering started at {}%.'.format(moisture))
elif moisture >= RECOVERY_THRESHOLD:
relay_off()
print('Soil recovered above the recovery threshold. No watering needed.')
else:
print('Ready state | Soil: {}% | Waiting for a drier condition.'.format(moisture))
time.sleep(CHECK_INTERVAL)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Rain input | Detects whether water is currently on the rain sensor | The system should never ignore active rain when deciding to irrigate |
| RAIN_LOCKOUT_SECONDS | Delays irrigation for a period after the rain event | This gives natural moisture time to reach the soil and avoids wasting water |
| Dry and recovery thresholds | Define when watering should begin and when the soil is considered recovered | These thresholds keep the logic stable |
| Status LEDs | Show whether the controller is ready to irrigate or blocked by rain lockout | Students can understand the logic without reading the code constantly |
Expected Result
When the rain sensor is active, the rain-lockout LED should turn on and the relay should stay off.
After rain stops, the controller should continue to block watering until the lockout time expires.
When the lockout ends and the soil is still dry, the relay should run one timed watering cycle and the serial monitor should explain why.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The system never leaves rain lockout | The rain sensor is always active or too sensitive | Adjust the sensor module threshold and confirm the DOUT signal changes between dry and wet states |
| The relay waters even while it is raining | The rain sensor logic is reversed | Check whether your module outputs LOW or HIGH when wet and adjust is_raining() if needed |
| The moisture percentage does not match the soil condition | The soil calibration values are incorrect | Re-measure SOIL_DRY and SOIL_WET using your actual sensor and soil |
| The lockout is too short for real outdoor use | The demo value is still set for classroom testing | Increase RAIN_LOCKOUT_SECONDS to match your real irrigation environment |





