Skip to content

Project 2.1.31: Water-efficient Irrigation Controller

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


Overview

This project builds an irrigation controller that uses short watering bursts and soak time instead of one long pump run.

Students will combine soil sensing, a water-available safety switch, relay control, and burst-based decision logic to reduce wasted water.

The final system should detect dry soil, apply one or more short watering bursts, wait between bursts for soaking, and stop early when the target moisture level is reached.

Required Components

Soil moisture sensor (analog output)
Soil moisture sensor (analog output)
External pump or valve power supply
External pump or valve power supply
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Water level sensor or water level sensor
Water level sensor or water level sensor
1-channel relay module
1-channel relay module
Status LED and 220 Ω resistor
Status LED and 220 Ω resistor
Breadboard and jumper wires
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
Water level sensor VCC 3.3V Physical pin 36 Sensor power
Water level sensor GND GND Any GND pin Common ground
Water level sensor AOUT GPIO 27 GPIO 27 / physical pin 32 ADC water-level input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Irrigation relay control
Status LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 Active watering indicator
Status LED cathode GND Any GND pin Return path

Wiring Diagram

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 26 ───────────┤──── Soil Sensor AOUT
  │  GPIO 4  ───────────┤──── Water Level Sensor ──── GND
  │                     │
  │  GPIO 15 ───────────┤──── Relay IN
  │  GPIO 16 ────220Ω───┤──── Status LED (+)
  │                     │
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │                     │
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── Status LED (-)
  │  GND     ───────────┤──── Relay GND
  │  GND     ───────────┤──── Water Level Sensor 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 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 and Connect the Water Level Sensor

Place the water level sensor where it can show whether reservoir or source water is available. Connect one water level sensor wire to GPIO 4. Connect the other water level sensor wire to GND. The code uses an internal pull-up, so water available reads LOW.

Step 5: Place and Wire the Relay Driver

Identify relay VCC, GND, IN, COM, NO, and NC before wiring. Connect relay IN to GPIO 15. Power the relay driver according to its board label. Connect relay GND to Pico GND and to the external pump or valve 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 or Valve on the Relay Side

Route the external pump or valve 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] Water level sensor connects between GPIO 4 and GND
  • [x] Relay IN connects to GPIO 15
  • [x] Relay driver uses external power and shared ground where required
  • [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] Pump or valve positive path uses relay COM and NO
  • [x] No load current is routed through the Pico
  • [x] No loose jumper wires

Intermediate Note Calibrate the soil sensor before final use. Test the water level sensor in both states so the controller skips irrigation when source water is missing.

Safety Note Do not power the pump or valve from the Pico. Use an external supply, keep electronics away from water, and never leave a pump dry-running during troubleshooting.


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 relay control side first with the real pump disconnected
  • Label the water level sensor orientation so you remember which position means water available

Test the Input Sensor

  • Record soil sensor values in dry and wet samples and confirm the water level sensor changes state correctly
  • Print both inputs in the Shell so you know the controller is receiving believable values

Test the Output Device

  • Run a short relay click test with no pump connected and confirm the status LED follows the relay state
  • If the relay logic is reversed, invert the relay ON/OFF values to match your hardware

Test Communication

  • Watch the Shell during one test cycle and confirm it prints burst number, soak wait, and soil recovery values
  • Check that the estimated savings message appears at the end of the irrigation session

Run the Full System

  • Let the soil become dry enough to trigger irrigation and observe whether one short burst is enough or whether several bursts are used
  • Force the water level sensor to the no-water state and confirm that the controller refuses to irrigate

Save the Project

  • Save the final program and record the burst duration, soak time, and target moisture values used in your test
  • Write down how the efficient burst approach compared with a single longer watering run

Additional Testing and Calibration Checks

  • Dry/wet reading test: record raw soil values and tune the calibration before final use
  • Water level sensor test: confirm the switch clearly reports water available versus no water
  • Threshold condition test: verify that irrigation starts only when the soil is below the dry threshold
  • Relay click test: confirm the relay and status LED operate only during watering bursts
  • Pump safety test: if using a real pump, keep the first run short and be ready to disconnect power immediately if wiring is wrong

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)
water_sensor = ADC(27)
relay = Pin(15, Pin.OUT)
status_led = Pin(16, Pin.OUT)

