Project 3.1.3: Compost Control
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico reads a compost moisture sensor and runs a relay-controlled watering device when the compost becomes too dry.
Compost systems work best when moisture stays within a useful range instead of swinging between dry and soggy conditions.
A Pico 2 W prototype with an analog moisture sensor, relay output, and LED status indicator.
Calibration, moisture interpretation, and safer actuator control for a real-world biological process.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Relay module |
![]() Breadboard |
![]() Jumper wires |
![]() LED |
![]() Resistor |
![]() Water pump |
![]() Soil moisture sensor |
![]() 9V battery |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Moisture sensor VCC | 3.3V output | Physical pin 36 | Use a 3.3V-safe sensor output |
| Moisture sensor GND | GND | Physical pin 38 | Common ground |
| Moisture sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Analog moisture reading |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Relay control |
| Relay VCC | External 5V or module-rated supply | Not a Pico GPIO pin | Use the correct module supply |
| Relay GND | GND | Physical pin 38 | Share ground unless fully isolated |
| LED anode | 220 ohm resistor to GPIO 16 | GPIO 16 / physical pin 21 | Watering status LED |
| LED cathode | GND | Physical pin 23 or 38 | Ground return |
Wiring Diagram

3.3V -> moisture sensor VCC
GND -> moisture sensor GND, relay GND, LED cathode
Moisture sensor AO -> GPIO 26
GPIO 15 -> relay IN
GPIO 16 -> 220 ohm resistor -> LED anode
External supply -> relay-switched pump or valve
Step-by-Step Assembly
- Connect the moisture sensor to Pico 3.3V, GND, and GPIO 26.
- Connect the relay control input to GPIO 15 and power the relay module correctly.
- Wire the LED to GPIO 16 through a 220 ohm resistor and connect the LED cathode to GND.
- Dry-test the relay before connecting the watering load.
- Measure dry and moist compost values and update the calibration constants before full testing.
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: Read raw moisture values in dry and wet compost so the calibration values are based on real measurements.
- Test the output device: Toggle the relay and LED without the pump connected so the actuator path is safe first.
- Test the decision logic: Confirm that watering starts below the low threshold and stays off above the high threshold.
- Run the full system: Run the full system and verify that the printed moisture percentage matches the relay state.
- Validate the prototype: Check whether sensor placement depth changes the moisture percentage enough to require recalibration.
- 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, Pin
import time
MOISTURE_PIN = 26
RELAY_PIN = 15
LED_PIN = 16
DRY_CAL = 14000
WET_CAL = 42000
WATER_ON_PCT = 45
WATER_OFF_PCT = 60
moisture_sensor = ADC(MOISTURE_PIN)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
watering = False
relay.off()
led.off()
print("Compost control ready")
def moisture_percent(raw):
span = WET_CAL - DRY_CAL
if span == 0:
return 0
pct = int((raw - DRY_CAL) * 100 / span)
if pct < 0:
return 0
if pct > 100:
return 100
return pct
while True:
raw = moisture_sensor.read_u16()
pct = moisture_percent(raw)
if not watering and pct <= WATER_ON_PCT:
watering = True
elif watering and pct >= WATER_OFF_PCT:
watering = False
relay.value(1 if watering else 0)
led.value(1 if watering else 0)
status = "WATERING" if watering else "MONITORING"
print("Raw: {} | Moisture: {} % | Status: {}".format(raw, pct, status))
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Calibration constants | Defines dry and wet reference values plus on and off thresholds | Moisture control is only meaningful when calibration matches the real compost material | Update DRY_CAL and WET_CAL after measuring your own compost |
| moisture_percent | Converts the raw sensor reading into an estimated moisture percentage | A percentage is easier for students to reason about than a raw ADC number | If your sensor behaves in reverse, swap or recalculate the calibration values |
| Hysteresis control | Turns watering on at the low threshold and off at the higher threshold | Two thresholds reduce chattering and create more stable control | Tune WATER_ON_PCT and WATER_OFF_PCT after real wetting tests |
| Relay and reporting | Updates the load state and prints the live reading and status | This helps validate whether the control rule matches the compost condition | Only change the status wording after the logic is correct |
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 could be used in school, farm, or sanitation demonstrations with safe low-voltage loads
- Before deployment, the load wiring, enclosure, and power design need careful review
- Actuator systems need maintenance planning because relay wear and sensor drift affect reliability
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Moisture percentage is nonsense | Calibration values do not match the real material | Measure dry and moist reference readings again and update DRY_CAL and WET_CAL |
| Pump never runs | Low threshold is too low or the relay path is wrong | Raise WATER_ON_PCT temporarily and dry-test the relay output |
| Pump runs too long | High threshold is too high or the sensor is in a poor location | Lower WATER_OFF_PCT or reposition the sensor where moisture is more representative |
| Sensor corrodes quickly | A resistive probe is being used in wet material for long periods | Switch to a capacitive probe or limit how long the probe stays inserted |








