Skip to content

Project 2.6.16: Smart Irrigation Learning Kit

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


Overview

This project builds a guided irrigation trainer that helps students compare automatic watering with manual override control.

Students will build a soil-monitoring circuit, add mode selection and manual watering buttons, and test how the relay behaves in AUTO and MANUAL modes.

The final system should read soil moisture, switch between AUTO and MANUAL learning modes, and run a short watering cycle only when the logic or the student requests it.

Required Components

Soil moisture sensor (analog output)
Soil moisture sensor (analog output)
Yellow LED and 220 Ω resistor
Yellow LED and 220 Ω resistor
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Relay module or transistor relay driver
Relay module or transistor relay driver
Push button
Push button
Green LED and 220 Ω resistor
Green 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
Mode button one side GPIO 5 GPIO 5 / physical pin 7 Uses internal pull-up
Mode button other side GND Any GND pin Pressing pulls the input low
Manual water button one side GPIO 6 GPIO 6 / physical pin 9 Uses internal pull-up
Manual water button other side GND Any GND pin Pressing pulls the input low
Relay IN GPIO 15 GPIO 15 / physical pin 20 Watering output control
AUTO LED anode GPIO 16 through 220 Ω resistor GPIO 16 / physical pin 21 AUTO mode indicator
MANUAL LED anode GPIO 17 through 220 Ω resistor GPIO 17 / physical pin 22 MANUAL mode indicator
LED cathodes GND Any GND pin Return path

Wiring Diagram

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 26 ───────────┤──── Soil Sensor AOUT
  │                     │
  │  GPIO 5  ───────────┤──── Mode Button ──── GND
  │  GPIO 6  ───────────┤──── Water Button ──── GND
  │                     │
  │  GPIO 15 ───────────┤──── Relay IN
  │  GPIO 16 ────220Ω───┤──── AUTO LED (+)
  │  GPIO 17 ────220Ω───┤──── MANUAL LED (+)
  │                     │
  │  3.3V    ───────────┤──── Soil Sensor VCC
  │                     │
  │  GND     ───────────┤──── Soil Sensor GND
  │  GND     ───────────┤──── AUTO LED (-)
  │  GND     ───────────┤──── MANUAL LED (-)
  │  GND     ───────────┤──── Relay GND
  │  GND     ───────────┤──── Mode Button GND
  │  GND     ───────────┤──── Water Button 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: Place the Mode Button

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

Step 4: Place the Manual Water Button

Place the manual water push button across the breadboard center gap. Connect one side of the manual water button to GPIO 6. Connect the opposite side of the manual water button to GND.

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 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 6: Place and Connect the Mode LEDs

Place the AUTO LED and MANUAL LED on the breadboard. Connect the AUTO LED long leg through a 220Ω resistor to GPIO 16. Connect the MANUAL LED long leg through a 220Ω resistor to GPIO 17. Connect both LED short legs to GND.

Step 7: Keep the Load on the Relay Side

Keep the real pump disconnected until the relay click test is successful. If you connect a pump or valve, route the external supply positive wire through relay COM and NO.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
  • [x] Mode button connects between GPIO 5 and GND
  • [x] Manual water button connects between GPIO 6 and GND
  • [x] Relay IN connects to GPIO 15
  • [x] AUTO LED long leg connects through a 220Ω resistor to GPIO 16
  • [x] MANUAL LED long leg connects through a 220Ω resistor to GPIO 17
  • [x] Both LED short legs connect to GND
  • [x] No load current is routed through the Pico
  • [x] No loose jumper wires

Intermediate Note The mode and water buttons use internal pull-ups, so they read LOW when pressed. Test AUTO and MANUAL behavior before connecting a real pump.

Safety Note Do not power pumps, valves, or relay coils from the Pico. Use external load power, keep electronics away from water, and do not leave the relay ON continuously during demonstrations.


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, two buttons, and LEDs first, then add the relay control wire
  • Label the buttons so students do not confuse mode selection with manual watering

Test the Input Sensor

  • Read the soil sensor in dry soil and wet soil and record the values before finalising the calibration
  • Press each button and confirm that the serial output shows a clean input change

Test the Output Device

  • Run a relay click test with no pump connected and confirm the AUTO and MANUAL LEDs match the active mode
  • If the relay behaves backward, adjust the relay logic before connecting external power

Test Communication

  • Watch the Thonny Shell and confirm it prints moisture percentage, current mode, and watering events
  • Use the serial messages to explain why a watering cycle started or did not start

Run the Full System

  • Try dry and wet soil samples in AUTO mode and confirm the relay only runs when the soil is below the threshold
  • Switch to MANUAL mode and use the button to trigger one short watering cycle on demand

Save the Project

  • Save the final code and record the threshold and cooldown values that worked best in your classroom test
  • Write down what students learned from comparing AUTO and MANUAL modes

