0
0
Raspberry Piprogramming~10 mins

Automated plant watering system in Raspberry Pi

Choose your learning style9 modes available
Introduction

An automated plant watering system helps keep plants healthy by watering them when they need it without you having to remember.

When you want to water your plants regularly but often forget.
If you travel and cannot water your plants yourself.
To save water by only watering plants when the soil is dry.
For plants that need very specific watering schedules.
To learn how sensors and small computers can work together.
Syntax
Raspberry Pi
import time
import RPi.GPIO as GPIO

# Setup GPIO pins
GPIO.setmode(GPIO.BCM)
soil_sensor_pin = 17
water_pump_pin = 27

GPIO.setup(soil_sensor_pin, GPIO.IN)
GPIO.setup(water_pump_pin, GPIO.OUT)

while True:
    if GPIO.input(soil_sensor_pin) == GPIO.LOW:  # Soil is dry
        GPIO.output(water_pump_pin, GPIO.HIGH)  # Turn pump on
        time.sleep(5)  # Water for 5 seconds
        GPIO.output(water_pump_pin, GPIO.LOW)   # Turn pump off
    else:
        GPIO.output(water_pump_pin, GPIO.LOW)   # Soil is wet, pump off
    time.sleep(60)  # Check every minute

This code uses the Raspberry Pi's GPIO pins to read a soil moisture sensor and control a water pump.

GPIO.LOW means the soil is dry in this example, so the pump turns on to water the plant.

Examples
Set the soil sensor pin as input and the water pump pin as output.
Raspberry Pi
GPIO.setup(soil_sensor_pin, GPIO.IN)
GPIO.setup(water_pump_pin, GPIO.OUT)
If the soil sensor reads dry, turn the water pump on.
Raspberry Pi
if GPIO.input(soil_sensor_pin) == GPIO.LOW:
    GPIO.output(water_pump_pin, GPIO.HIGH)
Water the plant for 5 seconds, then turn the pump off.
Raspberry Pi
time.sleep(5)
GPIO.output(water_pump_pin, GPIO.LOW)
Sample Program

This program checks the soil moisture every minute. If the soil is dry, it waters the plant for 5 seconds. It prints messages to show what it is doing. You can stop it safely with Ctrl+C.

Raspberry Pi
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
soil_sensor_pin = 17
water_pump_pin = 27

GPIO.setup(soil_sensor_pin, GPIO.IN)
GPIO.setup(water_pump_pin, GPIO.OUT)

try:
    while True:
        if GPIO.input(soil_sensor_pin) == GPIO.LOW:
            print("Soil is dry. Watering plant...")
            GPIO.output(water_pump_pin, GPIO.HIGH)
            time.sleep(5)
            GPIO.output(water_pump_pin, GPIO.LOW)
            print("Watering done.")
        else:
            print("Soil is wet. No watering needed.")
            GPIO.output(water_pump_pin, GPIO.LOW)
        time.sleep(60)
except KeyboardInterrupt:
    print("Program stopped by user.")
    GPIO.cleanup()
OutputSuccess
Important Notes

Make sure to connect the soil moisture sensor and water pump to the correct GPIO pins.

Use a relay or transistor to safely control the water pump from the Raspberry Pi.

Always clean up GPIO pins with GPIO.cleanup() when your program ends.

Summary

An automated plant watering system uses sensors and a pump to water plants only when needed.

It saves time and water by checking soil moisture regularly.

Raspberry Pi GPIO pins can read sensors and control devices like pumps easily.