Project 3.1.7: Garden Moisture Display
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico reads a soil-moisture sensor and shows the result as a percentage and category on a local display.
Garden irrigation decisions are harder to justify when only a raw analog number is shown.
A Pico 2 W prototype with a soil-moisture sensor and OLED display.
Sensor calibration, percent mapping, and moisture-category interpretation.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Soil moisture sensor |
![]() Breadboard |
![]() Jumper wires |
![]() OLED |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Analog moisture input |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
Wiring Diagram

Soil sensor AO -> GPIO 26
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> soil sensor and OLED
Step-by-Step Assembly
- Connect the soil sensor analog output to GPIO 26.
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21.
- Upload ssd1306.py and confirm it is present on the Pico.
- Measure dry-soil and wet-soil values before enabling the percentage interpretation.
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 the analog reading in dry and wet soil separately to create calibration points.
- Test the output device: Run an OLED hello-world script before using the full display logic.
- Test the decision logic: Move the probe between dry, moist, and wet soil and confirm the category changes logically.
- Run the full system: Run the full display and compare the printed percent with the displayed category.
- Validate the prototype: Repeat the test in different soil types and discuss how soil composition affects calibration.
- 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 time
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
SENSOR_PIN = 26
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
DRY_VALUE = 52000
WET_VALUE = 22000
sensor = ADC(SENSOR_PIN)
i2c = I2C(0, sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=400000)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
print('Soil moisture display ready')
while True:
reading = sensor.read_u16()
reading = max(WET_VALUE, min(DRY_VALUE, reading))
percent = int((DRY_VALUE - reading) * 100 / (DRY_VALUE - WET_VALUE))
if percent >= 70:
label = 'WET'
elif percent >= 35:
label = 'MOIST'
else:
label = 'DRY'
print('Reading:{} Moisture:{}% Label:{}'.format(reading, percent, label))
if HAS_OLED:
oled.fill(0)
oled.text('Soil Moist', 0, 0)
oled.text('{}%'.format(percent), 0, 22)
oled.text(label, 0, 46)
oled.show()
time.sleep(1)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Calibration mapping | Converts the raw analog reading into a percentage using dry and wet endpoints | Percent values are easier for learners to interpret than raw ADC numbers | Measure your own dry and wet endpoints instead of copying the defaults blindly |
| Moisture labels | Maps the percent into DRY, MOIST, or WET | Categories make the display actionable in a classroom demo | Adjust the category boundaries after testing real plant or garden conditions |
| OLED display | Shows the moisture result locally | The display helps users compare the number and category quickly | Verify ssd1306.py and I2C wiring if the screen is blank |
Expected Result
The serial monitor reports the current reading or state clearly.
The hardware output responds when the decision logic changes state.
Subsystem behavior matches the thresholds, timing, or rules described in the document.
Validation Checks
- Normal condition test: confirm the system stays in its safe or idle state under baseline conditions
- Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible
- Critical condition test: trigger the strongest alert or control state and confirm the output response is correct
- Calibration test: compare the sensor or timing against a known reference or repeated trial
- Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves
Deployment and Limitations
- This prototype works well for classroom demonstrations and local monitoring pilots
- Before deployment, it needs calibration records, protective mounting, and regular validation checks
- Display-only systems still depend on correct sensor placement and trained users
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| The percent never changes much | The sensor is not inserted well or the soil conditions are too similar | Reposition the probe and compare clearly dry and wet samples |
| Percent looks backwards | The dry and wet calibration values are reversed | Measure both endpoints again and update the constants correctly |
| OLED is blank | The library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21 |
| Values drift over time | The probe is corroding or the soil contact is inconsistent | Clean the probe and use the same insertion depth during tests |




