# soil_monitor.py - MicroPython on Raspberry Pi Pico
# Reads soil sensor on GP26 (ADC0) and prints raw ADC, voltage, and moisture %
from machine import ADC
import utime
# === Configuration ===
ADC_PIN = 26 # GP26 = ADC0
VREF = 3.3 # Pico ADC reference voltage (3.3V)
SAMPLE_INTERVAL = 1.0 # seconds
# Initialize ADC on GP26
adc = ADC(ADC_PIN)
def raw_to_voltage(raw):
"""Convert 16-bit raw ADC reading to voltage (0..3.3V)."""
return (raw * VREF) / 65535.0
def voltage_to_moisture_pct(voltage):
"""Convert voltage to soil moisture % (simple linear mapping)."""
return (voltage / VREF) * 100.0
print("=== Soil Moisture Data Logger ===")
print("Pin: GP{} (ADC0), Reference: {:.1f} V".format(ADC_PIN, VREF))
print("Raw | Voltage (V) | Moisture (%)")
print("---------------------------------")
while True:
raw = adc.read_u16() # raw value (0..65535)
voltage = raw_to_voltage(raw)
moisture_pct = voltage_to_moisture_pct(voltage)
# Print results
print("{:5d} | {:6.3f} V | {:6.1f} %".format(raw, voltage, moisture_pct))
utime.sleep(SAMPLE_INTERVAL)