Skip to content

Project 2.6.18: Smart Irrigation Override System

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


Overview

This project adds local manual control to an automatic irrigation system so an operator can override the normal logic when needed.

Students will combine automatic soil-based watering with two physical buttons: one to return to AUTO mode and one to toggle MANUAL override.

The final system should water automatically in AUTO mode, allow the operator to force the pump on or off manually, and switch safely between the two modes.

Required Components

Soil moisture sensor
Soil moisture sensor
External power supply
External power supply
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Push button
Push button
1-channel relay module
1-channel relay module
Small DC pump or safe load
Small DC pump or safe load

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC moisture input
Relay IN GPIO 15 GPIO 15 / physical pin 20 Pump control
Mode button one side GPIO 16 GPIO 16 / physical pin 21 The code toggles AUTO and MANUAL mode.
Mode button other side GND Any GND pin Uses the internal pull-up in code.
---

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 26 ───────────┼──── Soil sensor AOUT
│  GPIO 15 ───────────┼──── Relay IN
│  GPIO 16 ───────────┼──── AUTO button ──┐
│  GPIO 17 ───────────┼──── Override button ──┐
│  3.3V    ───────────┼──── Relay VCC / Soil VCC (if needed)
│  GND     ───────────┼──── Relay GND / Soil GND / Both buttons
│                     │
└─────────────────────┘

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

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

Buttons
AUTO button      ─── GPIO 16  to  GND
Override button  ─── GPIO 17  to  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 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 5: Place the AUTO Button

Place the AUTO push button across the breadboard center gap. Connect one side of the AUTO button to GPIO 16. Connect the opposite side of the AUTO button to GND.

Step 6: Place the Override Button

Place the override push button across the breadboard center gap. Connect one side of the override button to GPIO 17. Connect the opposite side of the override button 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] Relay IN connects to GPIO 15
  • [x] AUTO button connects between GPIO 16 and GND
  • [x] Override button connects between GPIO 17 and 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 AUTO and override buttons use internal pull-ups, so each button reads LOW when pressed. Calibrate the soil sensor before testing automatic and manual pump behavior.

Safety Note

Do not power the pump from the Pico. Use an external pump supply, keep electronics away from water, and test the relay before connecting the pump.


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 and button inputs before connecting the live pump
  • Label the AUTO and override buttons so they are not mixed up during testing

Test the input sensor

  • Calibrate the soil sensor and confirm the moisture percentage is believable
  • Test each button in a short script to confirm the active-low wiring works

Test the output device

  • Verify relay switching before the live pump is connected
  • Test manual override with a safe load first so you know the mode switching works correctly

Run the full system

  • Start in AUTO mode and confirm that the pump runs only when the soil is dry enough
  • Then press the override button to enter manual control and toggle the pump state deliberately
  • Use the AUTO button to return the controller to its automatic behaviour

Save the project

  • Save the final code and record the dry and recovery thresholds used in your test
  • Write down whether the button debounce timing felt stable and responsive

Quick testing checklist

  • ☐ AUTO button returns the system to automatic mode
  • ☐ Override button toggles manual pump control
  • ☐ AUTO mode uses timed watering cycles
  • ☐ MANUAL mode can force the pump ON and OFF safely
  • ☐ Mode messages in the Shell match the actual behaviour

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 = 30

soil = ADC(26)
relay = Pin(15, Pin.OUT)
mode_button = Pin(16, Pin.IN, Pin.PULL_UP)

soil_dry = 52000
soil_wet = 22000
dry_threshold = 35
recovery_threshold = 45

auto_mode = True
manual_force_on = False
ready_to_water = True

last_cycle = time.time() - MIN_REST_S
last_button = time.time()

relay.value(RELAY_OFF)

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

def soil_percent():
    raw = soil.read_u16()
    span = soil_dry - soil_wet
    if span == 0:
        return 0
    return clamp(int(((soil_dry - raw) * 100) / span), 0, 100)

def run_timed_cycle(reason):
    global last_cycle, ready_to_water
    relay.value(RELAY_ON)
    print("Pump ON:", reason)
    time.sleep(PUMP_RUN_S)
    relay.value(RELAY_OFF)
    print("Pump OFF: timed cycle complete")
    last_cycle = time.time()
    ready_to_water = False

print("Smart irrigation override system ready")

while True:
    now = time.time()
    moisture = soil_percent()

    if moisture >= recovery_threshold:
        ready_to_water = True

    if mode_button.value() == 0 and (now - last_button) > 0.3:
        if auto_mode:
            auto_mode = False
            manual_force_on = not manual_force_on
            relay.value(RELAY_ON if manual_force_on else RELAY_OFF)
            print("Mode changed: MANUAL Pump:", manual_force_on)
        else:
            auto_mode = True
            manual_force_on = False
            relay.value(RELAY_OFF)
            print("Mode changed: AUTO")
        last_button = now
        while mode_button.value() == 0:
            time.sleep(0.05)

    if auto_mode:
        if ready_to_water and moisture <= dry_threshold and (now - last_cycle) >= MIN_REST_S:
            run_timed_cycle("automatic soil rule")
        else:
            print("Mode:AUTO Moisture:{}% Ready:{}".format(moisture, ready_to_water))
    else:
        print("Mode:MANUAL Pump:{} Moisture:{}%".format("ON" if manual_force_on else "OFF", moisture))
    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters
AUTO and MANUAL states Separate the two control philosophies clearly Mixed control without explicit modes becomes unsafe quickly
Button debounce timing Prevents one press from being read many times Mode switching should be deliberate
run_timed_cycle() Keeps AUTO mode limited to short watering bursts Timed automatic cycles are safer than unlimited automatic ON states
Override toggle Lets the operator force the pump ON or OFF Manual intervention is essential for maintenance and emergency use

Expected Result

In AUTO mode, the pump should run only when the soil is below the dry threshold and the rest timer allows a new cycle. Pressing the override button should switch to MANUAL mode and toggle the pump state directly. Pressing the AUTO button should stop manual override and return the system to automatic control.


Troubleshooting

Problem Possible cause Solution
The pump changes state several times from one press The button needs better debounce handling Increase the debounce interval or hold the button less briefly during tests.
AUTO mode never waters The soil threshold is too low or calibration is wrong Recalibrate the soil sensor and compare the live reading with the threshold.
Manual override does nothing The button wiring is wrong or relay logic is reversed Test the override button state and verify RELAY_ON / RELAY_OFF.
The system forgets to leave manual mode The AUTO button is not being read correctly Check the GPIO 16 button wiring and the Shell messages after pressing AUTO.