Project 3.1.1: Ai-style Irrigation Decision System
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico uses soil moisture and temperature inputs to choose when a pump relay should water a plant bed.
Overwatering and underwatering both waste resources, so irrigation should respond to more than one condition and should avoid unstable relay switching.
A Pico 2 W prototype with a Soil Moisture Sensor, BME280, relay module, pump output, and status LED.
Rule-based AI-style decision logic, relay safety, hysteresis, and subsystem validation before full automation.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Relay module |
![]() Breadboard |
|
![]() LED |
![]() Resistor |
![]() Water pump |
![]() Soil moisture |
![]() BME280 sensor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Analog moisture reading |
| BME280 VCC | 3.3V output | Physical pin 36 | Sensor power |
| BME280 GND | GND | Physical pin 38 | Common ground |
| BME280 SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| Relay IN | GPIO 14 | GPIO 14 / physical pin 19 | Active-high relay control |
| LED anode | 220 ohm resistor to GPIO 16 | GPIO 16 / physical pin 21 | Irrigation active LED |
Wiring Diagram

Soil sensor AO -> GPIO 26
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
GPIO 14 -> relay IN
GPIO 16 -> 220 ohm resistor -> LED anode
Relay load path -> external pump supply and pump
BME280 VCC -> 3.3V
BME280 GND -> GND
Step-by-Step Assembly
- Connect the Soil Moisture Sensor analog output to GPIO 26.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Connect the relay control input to GPIO 14 and verify whether your module is active high or active low.
- Connect the LED to GPIO 16 through a 220 ohm resistor.
- Wire the pump or test lamp through the relay and external power supply only after relay-only tests pass.
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.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Measure soil sensor and temperature values separately and collect dry, damp, and wet calibration points.
- Test the output device: Toggle the relay and LED with a short test script before connecting the pump or valve.
- Test the decision logic: Confirm that the rule score starts irrigation only in truly dry or hot-dry conditions and stops with hysteresis.
- Run the full system: Run the full controller and compare the relay state with the printed score and dry percentage.
- Validate the prototype: Test slightly different soil conditions to see whether the thresholds are stable or too sensitive.
- 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, I2C, Pin
import bme280
import time
SOIL_PIN = 26
BME_SDA_PIN = 20
BME_SCL_PIN = 21
RELAY_PIN = 14
LED_PIN = 16
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
IRRIGATE_ON_DRY = 70
IRRIGATE_OFF_DRY = 45
MAX_RUN_MS = 8000
soil = ADC(SOIL_PIN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
relay.off()
led.off()
pump_on = False
pump_start_ms = 0
def clamp(value, low, high):
return max(low, min(high, value))
def dry_percent(raw):
span = SOIL_DRY_RAW - SOIL_WET_RAW
if span <= 0:
return 0
pct = int((raw - SOIL_WET_RAW) * 100 / span)
return clamp(pct, 0, 100)
print('AI-style irrigation controller ready')
while True:
soil_raw = soil.read_u16()
dry_pct = dry_percent(soil_raw)
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
pressure = float(values[1].replace('hPa', ''))
humidity = float(values[2].replace('%', ''))
except OSError:
temperature = 0
rule_score = 0
if dry_pct >= 60:
rule_score += 2
if temperature >= 30:
rule_score += 1
now = time.ticks_ms()
if not pump_on and dry_pct >= IRRIGATE_ON_DRY and rule_score >= 2:
pump_on = True
pump_start_ms = now
elif pump_on:
runtime = time.ticks_diff(now, pump_start_ms)
if dry_pct <= IRRIGATE_OFF_DRY or runtime >= MAX_RUN_MS:
pump_on = False
relay.value(1 if pump_on else 0)
led.value(1 if pump_on else 0)
action = 'IRRIGATE' if pump_on else 'WAIT'
print('SoilRaw:{} Dry:{}% Temp:{}C Score:{} Action:{}'.format(
soil_raw,
dry_pct,
temperature,
rule_score,
action,
))
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Calibration constants | Defines the dry and wet references and the on and off thresholds | These values control whether the actuator behaves sensibly or wastes water | Measure real dry and wet values before changing the constants |
| dry_percent | Turns the raw soil reading into a dryness percentage | A calibrated percentage is easier to reason about than a raw ADC number | If the percentage moves the wrong way, recheck the calibration order |
| Rule score | Adds points for dry soil and hot temperature | This is the honest rule-based AI-style part of the project | Tune the score rules only after observing repeated trials |
| Pump control | Uses hysteresis and maximum run time to avoid unstable switching | Safe control design matters as much as the sensing itself | Test with the pump disconnected before using a real load |
Expected Result
The relay and LED remain off when the soil is not dry enough to justify irrigation.
Dry soil or hot-dry conditions increase the score and eventually switch the relay on.
The relay switches off only after the soil recovers or the maximum run time is reached.
Validation Checks
- Normal condition test: place the sensor in damp soil and confirm the relay stays off
- Warning condition test: dry the soil gradually and confirm the dry percentage rises before the relay turns on
- Critical condition test: force a very dry reading and confirm the relay activates only after the rule threshold is met
- Relay validation: test the relay first with no pump attached, then with a safe low-voltage load
- Calibration test: collect raw readings from wet soil, field-moist soil, and dry soil before choosing thresholds
- False trigger test: shake the wires gently and confirm that loose connections do not create false irrigation events
Deployment and Limitations
- This prototype could support classroom irrigation demos, plant labs, or greenhouse pilots with safe low-voltage loads
- Before deployment, the relay wiring, pump protection, enclosure, and sensor calibration routine need further work
- Outdoor use would also need weather protection, more durable moisture sensing, and maintenance planning for drift
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Relay never turns on | The relay module logic is inverted or the dry threshold is too high | Test the relay output separately and confirm whether your module expects active-high or active-low control |
| Pump turns on too often | The on threshold is too low or the calibration is wrong | Raise IRRIGATE_ON_DRY and confirm the soil reference values |
| Dry percentage is always 0 or 100 | The calibration span is wrong or the sensor wiring is faulty | Print the raw value and re-measure the dry and wet endpoints |
| Temperature stays at 0 | The BME280 read failed | Shorten the I2C wires and test the climate sensor on its own |