Additional Testing and Calibration Checks

  • Dry/wet reading test: record soil readings in dry and wet samples before finalising SOIL_DRY and SOIL_WET
  • Normal condition test: confirm the relay stays off in AUTO mode when the soil is already moist enough
  • Threshold condition test: lower the moisture level and confirm AUTO mode starts a watering cycle only below the threshold
  • Output response test: confirm the LEDs and relay match the selected mode and the watering event
  • Relay click test: perform the first hardware test without a pump connected
  • Pump safety test: if a real pump is used later, keep the first run short and supervise it closely

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)
mode_button = Pin(5, Pin.IN, Pin.PULL_UP)
water_button = Pin(6, Pin.IN, Pin.PULL_UP)
relay = Pin(15, Pin.OUT)
auto_led = Pin(16, Pin.OUT)
manual_led = Pin(17, Pin.OUT)

RELAY_ON = 1
RELAY_OFF = 0
SOIL_DRY = 52000
SOIL_WET = 22000
AUTO_THRESHOLD = 35
RECOVERY_THRESHOLD = 50
AUTO_WATER_SECONDS = 4
MANUAL_WATER_SECONDS = 3
COOLDOWN_SECONDS = 45
DEBOUNCE_MS = 250

mode = 'AUTO'
last_watered = time.time() - COOLDOWN_SECONDS
last_mode_press = 0
last_water_press = 0


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 button_event(pin, last_press_ms):
    now_ms = time.ticks_ms()
    if pin.value() == 0 and time.ticks_diff(now_ms, last_press_ms) > DEBOUNCE_MS:
        while pin.value() == 0:
            time.sleep(0.02)
        return True, now_ms
    return False, last_press_ms


def relay_on():
    relay.value(RELAY_ON)


def relay_off():
    relay.value(RELAY_OFF)


def set_mode_leds(current_mode):
    auto_led.value(1 if current_mode == 'AUTO' else 0)
    manual_led.value(1 if current_mode == 'MANUAL' else 0)


def water_once(seconds, reason):
    global last_watered
    relay_on()
    print(reason)
    time.sleep(seconds)
    relay_off()
    last_watered = time.time()


print('=== Smart Irrigation Learning Kit ===')
print('Use the mode button to switch between AUTO and MANUAL learning modes.\n')

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

    mode_pressed, last_mode_press = button_event(mode_button, last_mode_press)
    if mode_pressed:
        mode = 'MANUAL' if mode == 'AUTO' else 'AUTO'
        print('Mode changed to', mode)

    water_pressed, last_water_press = button_event(water_button, last_water_press)

    set_mode_leds(mode)

    if mode == 'AUTO':
        if moisture <= AUTO_THRESHOLD and (now - last_watered) >= COOLDOWN_SECONDS:
            water_once(AUTO_WATER_SECONDS, 'AUTO watering started at {}% soil moisture.'.format(moisture))
        elif moisture >= RECOVERY_THRESHOLD:
            relay_off()
    else:
        relay_off()
        if water_pressed:
            water_once(MANUAL_WATER_SECONDS, 'MANUAL watering cycle started by the student.')

    print('Mode: {} | Soil: {}% | Relay: {} | Seconds since water: {}'.format(
        mode,
        moisture,
        relay.value(),
        int(time.time() - last_watered)
    ))
    time.sleep(2)

How the Code Works

Code Section What It Does Why It Matters
Mode buttons Let the student switch between AUTO and MANUAL behaviour This turns the project into a learning kit instead of only a fixed controller
Soil calibration values Convert raw ADC readings into a realistic moisture percentage Thresholds mean more when they are based on measured dry and wet values
Cooldown timer Waits before another watering cycle is allowed Prevents rapid relay switching and repeated watering
water_once() Runs one controlled watering cycle and logs the reason Students can see exactly why the relay turned on

Expected Result

In AUTO mode, the green LED should stay on and the relay should run only when soil moisture falls below the dry threshold and the cooldown has expired.

In MANUAL mode, the yellow LED should stay on and the manual water button should trigger one short watering cycle.

The serial monitor should show the soil reading, current mode, relay state, and time since the last watering cycle.


Troubleshooting

Problem Possible cause Solution
The mode never changes The button is wired incorrectly or not connected to GND Check the GPIO 5 button wiring and confirm the input goes LOW when pressed
The relay waters too often The dry threshold is too high or the cooldown is too short Lower AUTO_THRESHOLD or increase COOLDOWN_SECONDS after reviewing real soil readings
The MANUAL button does nothing The system is still in AUTO mode or the GPIO 6 button is miswired Switch to MANUAL mode first and recheck the water-button wiring
The moisture percentage looks wrong The dry and wet calibration values do not match the sensor Record new SOIL_DRY and SOIL_WET values using your actual sensor and soil