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.

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
| Part | Type |
|---|---|
| MCP3008 and soil probe | MCP3008 + soil probe |
| Relay and pump | Relay module |
| Dry warning | LED |
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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Orange | MCP3008 and soil probe - 3.3 V | Pin 1 (3V3) |
| 2 | Black | MCP3008 and soil probe - GND | Pin 6 (GND) |
| 3 | Coloured | MCP3008 and soil probe - CLK | Pin 23 (GPIO11) |
| 4 | Coloured | MCP3008 and soil probe - MISO | Pin 21 (GPIO9) |
| 5 | Coloured | MCP3008 and soil probe - MOSI | Pin 19 (GPIO10) |
| 6 | Coloured | MCP3008 and soil probe - CS | Pin 24 (GPIO8) |
| 7 | Coloured | Relay and pump - IN | Pin 11 (GPIO17) |
| 8 | Black | Relay and pump - GND | Pin 9 (GND) |
| 9 | Red | Relay and pump - VCC | Pin 2 (5V) |
| 10 | Coloured | Dry warning - anode + | Pin 15 (GPIO22) |
| 11 | Black | Dry 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
- The probe gives a voltage that follows the water in the soil. The MCP3008 turns it into a number from 0 to 1.
- When the value falls below DRY_BELOW, the pump runs for exactly 3 seconds. Never leave a pump on until the soil reads wet: water takes time to reach the probe.
- The wait of 5 seconds after watering gives the soil time to soak. Without it the Pi would water again and flood the pot.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 230 V AC small water pump Needs a proper starter and an RCCB | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | MCP3008 8-channel 10-bit ADC (DIP-16) One chip can read 8 sensors | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | Capacitive soil moisture sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 13 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 14 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.