Illustration: a potted basil with a soil sensor, a pump in a glass of water, a tube to the pot, a relay and the board.Home and IoT
Illustration (made with AI): roughly what the finished project looks like.

Automatic Watering for a Potted Plant

A sensor checks the soil moisture. When the soil dries out, a relay switches on a pump and the plant waters itself.

Difficulty
Intermediate
Time
2 h
Age
12+
Parts
6, 6 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. The sensor

    Push the soil moisture sensor into the soil. Connect its VCC to 5V, GND to the board's GND and its analogue output (AO) to A0.

    The relay's IN can go to any free digital pin; here it is D7, and the code must use the same pin. The pump is powered from its own supply. D7 is the board's digital pin 7, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Soil sensor VCC → Arduino 5V
    • Soil sensor GND → Arduino GND
    • Soil sensor AO → Arduino A0
  2. Relay and pump

    Run the pump through the relay with its own power supply. Never connect the pump straight to a board pin. The relay's IN goes to D7.

    The relay's IN can go to any free digital pin; here it is D7, and the code must use the same pin. The pump is powered from its own supply. D7 is the board's digital pin 7, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Relay module VCC → Arduino 5V
    • Relay module GND → Arduino GND
    • Relay module IN → Arduino D7
    • Relay module COM → 5 V supply +
    • 5 V pump + → Relay module NO
    • 5 V supply − → 5 V pump −
  3. Upload the code and set the threshold

    Upload the code and open the Serial Monitor (9600). Read the values for wet and dry soil (dry soil gives the higher number) and put a number between them in DRY in the code. If the relay works the other way round (the pump is always on), swap RELAY_ON and RELAY_OFF. On an ESP32 connect the sensor to 3V3 and GPIO 34 and the relay's IN to GPIO 26, and set SOIL_PIN = 34 and RELAY_PIN = 26 in the code. An ESP32 reads from 0 to 4095, so measure DRY again. If the relay won't switch on a 3.3 V signal, use a logic level shifter.

    Arduino C++
    // Automatic plant watering: a soil moisture sensor on A0, a relay module's IN on D7.
    // When the soil is dry the pump runs for a few seconds, then the code waits
    // 10 minutes for the water to soak in before it measures again.
    
    const int SOIL_PIN = A0;    // the sensor's analogue output (AO)
    const int RELAY_PIN = 7;    // the relay module's IN
    const int RELAY_ON = LOW;   // most relay modules switch on with LOW;
    const int RELAY_OFF = HIGH; //   if yours is the other way round, swap these two
    
    int DRY = 450;              // above this the soil is dry: set it from your own readings
    const unsigned long PUMP_MS = 3000;              // how long the pump runs each time
    const unsigned long SOAK_MS = 10UL * 60 * 1000;  // 10 minutes for the water to soak in
    const unsigned long CHECK_MS = 2000;             // how often to measure otherwise
    
    // The average of ten readings, so one noisy value doesn't start the pump
    int readSoil() {
      long sum = 0;
      for (int i = 0; i < 10; i++) {
        sum += analogRead(SOIL_PIN);
        delay(10);
      }
      return sum / 10;
    }
    
    void setup() {
      digitalWrite(RELAY_PIN, RELAY_OFF);   // set the pin before it becomes an output, so the pump doesn't kick at start
      pinMode(RELAY_PIN, OUTPUT);
      Serial.begin(9600);
      Serial.println("Soil reading (higher = drier):");
    }
    
    void loop() {
      int soil = readSoil();
      Serial.println(soil);
    
      if (soil > DRY) {
        Serial.println("Dry: watering");
        digitalWrite(RELAY_PIN, RELAY_ON);
        delay(PUMP_MS);
        digitalWrite(RELAY_PIN, RELAY_OFF);
        Serial.println("Waiting 10 minutes for the water to soak in");
        delay(SOAK_MS);
      } else {
        delay(CHECK_MS);
      }
    }
  4. Try it

    Test the pump in a glass of water first, then on the plant. After watering the code waits 10 minutes for the water to soak in. If the plant gets too little or too much water, change PUMP_MS.

  • Home and IoT
    Beginner40 min10+

    A Room Thermometer on an LCD

    A DHT11 sensor measures the temperature and humidity, and a 16×2 display shows both, updating every two seconds.

    5/5 parts on the site
  • Home and IoT
    Intermediate1 h 30 min12+

    A Weather Station on Your Phone

    An ESP32 measures the room's temperature and humidity and serves a small web page you open in your phone's browser on the home Wi-Fi.

    4/4 parts on the site
  • Home and IoT
    Beginner30 min10+

    A Motion Alarm

    A PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.

    6/6 parts on the site