← How-Tos
raspberry-pi Mar 26, 2026 ◯ 1 min read

Automated Plant Watering System with Raspberry Pi

raspberry pigpioautomationsensorspython

Read soil moisture and trigger a pump when plants need water. A practical GPIO project combining sensors and automation.

Hardware

Enable SPI for MCP3008

sudo raspi-config
# Interface Options > SPI > Enable

Reading Moisture

import spidev

spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000

def read_channel(channel):
    adc = spi.xfer2([1, (8 + channel) << 4, 0])
    return ((adc[1] & 3) << 8) + adc[2]

# Returns 0 (wet) to 1023 (bone dry)
moisture = read_channel(0)

Full Automation Script

from gpiozero import OutputDevice
import spidev, time

PUMP_PIN = 18
DRY_THRESHOLD = 700   # above this = needs water
WATER_SECONDS = 3     # how long to run the pump

pump = OutputDevice(PUMP_PIN, active_high=False)
spi = spidev.SpiDev()
spi.open(0, 0)

def read_moisture():
    adc = spi.xfer2([1, (8 + 0) << 4, 0])
    return ((adc[1] & 3) << 8) + adc[2]

while True:
    moisture = read_moisture()
    if moisture > DRY_THRESHOLD:
        print("Watering...")
        pump.on()
        time.sleep(WATER_SECONDS)
        pump.off()
        time.sleep(30)   # wait before re-checking
    time.sleep(300)      # check every 5 minutes

Calibration

Measure sensor output in dry soil and fully watered soil for your specific potting mix. Adjust DRY_THRESHOLD accordingly โ€” values vary by sensor and soil type.