Project 2.6.22: Smart Irrigation System
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds an automatic irrigation controller that reads soil moisture and waters only when the soil is genuinely dry.
Students will build a calibrated soil-sensing system, a relay-controlled pump stage, and a simple re-arm rule to prevent rapid pump cycling.
The final system should measure moisture, decide when watering is needed, run the pump for a timed burst, and wait for the soil to recover before allowing another cycle.
Required Components
![]() Soil moisture sensor |
![]() LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() 1-channel relay module |
![]() Small DC water pump |
![]() External pump power supply |
![]() Breadboard and jumper wires |
Water container and tubing |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | 3.3V | Physical pin 36 | Sensor power |
| Soil sensor GND | GND | Physical pin 38 | Common ground |
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC0 input |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Relay control signal |
| Relay VCC | Relay supply voltage | Not a Pico GPIO | Check the relay module label |
| Relay GND | Pico GND and external supply GND | Physical pin 38 for Pico GND | Shared ground unless the module is fully isolated |
| Status LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Pump activity indicator |
| Status LED cathode | GND | Physical pin 23 or any GND | LED return path |
| Pump positive lead | Relay COM to NO path | Not a Pico GPIO | Relay switches the pump supply |
| Pump negative lead | External supply negative | Not a Pico GPIO | Do not route pump current through the Pico |
Wiring Diagram

