Raspberry Pi - Irrigation
This tutorial instructs you how to create an automatic irrigation system by using Raspberry Pi, a soil moisture sensor, relay, and pump. In detail, we learn how to make two application use-cases:
- Application 1:
- Raspberry Pi will control the relay to turn on the pump when the soil moisture is dry, thus watering the plants.
- Raspberry Pi will control the relay to turn off the pump when the soil moisture is wet.
- Application 2:
- Raspberry Pi will control the relay to turn on the pump 5 seconds if the soil moisture is dry, and then turn the pump off, and check the soil moisture again.
- Raspberry Pi - Soil Moisture Sensor tutorial
- Raspberry Pi - Controls Pump tutorial
- The Raspberry Pi will control the relay to turn on the pump when the soil moisture is dry, watering the plants.
- The Raspberry Pi will control the relay to turn off the pump when the soil moisture is wet.
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
- Calibrate the sensor to get the wet-dry THRESHOLD value by following the instructions in Raspberry Pi - Calibrates Soil Moisture Sensor.
- Create a Python script file auto_irrigation_1.py and add the following code:
- Update the calibrated THRESHOLD value in the code.
- Save the file and run the Python script by executing the following command in the terminal:
- Check the result on the Terminal.
- The Raspberry Pi will turn on the pump when the soil is dry for 5 seconds, and then it will turn off the pump.
- Create a Python script file auto_irrigation_2.py and add the following code:
- Update the calibrated THRESHOLD value in the code.
- Save the file and run the Python script by executing the following command in the terminal:
- Check the result on the Terminal.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
Additionally, some of these links are for products from our own brand, DIYables.
Overview of soil moisture sensor and Pump
If you are unfamiliar with pump and soil moisture sensor (including pinout, operation, programming, etc.), the following tutorials can help:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code - Application 1
We'll create the irrigation system as follows:
Detailed Instructions
sudo apt-get update
sudo apt-get install python3-rpi.gpio
sudo pip install Adafruit-ADS1x15
"""
This Raspberry Pi code was developed by newbiely.com
This Raspberry Pi code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-irrigation
"""
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
# Create an ADS1115 ADC object
adc = Adafruit_ADS1x15.ADS1115()
# Set the gain to ±4.096V (adjust if needed)
GAIN = 1
# Single threshold for wet/dry classification (adjust as needed)
THRESHOLD = 45000
# GPIO pin connected to the relay module to control the pump
RELAY_PIN = 23
# Setup GPIO mode and relay pin as output
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
# Initialize pump state to OFF
GPIO.output(RELAY_PIN, GPIO.LOW)
pump_state = False
# Main loop to read the analog value from the soil moisture sensor and control the pump
try:
while True:
# Read the raw analog value from channel A3
raw_value = adc.read_adc(3, gain=GAIN)
# Determine the wet-dry level based on the raw ADC value
if raw_value > THRESHOLD:
level = "DRY"
else:
level = "WET"
# Print the results
print("Raw Value: {} \t Wet-Dry Level: {}".format(raw_value, level))
# Control the pump based on the wet-dry level
if level == "DRY" and not pump_state:
GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn on the pump
pump_state = True
print("Pump State: ON")
elif level == "WET" and pump_state:
GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the pump
pump_state = False
print("Pump State: OFF")
# Add a delay between readings (adjust as needed)
time.sleep(1)
except KeyboardInterrupt:
# Cleanup GPIO on exit
GPIO.cleanup()
print("\nExiting the program.")
python3 auto_irrigation_1.py
PuTTY - Raspberry Pi
Raw Value: 48520 Wet-Dry Level: DRY
Pump State: ON
Raw Value: 47235 Wet-Dry Level: DRY
Raw Value: 46032 Wet-Dry Level: DRY
Raw Value: 45047 Wet-Dry Level: DRY
Raw Value: 44047 Wet-Dry Level: WET
Pump State: OFF
Raw Value: 44047 Wet-Dry Level: WET
Raw Value: 44031 Wet-Dry Level: WET
Raw Value: 44023 Wet-Dry Level: WET
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
Raspberry Pi Code - Application 2
We'll make the irrigation system work like this:
Detailed Instructions
"""
This Raspberry Pi code was developed by newbiely.com
This Raspberry Pi code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-irrigation
"""
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
# Create an ADS1115 ADC object
adc = Adafruit_ADS1x15.ADS1115()
# Set the gain to ±4.096V (adjust if needed)
GAIN = 1
# Single threshold for wet/dry classification (adjust as needed)
THRESHOLD = 45000
# GPIO pin connected to the relay module to control the pump
RELAY_PIN = 12
# Function to determine the wet-dry level based on the soil moisture percentage
def wet_dry_level(soil_moisture):
if soil_moisture > THRESHOLD:
return "DRY"
else:
return "WET"
# Function to activate the pump for a specified duration
def activate_pump():
GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn on the pump
print("Pump State: ON")
time.sleep(5) # Keep the pump on for 5 seconds
GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the pump
print("Pump State: OFF")
# Setup GPIO mode and relay pin as output
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
# Main loop to read the analog value from the soil moisture sensor and control the pump
try:
while True:
# Read the raw analog value from channel A3
raw_value = adc.read_adc(3, gain=GAIN)
# Determine the wet-dry level based on the raw ADC value
level = wet_dry_level(raw_value)
# Print the results
print("Raw Value: {} \t Wet-Dry Level: {}".format(raw_value, level))
# If soil is wet, activate the pump
if level == "DRY":
activate_pump()
# Add a delay between readings (adjust as needed)
time.sleep(1)
except KeyboardInterrupt:
# Cleanup GPIO on exit
GPIO.cleanup()
print("\nExiting the program.")
python3 auto_irrigation_2.py
PuTTY - Raspberry Pi
Raw Value: 48520 Wet-Dry Level: DRY
Pump State: ON
Pump State: OFF
Raw Value: 42400 Wet-Dry Level: WET
Raw Value: 42600 Wet-Dry Level: WET
Raw Value: 42800 Wet-Dry Level: WET