Project 2.1.25: Soil Moisture Comparison System
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project compares moisture levels in two soil zones so irrigation differences can be noticed before one area becomes too dry or too wet.
Students will calibrate two sensors separately, calculate the moisture difference, and classify whether the zones are balanced or whether one side is drier.
The final system should report both moisture levels, the percentage difference, and a simple recommendation about which zone needs attention.
Required Components
![]() Soil moisture sensors |
![]() Raspberry Pi Pico 2 W |
![]() Green LED and 220Ω resistor |
![]() Red LED and 220Ω resistor |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Zone A soil AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC0 input |
| Zone B soil AOUT | GPIO 27 | GPIO 27 / physical pin 32 | ADC1 input |
| Green LED anode | GPIO 16 through 220Ω resistor | GPIO 16 / physical pin 21 | Balanced-state indicator |
| Red LED anode | GPIO 17 through 220Ω resistor | GPIO 17 / physical pin 22 | Difference alert indicator |
| Both LED cathodes | GND | Any GND pin | Common return |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┼──── Zone A soil sensor AOUT
│ GPIO 27 ───────────┼──── Zone B soil sensor AOUT
│ GPIO 16 ───────────┼──── Resistor (220Ω) ── Green LED anode (+)
│ GPIO 17 ───────────┼──── Resistor (220Ω) ── Red LED anode (+)
│ GND ───────────┼──── Both LED cathodes (-)
│ │
└─────────────────────┘
Zone A Soil Sensor
VCC ─────────────── 3.3V
GND ─────────────── GND
AOUT ─────────────── GPIO 26
Zone B Soil Sensor
VCC ─────────────── 3.3V
GND ─────────────── GND
AOUT ─────────────── GPIO 27
Green LED (Balanced)
Anode (+) ──[220Ω]── GPIO 16
Cathode (-) ───────── GND
Red LED (Difference Alert)
Anode (+) ──[220Ω]── GPIO 17
Cathode (-) ───────── 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: Label the Two Soil Sensors
Label the two soil moisture sensors as Zone A and Zone B before wiring. Identify VCC, GND, and AOUT / AO / Signal on each module.
Step 3: Connect Zone A Soil Sensor
Connect Zone A sensor VCC to 3.3V. Connect Zone A sensor GND to GND. Connect Zone A AOUT, AO, or Signal to GPIO 26 (ADC0).
Step 4: Connect Zone B Soil Sensor
Connect Zone B sensor VCC to 3.3V. Connect Zone B sensor GND to GND. Connect Zone B AOUT, AO, or Signal to GPIO 27 (ADC1).
Step 5: Place and Connect the Green LED
Place the green LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the green LED long leg through a 220Ω resistor to GPIO 16. Connect the green LED short leg to GND.
Step 6: Place and Connect the Red LED
Place the red LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the red LED long leg through a 220Ω resistor to GPIO 17. Connect the red LED short leg to GND.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Zone A soil signal connects to GPIO 26
- [x] Zone B soil signal connects to GPIO 27
- [x] Green LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Red LED long leg connects through a 220Ω resistor to GPIO 17
- [x] Both LED short legs connect to GND
- [x] No loose jumper wires
Intermediate Note
Calibrate Zone A and Zone B separately. Two soil sensors can produce different raw readings even when placed in similar soil.
Safety Note
Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Add water to soil samples away from the electronics.
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 one soil sensor path first, then copy the wiring pattern for the second zone
- Label the two zones so the readings do not get mixed up later
Test the input sensors
- Calibrate Zone A and Zone B separately because two sensors may not match exactly
- Test each ADC channel by moving only one sensor at a time between dry and wet conditions
Test the output devices
- Test both LEDs before running the full comparison logic
- Confirm that only one overall comparison state is indicated at a time
Run the full system
- Place the sensors in two different soil samples and compare the results over several readings
- Dry one zone more than the other and observe how the state and LED outputs change
Save the project
- Save the final code and record the calibration values used for each zone
- Write down the difference threshold that best separated normal variation from a real imbalance
Quick testing checklist
- ☐ Both ADC channels respond independently
- ☐ Zone A and Zone B percentages look believable
- ☐ The system reports which zone is drier
- ☐ Green LED shows a small difference
- ☐ Red LED shows a large imbalance
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
sensor_a = ADC(26)
sensor_b = ADC(27)
green_led = Pin(16, Pin.OUT)
red_led = Pin(17, Pin.OUT)
a_dry = 52000
a_wet = 22000
b_dry = 52000
b_wet = 22000
difference_alert = 15
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent(sensor, dry_ref, wet_ref):
raw = sensor.read_u16()
span = dry_ref - wet_ref
if span == 0:
return 0
percent = int(((dry_ref - raw) * 100) / span)
return clamp(percent, 0, 100)
def comparison_state(a_value, b_value):
diff = abs(a_value - b_value)
if diff <= 5:
return diff, "BALANCED"
if a_value < b_value:
return diff, "ZONE A DRIER"
return diff, "ZONE B DRIER"
print("Soil moisture comparison system ready")
while True:
moisture_a = moisture_percent(sensor_a, a_dry, a_wet)
moisture_b = moisture_percent(sensor_b, b_dry, b_wet)
diff, state = comparison_state(moisture_a, moisture_b)
green_led.value(1 if diff < difference_alert else 0)
red_led.value(1 if diff >= difference_alert else 0)
print("ZoneA:{}% ZoneB:{}% Diff:{}% State:{}".format(moisture_a, moisture_b, diff, state))
time.sleep(5)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Separate calibration values | Allow Zone A and Zone B to be scaled independently | Two sensors should not be forced to share one calibration blindly |
| comparison_state() | Turns raw percentages into a practical difference label | Students need to know which side is drier, not only the numbers |
| difference_alert | Defines when the mismatch is large enough to matter | Not every small percentage difference requires intervention |
| LED logic | Shows balanced versus significant difference visually | Visual output makes side-by-side comparison faster |
Expected Result
The system should print both moisture percentages and show whether the zones are BALANCED, ZONE A DRIER, or ZONE B DRIER. The green LED should show a small difference, while the red LED should indicate a larger imbalance.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| One zone always appears drier | That sensor is miscalibrated or wired differently | Recalibrate the two sensors independently and recheck the ADC pin assignments. |
| The difference is always zero | Both sensors are in the same condition or one input is not changing | Test each sensor separately in different moisture samples. |
| The wrong LED is on | The threshold or LED wiring is wrong | Check difference_alert and confirm the LED pin assignments. |
| The state label seems backwards | Zone A and Zone B were swapped physically or in the code | Label the sensors and compare the live readings while moving only one probe. |