RELAY_ON = 1
RELAY_OFF = 0
SOIL_DRY = 52000
SOIL_WET = 22000
DRY_THRESHOLD = 35
TARGET_MOISTURE = 55
MIN_WATER_LEVEL = 30
CHECK_INTERVAL = 120
BURST_SECONDS = 3
SOAK_SECONDS = 10
MAX_BURSTS = 3
TRADITIONAL_RUN_SECONDS = 12

last_check = time.time() - CHECK_INTERVAL
water_saved_seconds = 0
relay.value(RELAY_OFF)
status_led.value(0)


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


def soil_percent():
    raw = soil_sensor.read_u16()
    span = SOIL_DRY - SOIL_WET
    if span <= 0:
        return 0
    return clamp(int(((SOIL_DRY - raw) * 100) / span), 0, 100)


def water_level_percent():
    return int((water_sensor.read_u16() / 65535) * 100)


def water_available():
    return water_level_percent() >= MIN_WATER_LEVEL


def relay_on():
    relay.value(RELAY_ON)
    status_led.value(1)


def relay_off():
    relay.value(RELAY_OFF)
    status_led.value(0)


def efficient_irrigation_session():
    global water_saved_seconds
    if not water_available():
        print('Irrigation skipped: water level too low.')
        return
    start_moisture = soil_percent()
    if start_moisture > DRY_THRESHOLD:
        print('Soil already above dry threshold. No irrigation needed.')
        return
    actual_run_seconds = 0
    print('Efficient irrigation session started at {}% moisture.'.format(start_moisture))
    for burst in range(1, MAX_BURSTS + 1):
        if not water_available():
            print('Irrigation stopped: water level dropped before burst {}.'.format(burst))
            relay_off()
            return
        relay_on()
        time.sleep(BURST_SECONDS)
        relay_off()
        actual_run_seconds += BURST_SECONDS
        time.sleep(SOAK_SECONDS)
        current_moisture = soil_percent()
        print('Moisture after burst {}: {}%'.format(burst, current_moisture))
        if current_moisture >= TARGET_MOISTURE:
            break
    saved = max(0, TRADITIONAL_RUN_SECONDS - actual_run_seconds)
    water_saved_seconds += saved
    print('Estimated relative water savings this session: {} seconds'.format(saved))


print('=== Water-Efficient Irrigation Controller ===')
while True:
    now = time.time()
    if now - last_check >= CHECK_INTERVAL:
        last_check = now
        efficient_irrigation_session()
    print('Soil:{}% Water:{}% Saved:{}s'.format(soil_percent(), water_level_percent(), water_saved_seconds))
    time.sleep(5)

How the Code Works

Code Section What It Does Why It Matters
Burst and soak settings Control how long each irrigation burst runs and how long the soil rests before the next check This is the main water-efficiency strategy in the project
water_available() Reads the water level sensor before and during irrigation The system should not keep running if the water source is empty
TARGET_MOISTURE Defines when the controller has watered enough for the current session This prevents unnecessary extra bursts
Water savings counter Compares the burst total with a longer traditional run time Students can estimate why efficient timing matters

Expected Result

When the soil is dry and water is available, the controller should start a short burst, pause for soaking, then decide whether another burst is needed.

If the soil reaches the target moisture before all bursts are used, the system should stop early.

If the water source is unavailable, the relay should stay off and the Shell should report that irrigation was skipped.


Troubleshooting

Problem Possible cause Solution
The relay never starts The water level sensor logic is reversed or the soil threshold is too low Check the water-level calibration and verify the soil reading is actually below DRY_THRESHOLD
The controller keeps using all bursts The target moisture is too high or the soak time is too short Lower TARGET_MOISTURE slightly or allow more soak time before rechecking
The moisture reading does not improve after watering The soil probe placement is poor or water is not reaching the sensing area Move the probe closer to the root zone and verify the irrigation path
Estimated savings always shows zero The traditional reference time is too short compared with the burst total Increase TRADITIONAL_RUN_SECONDS if you want to compare against a longer continuous watering style