Skip to content

Project 3.2.4: Autonomous Irrigation Controller

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


Overview

The Pico waters a plant bed only when the soil is dry enough and the irrigation tank still has usable water.

An irrigation system is not truly autonomous if it waters blindly without checking whether water is available or whether the soil has already recovered.

A Pico 2 W prototype with a soil moisture sensor, water-level sensor, relay-controlled pump, and status LED.

Autonomous irrigation logic, resource-awareness, hysteresis, 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
Water pump
Water pump
Soil moisture
Soil moisture
Water level sensor
Water level sensor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor AO GPIO 26 ADC0 GPIO 26 / physical pin 31 Soil input
Water-level sensor AO GPIO 27 ADC1 GPIO 27 / physical pin 32 Tank input
Relay IN GPIO 14 GPIO 14 / physical pin 19 Pump relay control
LED anode 220 ohm resistor to GPIO 15 GPIO 15 / physical pin 20 Pump status LED

Wiring Diagram

autonomous irrigation controller

  Soil sensor AO -> GPIO 26
  Water-level sensor AO -> GPIO 27
  GPIO 14 -> relay IN
  GPIO 15 -> status LED via 220 ohm
  Relay load path -> external pump supply and pump

Step-by-Step Assembly

  1. Connect the soil moisture sensor analog output to GPIO 26.
  2. Connect the water-level sensor analog output to GPIO 27 and keep the wet hardware area separated from the Pico.
  3. Connect the relay control input to GPIO 14.
  4. Connect the LED to GPIO 15 through a 220 ohm resistor.
  5. Attach the pump through the relay and external supply only after relay-only testing passes.

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 soil and tank sensors separately so the dry, wet, empty, and full baselines are known.
  3. Test the output device: Toggle the relay and LED separately before connecting the pump.
  4. Test the decision logic: Confirm that irrigation starts only when both dryness and available water conditions are satisfied.
  5. Run the full system: Run the full controller and compare the printed percentages with the pump state.
  6. Validate the prototype: Test borderline conditions and confirm the hysteresis prevents unstable on and off switching.
  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, Pin
import time

SOIL_PIN = 26
TANK_PIN = 27
RELAY_PIN = 14
LED_PIN = 15
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
TANK_EMPTY_RAW = 5000
TANK_FULL_RAW = 50000
IRRIGATE_ON_DRY = 75
IRRIGATE_OFF_DRY = 45
MIN_TANK_PERCENT = 20
MAX_RUN_MS = 8000

soil = ADC(SOIL_PIN)
tank = ADC(TANK_PIN)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
relay.off()
led.off()
pump_on = False
pump_started_ms = 0


def clamp(value, low, high):
    return max(low, min(high, value))


def percent_from_range(raw, low_raw, high_raw):
    span = high_raw - low_raw
    if span == 0:
        return 0
    pct = int((raw - low_raw) * 100 / span)
    return clamp(pct, 0, 100)


print('Autonomous irrigation controller ready')

while True:
    soil_raw = soil.read_u16()
    tank_raw = tank.read_u16()
    dry_pct = percent_from_range(soil_raw, SOIL_WET_RAW, SOIL_DRY_RAW)
    tank_pct = percent_from_range(tank_raw, TANK_EMPTY_RAW, TANK_FULL_RAW)

    now = time.ticks_ms()
    if not pump_on and dry_pct >= IRRIGATE_ON_DRY and tank_pct >= MIN_TANK_PERCENT:
        pump_on = True
        pump_started_ms = now
    elif pump_on:
        runtime = time.ticks_diff(now, pump_started_ms)
        if dry_pct <= IRRIGATE_OFF_DRY or runtime >= MAX_RUN_MS or tank_pct < MIN_TANK_PERCENT:
            pump_on = False

    relay.value(1 if pump_on else 0)
    led.value(1 if pump_on else 0)

    if tank_pct < MIN_TANK_PERCENT and dry_pct >= IRRIGATE_ON_DRY:
        status = 'REFILL TANK'
    else:
        status = 'IRRIGATE' if pump_on else 'WAIT'

    print('Dry:{}% Tank:{}% Status:{}'.format(dry_pct, tank_pct, status))
    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Dual calibration mapping Converts both soil and tank sensor values into percentages Autonomous control depends on understanding crop need and water availability together Measure your real dry, wet, empty, and full baselines before changing the constants
Autonomous control rule Starts watering only when the crop is dry and the tank still has reserve water This is what makes the project truly autonomous rather than reactive Adjust the dryness and minimum tank thresholds only after repeated tests
Pump hysteresis Uses separate on and off dryness thresholds plus a maximum run time Hysteresis reduces unstable switching near the threshold Validate the relay path without the pump first
Status reporting Explains whether the system is waiting, irrigating, or blocked by low tank level Users need to understand why an autonomous controller did not act If the status seems wrong, print the percentages and inspect the sensor baselines first

Expected Result

The relay and LED turn on only when the soil is dry enough and the tank still has enough water.

The system reports REFILL TANK when the crop is dry but the stored water is too low for safe irrigation.

The relay turns off again when the soil recovers or the control limits are reached.

Validation Checks

  • Normal condition test: keep the soil moderately moist and confirm the pump remains off
  • Dry-soil test: dry the soil sensor gradually and confirm irrigation begins only after the dry threshold is reached
  • Low-tank test: simulate a low tank and confirm the controller refuses to irrigate even when the soil is dry
  • Relay validation: test the relay first with no pump attached, then with a safe low-voltage pump
  • Calibration test: record dry, wet, empty, and full raw values before final threshold tuning
  • False trigger test: hold the readings near the thresholds and confirm the relay does not chatter

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
Pump never starts The dry threshold is too high or the relay logic is inverted Test the relay separately and verify the dry percentage reaches the on threshold
Pump runs with an empty tank The tank calibration or minimum level threshold is wrong Print the tank percentage and recalibrate the empty and full references
Dry percentage is backwards The soil calibration order is wrong Re-measure the dry and wet soil references and update the constants
Relay chatters on and off The on and off thresholds are too close or the wiring is unstable Increase the hysteresis gap and check the wiring stability