Raspberry Pi Pico 2 W
┌──────────────────────────┐
│ │
│ GPIO 26 ────────────────┤──── Soil sensor AOUT (ADC)
│ 3.3V ─────────────────┤──── Soil sensor VCC
│ GND ─────────────────┤──── Soil sensor GND
│ GPIO 15 ────────────────┤──── Relay IN control
│ GPIO 16 ────────────────┤──── Status LED (through 220Ω)
│ GND ─────────────────┤──── LED and relay GND
│ │
└──────────────────────────┘
Relay Module (external power supply)
├── IN (GPIO 15) from Pico
├── VCC (external relay supply)
├── GND (Pico GND + external supply GND)
├── COM (from pump supply positive)
└── NO (to pump positive)
Water pump (external power supply)
├── Positive from relay NO
└── Negative to supply negative
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.
Check the module labels and identify VCC, GND, and AOUT / AO / Signal.
Step 3: Connect Soil Sensor VCC
Connect the soil sensor VCC pin to 3.3V.
Step 4: Connect Soil Sensor GND
Connect the soil sensor GND pin to GND.
Step 5: Connect Soil Sensor AOUT
Connect the soil sensor AOUT, AO, or Signal pin to GPIO 26 (ADC0).
Step 6: Place the Relay Module
Place the relay module where its control pins and screw terminals are easy to reach.
Identify VCC, GND, IN, COM, NO, and NC before wiring.
Step 7: Connect Relay IN
Connect relay IN to GPIO 15.
Step 8: Connect Relay VCC
Connect relay VCC to the relay supply voltage required by the module label.
Step 9: Connect Relay GND
Connect relay GND to Pico GND and to the external pump supply GND unless the relay module is fully isolated.
Step 10: Place the Status LED
Place the status LED on the breadboard.
Identify the long leg as the anode (+) and the short leg as the cathode (-).
Step 11: Connect the Status LED Long Leg
Connect the status LED long leg to one end of a 220Ω resistor.
Connect the other end of the resistor to GPIO 16.
Step 12: Connect the Status LED Short Leg
Connect the status LED short leg to GND.
Step 13: Wire the Pump Positive Path
Connect the external pump supply positive wire to relay COM.
Connect relay NO to the pump positive lead.
Step 14: Connect the Pump Negative Lead
Connect the pump negative lead directly to the external pump supply negative wire.
Step 15: Arrange the Water Area
Place the pump intake in the water container before testing.
Keep the Pico, breadboard, relay control wiring, USB cable, and jumper wires away from water.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor VCC connects to 3.3V
- [x] Soil sensor GND connects to GND
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Relay IN connects to GPIO 15
- [x] Relay VCC connects to the correct relay supply voltage
- [x] Relay GND, Pico GND, and external pump supply GND share a common reference where required
- [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Status LED short leg connects to GND
- [x] Pump positive path uses relay COM and NO
- [x] Pump negative connects to external pump supply negative
- [x] No pump current is routed through the Pico
- [x] No loose jumper wires
Intermediate Note
Test dry soil and wet soil before choosing moisture thresholds. The relay code is active-low, so the pump may turn on when GPIO 15 is driven LOW.
Safety Note
Do not power the pump from the Pico. Use an external pump supply, keep electronics away from water, and never let the pump run dry.
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 circuit first, then add the LED and relay wiring
- Keep the pump and water container physically separate from the Pico and breadboard
Test the input sensor
- Run a short ADC test and record dry and wet sensor readings
- Confirm that the raw value changes when the probe moves between dry and wet conditions
Test the output device
- Test the relay control with the status LED first
- Only connect the real pump after the relay logic behaves correctly
Run the full system
- Upload the final code and observe the reported moisture percentage
- Check that the pump runs only when the soil is below the dry threshold and stops after the timed cycle
- Confirm that the system does not immediately retrigger until the moisture rises above the recovery threshold
Save the project
- Save the working code to the Pico and to your computer
- Write down the dry and wet reference values used for your calibration
Full Project Code
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)
status_led = Pin(16, Pin.OUT)
dry_reading = 52000
wet_reading = 22000
dry_threshold = 35
recovery_threshold = 45
last_cycle = time.time() - MIN_REST_S
ready_to_water = True
relay.value(RELAY_OFF)
status_led.value(0)
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent(raw_value):
span = dry_reading - wet_reading
if span == 0:
return 0
percent = int(((dry_reading - raw_value) * 100) / span)
return clamp(percent, 0, 100)
def water_cycle(reason):
relay.value(RELAY_ON)
status_led.value(1)
print("Pump ON:", reason)
time.sleep(PUMP_RUN_S)
relay.value(RELAY_OFF)
status_led.value(0)
print("Pump OFF: cycle complete")
print("Smart irrigation system ready")
while True:
now = time.time()
raw_value = soil.read_u16()
moisture = moisture_percent(raw_value)
if moisture >= recovery_threshold:
ready_to_water = True
if ready_to_water and moisture <= dry_threshold and (now - last_cycle) >= MIN_REST_S:
water_cycle("soil below dry threshold")
last_cycle = time.time()
ready_to_water = False
else:
print("Moisture:{}% Ready:{} Rest:{}s".format(
moisture,
ready_to_water,
int(now - last_cycle),
))
time.sleep(5)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| dry_reading and wet_reading | Store the two calibration reference points | The moisture percentage is only meaningful after calibration |
| dry_threshold and recovery_threshold | Set the watering trigger and re-arm level | These values reduce rapid on/off pump cycling |
| water_cycle() | Turns the pump on for a safe fixed time | Timed output is easier to test and troubleshoot than a continuous run |
| ready_to_water and last_cycle | Control when another watering cycle is allowed | These variables add decision logic, not just a single threshold |
Expected Result
After calibration, the pump should remain off while the soil is adequately moist. When the moisture falls below the dry threshold, the relay should switch the pump on for a short watering cycle, then wait until the soil reading rises above the recovery level before another automatic run is allowed.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| Pump runs all the time | Relay logic is reversed or the threshold is set too high | Swap RELAY_ON / RELAY_OFF if needed and confirm the calibrated moisture values |
| Pump never starts | Relay wiring, external power, or calibration values are wrong | Test the relay separately, confirm external pump power, and recalibrate the sensor |
| Moisture percentage looks unrealistic | Dry and wet references do not match your sensor | Record new dry and wet readings and update the calibration values in the code |
| Pump starts too often | The rest time is too short or the recovery threshold is too low | Increase MIN_REST_S or raise the recovery threshold after observing real readings |






