Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 16 of 47 · Home automation · Intermediate

Automatic plant watering

A soil moisture probe tells the Pi when the plant is dry. The Pi runs a small water pump for three seconds, then waits for the water to soak in.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Automatic plant watering: the board, its power supply and the parts wired to the 40-pin header
CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.

Parts in this circuit

PartType
MCP3008 and soil probeMCP3008 + soil probe
Relay and pumpRelay module
Dry warningLED

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1OrangeMCP3008 and soil probe - 3.3 VPin 1 (3V3)
2BlackMCP3008 and soil probe - GNDPin 6 (GND)
3ColouredMCP3008 and soil probe - CLKPin 23 (GPIO11)
4ColouredMCP3008 and soil probe - MISOPin 21 (GPIO9)
5ColouredMCP3008 and soil probe - MOSIPin 19 (GPIO10)
6ColouredMCP3008 and soil probe - CSPin 24 (GPIO8)
7ColouredRelay and pump - INPin 11 (GPIO17)
8BlackRelay and pump - GNDPin 9 (GND)
9RedRelay and pump - VCCPin 2 (5V)
10ColouredDry warning - anode +Pin 15 (GPIO22)
11BlackDry warning - cathode -Pin 14 (GND)

Python code (gpiozero)

from gpiozero import MCP3008, OutputDevice, LED
from time import sleep

soil = MCP3008(channel=0)               # 0 = bone dry, 1 = soaking wet (simplified)
pump = OutputDevice(17, active_high=False)
warn = LED(22)

DRY_BELOW = 0.30

while True:
    moisture = soil.value
    print("Soil moisture:", round(moisture * 100), "%")
    if moisture < DRY_BELOW:
        warn.on()
        print("Too dry: pump ON for 3 seconds")
        pump.on()
        sleep(3)
        pump.off()
        print("Waiting for the water to soak in...")
        sleep(5)
    else:
        warn.off()
        sleep(1)

How the code works

You will learn

  • Reading a soil moisture sensor
  • Timed actions: pump on, wait, pump off
  • Why the loop must wait after watering

Try this

  • Drag the Soil moisture slider down to 20 % and watch the pump.
  • Raise the slider to 80 % during the soak wait: does the program notice?
  • Add a safety counter: stop watering after 3 pumps in a row and print a "Tank empty?" warning.

On a real Pi

  • Real probes often read HIGH when dry. Test yours in a glass of water and flip the formula (1 - soil.value) if needed.
  • Use a capacitive probe: cheap resistive probes corrode in a few weeks.
  • Power the pump from its own supply through the relay. Keep every electrical part away from the water.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
61-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
7230 V AC small water pump
Needs a proper starter and an RCCB
1Robu.in Amazon.in ThinkRobotics
8Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
93-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
10MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
11MCP3008 8-channel 10-bit ADC (DIP-16)
One chip can read 8 sensors
1Robu.in Amazon.in ThinkRobotics
12Capacitive soil moisture sensor1Robu.in Amazon.in ThinkRobotics
13Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
14Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 15. Thermostat fan with hysteresisAll projects17. Smart door lock →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.