Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Projects

Project: A Plant Monitor with Automatic Watering

Build a Raspberry Pi plant monitor: read soil moisture through an ADC, drive a pump with a relay, use hysteresis and safety limits, and log watering events in Python.

Plants die from neglect and from too much attention. A Pi can measure soil moisture and give water only when needed, but only if the control logic is careful. A naive rule, "if the soil is dry, run the pump", floods the pot: the water takes minutes to spread, the sensor still reads dry, and the pump keeps running. Good controllers add hysteresis, minimum waiting times and safety limits.

The sensor and pump need hardware, so wiring and gpiozero code are read-only. The controller is plain Python, and you can run and test it here against a simulated pot.

Hardware

The Pi has no analog inputs, so a capacitive soil moisture sensor (analog output) needs an ADC such as the ADS1115 (I2C) or MCP3008 (SPI). Capacitive sensors are better than the cheap two-prong resistive type, which corrode within weeks.

  • Soil sensor output to ADC channel A0; sensor powered from 3.3 V.
  • A relay module or a MOSFET board on GPIO 20 switches a small 5 V or 12 V water pump from its own supply. Never power a pump from a Pi GPIO pin.
  • A flyback diode across motors and pumps if the driver board does not include one.
  • Keep electronics away from water: put the pump in the reservoir and the Pi above it, in a box, with drip loops in the tubes.

Reading the ADS1115 with Adafruit's library looks like this:

Python
import board, busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
chan = AnalogIn(ads, ADS.P0)
print(chan.value)       # raw 16-bit number

Calibration

Raw numbers mean nothing until you calibrate. Measure the sensor in dry air (value DRY) and in a glass of water (WET); capacitive sensors read lower when wetter. Then map any reading to a 0-100 percent moisture:

Python Convert raw ADC values to a moisture percentage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The min/max clamp makes noise outside the calibration range harmless. Soil is not a glass of water: expect "perfectly wet soil" to read 70 to 80 percent, so choose thresholds from experiments with your own plant.

Hysteresis and safety rules

A single threshold makes a controller chatter. With hysteresis there are two: start watering below 30 percent, and stop only when moisture exceeds 45 percent. In practice, you also water in short bursts and wait for the soil to absorb the water before measuring again.

Safety rules the controller must enforce:

  • Maximum pump run per burst (for example 10 seconds).
  • Minimum time between bursts (for example 30 minutes).
  • A daily limit (for example 60 seconds of pumping per day).
  • A sensor fault check: a reading near 0 or at the maximum raw value probably means a broken wire, and should disable the pump.
Python The watering controller

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Walk through the output. The first reading pumps; ten minutes later it waits even though it is still dry; at t=2000 it pumps again. At 50 percent nothing is done, and a raw value of 50 is rejected as a fault, not obeyed.

Testing against a simulated pot

You should never test a controller on a live plant. A tiny simulation of the pot reveals whether the numbers work: water added raises moisture slowly with a delay, and evaporation lowers it over time.

Python Simulate a pot for 24 hours

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A simulation is crude, but it shows the controller keeps the plant between thresholds without flooding. Adjust the burst length and the pause and see how the count changes.

Logging and alerts

Record every reading and pump event in SQLite with timestamps (as in the weather station). Then you can chart moisture and notice that a plant is drinking faster in a heat wave. Send yourself a message when the reservoir is empty. A cheap way to detect that: a float switch on a GPIO pin, read before every burst, with the pump disabled when the switch says empty so that it never runs dry.

flowchart TD
    A["Read ADC"] --> B["Convert to percent"]
    B --> C{"Sensor fault?"}
    C -- yes --> D["Disable pump and alert"]
    C -- no --> E{"Below LOW and allowed?"}
    E -- yes --> F["Pump one burst, log it"]
    E -- no --> G["Wait and read again"]

Every cycle checks safety before it decides to water.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the controller use two thresholds (30 and 45 percent)?

    Hysteresis creates a dead band so small fluctuations do not cause chatter.
  2. 2. Why wait between pump bursts?

    Measuring right after pumping still shows dry soil near the sensor.
  3. 3. Why is an ADC needed for an analog soil sensor?

    GPIO pins read only high or low; an ADC converts a voltage to a number.
  4. 4. What should the controller do when the raw sensor value is impossible?

    A fault means the data cannot be trusted; failing safe prevents a flood.

Summary

  • Analog sensors need an ADC (ADS1115 over I2C, MCP3008 over SPI); prefer capacitive soil sensors.
  • Calibrate dry and wet readings and clamp the percentage.
  • Use hysteresis, burst limits, pauses and daily caps; fail safe on faults.
  • Simulate the pot before connecting a pump.
  • Log events and alert when the water runs out.

Next: Scheduling Jobs with cron and systemd Timers