I got fed up with plants either drowning or drying out, so I built a Raspberry Pi to water them for me. It uses the Pi’s GPIO pins to read soil moisture sensors and switch a pump and valves, so each plant gets watered when it actually needs it and not on a guess. It works just as well for a few pots on the patio as it does in a greenhouse, and it takes a daily job off your list.
Why it’s worth it
The biggest win is not having to think about it. Your plants get looked after properly every day, even when you’re not there, and you can stop worrying about who’s going to water them while you’re on holiday.
Kit list
- 1x Raspberry Pi 4 running Ubuntu 22.04
- 1x ultrasonic water tank sensor (HC-SR04)
- 2x soil moisture sensors (LM393), powered from the Pi’s 3.3V pin
- 3x 5V single channel relay modules (two valves and the pump)
- 1x small water pump
- 1x 1kΩ and 1x 2kΩ resistor, for a voltage divider on the HC-SR04 echo pin
- 2x DC 4.5V inlet water solenoid valves, 1/4” high pressure
- 1x breadboard
Raspberry Pi pinout
- Plant pot 1 valve relay: GPIO 26
- Plant pot 2 valve relay: GPIO 27
- Water pump 1 relay: GPIO 22
- Plant pot 1 moisture sensor DO pin: GPIO 23
- Plant pot 2 moisture sensor DO pin: GPIO 6
- Ultrasonic water tank sensor trigger pin: GPIO 16
- Ultrasonic water tank sensor echo pin: GPIO 25, through a voltage divider
The HC-SR04 runs on 5V and its echo pin sends back a 5V signal, but the Pi’s GPIO pins only take 3.3V. Connecting it straight to the Pi can damage the GPIO pin, so I use a voltage divider: the echo pin goes through the 1kΩ resistor to GPIO 25, and the 2kΩ resistor connects GPIO 25 to ground. That brings the 5V signal down to about 3.3V. The trigger pin is fine as it is, because the Pi’s 3.3V output is enough for the sensor to read.
The script waters a pot when its moisture sensor reads dry, which on my boards is a DO reading of 0. LM393 boards don’t all work the same way round, so test yours with the sensor in a glass of water and in dry soil, and change DRY_READING at the top of the script if yours reads the other way. After watering a pot the script leaves it alone for 15 minutes, so the water has time to soak down to the sensor before it checks again.
How it’s gone
Mine waters two plant pots, and it’s still running. The one thing that caught me out was the water tank running dry.
When the tank is empty, the pump has nothing to push, so the plants don’t get watered and the pump runs dry, which isn’t good for it either. That’s why the ultrasonic tank sensor matters more than it first looks. It measures the gap between the top of the tank and the water, so the script knows when the level is getting low and stops watering when it’s empty, instead of switching the pump on for nothing.
While writing this up I found three bugs in the version of the script on this page, and I’ve fixed them below:
- The tank always read “Empty”. The level check tested the empty case first, against a range with no upper limit, so it matched every reading. It now checks from full down to empty.
- The empty tank check never kicked in. The script had a
water_tank_emptyflag for pausing until you refill, but nothing ever set it. It now updates from the tank sensor on every loop. - The overnight pause never paused. “Between 22:00 and 06:00” was written as one comparison, which can never be true because it crosses midnight. It’s now two checks joined with “or”.
If you’ve copied the script before, it’s worth grabbing it again.
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
# GPIO pin definitions
PLANT_POT_1_VALVE_PIN = 26
PLANT_POT_2_VALVE_PIN = 27
WATER_PUMP_1_PIN = 22
PLANT_POT_1_MOISTURE_PIN = 23
PLANT_POT_2_MOISTURE_PIN = 6
WATER_TANK_TRIGGER_PIN = 16
WATER_TANK_ECHO_PIN = 25
# What a moisture sensor's DO pin reads when the soil is dry. Mine read 0 when dry;
# many LM393 boards read 1 when dry instead, so test yours and change this if needed.
DRY_READING = 0
# After watering a pot, leave it this long before watering it again, so the water
# has time to soak down to the sensor.
COOLDOWN_SECONDS = 15 * 60
last_watered = {1: 0, 2: 0}
# Ultrasonic water tank sensor ranges
FULL_RANGE = (1, 40)
MEDIUM_RANGE = (41, 81)
LOW_RANGE = (82, 100)
EMPTY_RANGE = (101, float('inf'))
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(PLANT_POT_1_VALVE_PIN, GPIO.OUT)
GPIO.setup(PLANT_POT_2_VALVE_PIN, GPIO.OUT)
GPIO.setup(WATER_PUMP_1_PIN, GPIO.OUT)
GPIO.setup(WATER_TANK_TRIGGER_PIN, GPIO.OUT)
GPIO.setup(WATER_TANK_ECHO_PIN, GPIO.IN)
GPIO.setup(PLANT_POT_1_MOISTURE_PIN, GPIO.IN)
GPIO.setup(PLANT_POT_2_MOISTURE_PIN, GPIO.IN)
# Global variables
water_tank_empty = False
time_restriction_enabled = True
def check_moisture(sensor_pin):
# True when the soil is moist enough, False when it needs water
return GPIO.input(sensor_pin) != DRY_READING
def check_water_tank():
GPIO.output(WATER_TANK_TRIGGER_PIN, True)
time.sleep(0.00001)
GPIO.output(WATER_TANK_TRIGGER_PIN, False)
pulse_start = time.time()
pulse_end = time.time()
# Give up after 0.1 seconds, so a disconnected sensor can't hang the script.
# No reading counts as empty, which keeps the pump off.
timeout = time.time() + 0.1
while GPIO.input(WATER_TANK_ECHO_PIN) == 0:
pulse_start = time.time()
if pulse_start > timeout:
return 9999
timeout = time.time() + 0.1
while GPIO.input(WATER_TANK_ECHO_PIN) == 1:
pulse_end = time.time()
if pulse_end > timeout:
return 9999
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
return int(distance) # Convert to integer before returning
def water_tank_status():
# The sensor measures the gap from the top of the tank down to the water,
# so a bigger distance means less water. Check from full to empty.
distance = int(check_water_tank())
if distance <= FULL_RANGE[1]:
return "Full"
elif distance <= MEDIUM_RANGE[1]:
return "Medium"
elif distance <= LOW_RANGE[1]:
return "Low"
else:
return "Empty"
def run_irrigation_system():
global water_tank_empty
while True:
now = int(time.strftime('%H%M'))
# No watering overnight, between 22:00 and 06:00
if time_restriction_enabled and (now >= 2200 or now <= 600):
print("Time Restriction: Script paused due to time restriction.")
time.sleep(60)
continue
plant_pot_1_moisture = check_moisture(PLANT_POT_1_MOISTURE_PIN)
plant_pot_2_moisture = check_moisture(PLANT_POT_2_MOISTURE_PIN)
tank = water_tank_status()
water_tank_empty = tank == "Empty" # never run the pump with no water
print("Ultra Sonic Water Tank Sensor:", tank)
print("Plant Pot 1 Moisture Sensor:", plant_pot_1_moisture)
print("Plant Pot 1 Valve Relay:", GPIO.input(PLANT_POT_1_VALVE_PIN))
print("Plant Pot 2 Moisture Sensor:", plant_pot_2_moisture)
print("Plant Pot 2 Valve Relay:", GPIO.input(PLANT_POT_2_VALVE_PIN))
if water_tank_empty:
print("Water Tank Empty: Refill required.")
time.sleep(60)
continue
if not plant_pot_1_moisture and time.time() - last_watered[1] > COOLDOWN_SECONDS:
GPIO.output(WATER_PUMP_1_PIN, True)
GPIO.output(PLANT_POT_1_VALVE_PIN, True)
print("Watering Plant Pot 1...")
time.sleep(30)
GPIO.output(PLANT_POT_1_VALVE_PIN, False)
GPIO.output(WATER_PUMP_1_PIN, False)
print("Plant Pot 1 watered.")
last_watered[1] = time.time()
elif GPIO.input(PLANT_POT_1_VALVE_PIN):
GPIO.output(WATER_PUMP_1_PIN, False)
if not plant_pot_2_moisture and time.time() - last_watered[2] > COOLDOWN_SECONDS:
GPIO.output(WATER_PUMP_1_PIN, True)
GPIO.output(PLANT_POT_2_VALVE_PIN, True)
print("Watering Plant Pot 2...")
time.sleep(30)
GPIO.output(PLANT_POT_2_VALVE_PIN, False)
GPIO.output(WATER_PUMP_1_PIN, False)
print("Plant Pot 2 watered.")
last_watered[2] = time.time()
elif GPIO.input(PLANT_POT_2_VALVE_PIN):
GPIO.output(WATER_PUMP_1_PIN, False)
if plant_pot_1_moisture and plant_pot_2_moisture:
time.sleep(30)
if not GPIO.input(PLANT_POT_1_VALVE_PIN) and not GPIO.input(PLANT_POT_2_VALVE_PIN):
GPIO.output(WATER_PUMP_1_PIN, False)
time.sleep(1)
try:
run_irrigation_system()
except KeyboardInterrupt:
print("\nExiting...")
finally:
GPIO.cleanup()What to read next
3 pages
Got a question about this, or built something similar? Get in touch. New write-ups go out on the RSS feed and LinkedIn, everything I've built is on the projects page, and my free calculators and guides are on the tools page.