Project 2.1.26: Soil Moisture Trend Analyzer
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project studies how soil moisture changes over time instead of reacting only to one reading.
Students will collect repeated moisture samples, calculate a moving average, classify the trend, and estimate when the dryness threshold may be reached.
The final system should show the current moisture, a smoothed average, a trend label, and a simple prediction based on recent history.
Required Components
![]() Soil moisture sensor |
![]() Raspberry Pi Pico 2 W |
![]() 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 | Physical pin 38 | Common ground |
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC input |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ │
│ GND ───────────┤──── Soil Sensor GND
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
└─────────────────────┘
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 Sensor VCC
Connect the soil sensor VCC pin to 3.3V.
Step 4: Connect Sensor GND
Connect the soil sensor GND pin to GND.
Step 5: Connect AOUT to GPIO 26
Connect the soil sensor AOUT, AO, or Signal pin to GPIO 26 (ADC0).
Step 6: Stabilize the Sample
Keep the probe and soil sample still while collecting trend readings. Avoid repeatedly moving the probe, because that changes the measured trend.
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] Probe is stable in the soil sample
- [x] No loose jumper wires
Intermediate Note Test dry soil and wet soil before interpreting the trend. A trend is more useful when the probe position and soil sample stay consistent.
Safety Note Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Add water to the soil sample 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 the soil-sensor wiring only for this project
- Use one consistent soil sample so the trend is easier to interpret
Test the Input Sensor
- Calibrate dry and wet values first
- Confirm that the moisture percentage responds to real changes before starting the trend run
Test the Output Data
- Run the project long enough to collect at least six samples so the trend state can change from COLLECTING to a real label
- Watch the history list and compare it with the moving average output
Run the Full System
- Start with a moist sample and let it dry gradually while the analyzer records repeated readings
- Check whether the estimated time to dry becomes more meaningful once the trend is steadily falling
Save the Project
- Save the final code and record the sample interval and history size used in your test
- Write down how accurate the prediction seemed when compared with the real soil behaviour
Additional Testing and Calibration Checks
Calibration and tuning notes
- Use a shorter sample interval during classroom testing so the trend forms quickly
- Do not flood or disturb the soil sample between every reading because that breaks the natural trend
- If the trend seems noisy, increase the history size or reduce handling of the sensor
- Treat the prediction as a planning estimate, not an exact time guarantee
Quick testing checklist
- [ ] Moisture readings are stored in a rolling history
- [ ] The moving average updates as new data arrives
- [ ] The trend label changes after enough samples are collected
- [ ] The prediction appears only when the trend is meaningfully decreasing
- [ ] The analysis matches the real drying direction of the soil sample
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
import time
HISTORY_SIZE = 12
SAMPLE_INTERVAL_S = 10
DRY_ALERT = 35
soil = ADC(26)
dry_reading = 52000
wet_reading = 22000
history = []
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent():
raw = soil.read_u16()
span = dry_reading - wet_reading
if span == 0:
return 0
percent = int(((dry_reading - raw) * 100) / span)
return clamp(percent, 0, 100)
def moving_average(values):
return sum(values) / len(values) if values else 0
def trend_state(values):
if len(values) < 6:
return "COLLECTING"
older = moving_average(values[: len(values) // 2])
recent = moving_average(values[len(values) // 2 :])
delta = recent - older
if delta <= -3:
return "DECREASING"
if delta >= 3:
return "INCREASING"
return "STABLE"
def estimate_minutes_to_dry(values):
if len(values) < 6:
return None
slope = (values[-1] - values[0]) / (len(values) - 1)
if slope >= -0.2:
return None
points_until_dry = (values[-1] - DRY_ALERT) / abs(slope)
minutes = points_until_dry * SAMPLE_INTERVAL_S / 60
return max(0, int(minutes))
print("Soil moisture trend analyzer ready")
while True:
moisture = moisture_percent()
history.append(moisture)
if len(history) > HISTORY_SIZE:
history.pop(0)
average = moving_average(history)
trend = trend_state(history)
minutes_to_dry = estimate_minutes_to_dry(history)
print("Current:{}% Average:{:.1f}% Trend:{}".format(moisture, average, trend))
if minutes_to_dry is None:
print("Prediction: collect more data or trend is not falling yet")
else:
print("Prediction: about {} minutes until {}%".format(minutes_to_dry, DRY_ALERT))
print("History:", history)
print("-" * 40)
time.sleep(SAMPLE_INTERVAL_S)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
history list |
Stores the most recent moisture values | Trend analysis is impossible without past data |
moving_average() |
Smooths the sensor readings | Noise should not dominate the trend decision |
trend_state() |
Compares older and newer halves of the history | Students learn a simple form of time-series comparison |
estimate_minutes_to_dry() |
Predicts when the dry threshold may be reached | Prediction should be presented as an estimate, not a guarantee |
Expected Result
The analyzer should first show a COLLECTING state, then switch to DECREASING, INCREASING, or STABLE once enough readings are stored. It should also report a moving average and, when the trend is steadily falling, an estimated time until the dry threshold is reached.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The trend stays at COLLECTING | Not enough samples were stored yet | Wait until at least six samples exist in the history |
| The prediction never appears | The slope is flat or the moisture is not decreasing steadily | Let the soil dry more naturally or reduce disturbances during the test |
| The history looks erratic | The sensor is loose or the soil sample is being moved too much | Stabilise the wiring and keep the sample consistent |
| The analyzer predicts too soon or too late | The threshold or sample interval does not match reality | Tune DRY_ALERT and SAMPLE_INTERVAL_S based on observed behaviour |


