Skip to content

Project 2.2.4: Automated Irrigation With Weather Input

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


Overview

This project adds a weather-aware rule to irrigation so the system can skip watering when humidity suggests rain or very damp conditions are likely.

Students will combine soil moisture, humidity trend logic, a relay-controlled pump, and a timed watering cycle.

The final system should water only when the soil is dry enough and the weather rule does not suggest that watering should be postponed.

Required Components

Soil moisture sensor
Soil moisture sensor
External power supply
External power supply
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280 sensor module
BME280 sensor module
1-channel relay module
1-channel relay module
Small DC pump
Small DC pump
LED and 220Ω resistor
LED and 220Ω resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC moisture input
BME280 SDA GPIO 2 GPIO 2 / physical pin 4 I2C data
BME280 SCL GPIO 3 GPIO 3 / physical pin 5 I2C clock
BME280 VCC 3.3V Physical pin 36 Sensor power
BME280 GND GND Any GND pin Common ground
Relay IN GPIO 15 GPIO 15 / physical pin 20 Pump control
Status LED anode GPIO 16 through 220Ω resistor GPIO 16 / physical pin 21 Pump activity indicator
Status LED cathode GND Any GND pin LED return

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 26 ───────────┼──── Soil sensor AOUT
│  GPIO 2  ───────────┼──── BME280 SDA
│  GPIO 3  ───────────┼──── BME280 SCL
│  3.3V    ───────────┼──── BME280 VCC / Soil VCC
│  GND     ───────────┼──── BME280 GND / Soil GND / LED cathode
│  GPIO 15 ───────────┼──── Relay IN
│  GPIO 16 ───────────┼──── Resistor (220Ω) ── LED anode (+)
│                     │
└─────────────────────┘

Soil Sensor
VCC  ─────────────── 3.3V
GND  ─────────────── GND
AOUT ─────────────── GPIO 26

BME280
VCC  ─────────────── 3.3V
GND  ─────────────── GND
SDA  ─────────────── GPIO 2
SCL  ─────────────── GPIO 3

Relay Module (external pump switching)
IN   ─────────────── GPIO 15
VCC  ─────────────── External supply
GND  ─────────────── Pico GND + External supply GND
COM  ─────────────── External pump supply positive
NO   ─────────────── Pump positive lead

Pump
Positive ────────── Relay NO
Negative ────────── External supply negative

Status LED
Anode (+) ──[220Ω]── GPIO 16
Cathode (-) ───────── GND

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 before wiring.

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: Place the BME280 Sensor Module

Identify BME280 VCC, GND, SDA, and SCL before wiring. 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 5: 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 from the supply required by its label. Connect relay GND to Pico GND and to the external pump supply GND where shared grounding is required.

Step 6: Place and Connect the Status LED

Place the status LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the status LED long leg through a 220Ω resistor to GPIO 16. Connect the status LED short leg to GND.

Step 7: 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] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
  • [x] Relay IN connects to GPIO 15
  • [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Status 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

Calibrate the soil sensor and confirm the BME280 humidity reading is stable. The weather rule can skip watering when humidity is high or trending toward rain.

Safety Note

Do not power the pump from the Pico. Use an external pump supply, keep electronics away from water, 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 sensor, BME280, LED, and relay stage before any live pump test
  • Keep the weather-sensing and pump hardware both visible during early debugging

Test the input sensor

  • Calibrate the soil sensor and confirm the BME280 humidity reading is stable
  • Watch several humidity readings because the weather rule depends on trend, not only one value

Test the output device

  • Test the relay and status LED before using the live pump
  • Confirm that the pump cycle is short and predictable

Run the full system

  • Create a dry-soil condition and verify that watering happens when humidity is normal
  • Then raise humidity or create a rising-humidity trend and confirm that the system skips watering even if the soil is dry

Save the project

  • Save the final code and record the humidity skip threshold and trend rule that worked best
  • Write down one case where the weather input prevented watering

Quick testing checklist

  • ☐ Soil moisture and humidity readings appear correctly
  • ☐ The pump runs when soil is dry and humidity conditions are normal
  • ☐ The system skips watering during high or rising humidity conditions
  • ☐ The status LED matches the pump state
  • ☐ The Shell clearly reports whether watering was allowed or skipped

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, I2C, Pin
import bme280
import time

RELAY_ON = 0
RELAY_OFF = 1

PUMP_RUN_S = 5
MIN_REST_S = 60

soil = ADC(26)
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)

relay = Pin(15, Pin.OUT)
status_led = Pin(16, Pin.OUT)

soil_dry = 52000
soil_wet = 22000

dry_threshold = 35
recovery_threshold = 45

humidity_skip = 85
humidity_history = []
history_size = 6

last_cycle = time.time() - MIN_REST_S
ready_to_water = True

relay.value(RELAY_OFF)
status_led.off()

def clamp(value, low, high):
    if value < low:
        return low
    if value > high:
        return high
    return value

def soil_percent():
    raw = soil.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 humidity_value():
    temp_c, humidity, pressure_hpa = read_bme280()
    return humidity

def rain_watch(values):
    if len(values) < 6:
        return False
    recent = sum(values[-3:]) / 3
    older = sum(values[-6:-3]) / 3
    return (recent - older) >= 5

def run_cycle():
    global last_cycle, ready_to_water
    relay.value(RELAY_ON)
    status_led.on()
    print("Pump ON: soil dry and weather rule allowed watering")
    time.sleep(PUMP_RUN_S)
    relay.value(RELAY_OFF)
    status_led.off()
    print("Pump OFF: timed cycle complete")
    last_cycle = time.time()
    ready_to_water = False

print("Automated irrigation with weather input ready")

while True:
    moisture = soil_percent()
    humidity = humidity_value()

    humidity_history.append(humidity)
    if len(humidity_history) > history_size:
        humidity_history.pop(0)

    if moisture >= recovery_threshold:
        ready_to_water = True

    skip_for_weather = humidity >= humidity_skip or rain_watch(humidity_history)

    if ready_to_water and moisture <= dry_threshold and not skip_for_weather and (time.time() - last_cycle) >= MIN_REST_S:
        run_cycle()
    else:
        print("Moisture:{}% Humidity:{}% WeatherSkip:{}".format(moisture, humidity, skip_for_weather))
    time.sleep(10)

How the Code Works

Code Section What It Does Why It Matters
rain_watch() Looks at a short humidity trend instead of one reading A single humidity value is weaker than a short trend
skip_for_weather Combines current humidity and trend into one decision Weather-aware systems should explain when watering is intentionally skipped
Recovery threshold Prevents repeated watering from one dry event Prevents repeated watering from one dry event
Timed pump cycle Keeps each watering burst short and predictable Short cycles are safer and easier to tune

Expected Result

If the soil is dry and the weather rule does not block irrigation, the pump should run one timed cycle and the LED should light. If humidity is already very high or the humidity trend suggests possible rain, the system should skip irrigation and explain that choice in the Shell.


Troubleshooting

Problem Possible cause Solution
The pump never waters The weather rule is blocking all attempts Print the humidity history and compare it with the skip thresholds.
The system waters even when humidity is high The skip rule or trend logic is too permissive Lower the humidity_skip threshold or inspect the rain_watch() calculation.
The pump cycles too often The rest time or recovery threshold is too low Increase recovery_threshold or MIN_REST_S.
Humidity readings look unstable The BME280 is not seated or the read interval is too short Check the sensor wiring and keep the read interval reasonable.