IntermediateSmart Environments

Automatic Plant Watering System

Start a small pump when the soil is dry and stop when the target moisture is reached.

Time
2–3 h
Estimated cost
~£35
Hardware
ESP32-DevKitC V4 (38-pin)

Example code — not yet validated

Try it in the browser

What you will build

A self-watering pot. When moisture drops below 30 % the pump runs in short bursts, waits for the water to soak in, and stops once moisture passes 60 %. A button gives a manual 2-second watering, and safety limits stop the pump if something goes wrong (empty reservoir, sensor pulled out).

How it works

The sensing is the same as Project 06. A MOSFET module on GPIO 26 switches the pump's separate 5 V supply. Three rules keep it safe:

  • Hysteresis: start below 30 %, stop above 60 %, so the pump never flickers on and off around one value.
  • Burst + soak: run at most 4 s, then wait 2 minutes for the water to reach the sensor before deciding again.
  • Daily cap: no more than 10 bursts in 24 h. If the cap is hit, the OLED shows CHECK WATER and the pump locks out until reset.

Parts

Part Qty Purpose Required
Everything from Project 06 — Sensing + display Yes
3–5 V submersible mini pump 1 Moves water Yes
Logic-level MOSFET module 1 Switches the pump Yes
1N5819 (or 1N4007) diode 1 Flyback protection Yes
5 V 2 A USB supply + USB-to-terminal adapter 1 Pump power Yes
Silicone tube 6–8 mm, ~1 m 1 Water delivery Yes
Reservoir (jar or bottle) 1 Water supply Yes
Tactile button 1 Manual watering Yes

Build steps

  1. Keep the Project 06 sensor and OLED wiring.
  2. MOSFET module IN → GPIO 26, module GND → ESP32 GND.
  3. Pump red wire → 5 V supply +; pump black wire → MOSFET output (–); supply – → MOSFET GND.
  4. Fit the diode across the pump terminals, stripe (cathode) to the + side.
  5. Button between GPIO 27 and GND.
  6. Dry test first: pump out of the water, press the button → you should hear it buzz for 2 s. Never run it dry for long.
  7. Place the pump in the reservoir and clip the tube end into the pot. Keep the ESP32 above and away from the water.
  8. Upload with your RAW_DRY / RAW_WET values from Project 06. Checkpoint: in dry soil the pump runs a burst, pauses, and stops once moisture passes 60 %.

Try it in the browser

Playground Simulation. A virtual plant, soil gauge, reservoir level and pump indicator. Drag the thresholds, "empty" the reservoir and watch the daily cap lock out the pump. Labelled as a logic simulation.

Open Simulator Playground

Code

Download automatic-plant-watering.ino

// Automatic Plant Watering — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x
// Libraries: Adafruit SSD1306, Adafruit GFX
// Add the Wi-Fi dashboard from Project 06 once this works.
#include <Wire.h>
#include <Adafruit_SSD1306.h>

const int SOIL_PIN = 34, PUMP_PIN = 26, BUTTON_PIN = 27;
int RAW_DRY = 3000, RAW_WET = 1300;          // from Project 06
const int START_BELOW = 30, STOP_ABOVE = 60;  // percent
const unsigned long BURST_MS = 4000;
const unsigned long SOAK_MS = 120000;         // 2 min
const unsigned long MANUAL_MS = 2000;
const int MAX_BURSTS_PER_DAY = 10;

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
bool watering = false;      // inside a watering cycle
bool pumpOn = false, locked = false;
unsigned long pumpStart = 0, lastBurstEnd = 0, dayStart = 0;
unsigned long runFor = 0;
int burstsToday = 0, pct = 0;

int readPct() {
  long s = 0; for (int i = 0; i < 20; i++) { s += analogRead(SOIL_PIN); delay(2); }
  return constrain(map(s / 20, RAW_DRY, RAW_WET, 0, 100), 0, 100);
}

void pump(bool on, unsigned long ms = 0) {
  pumpOn = on; digitalWrite(PUMP_PIN, on ? HIGH : LOW);
  if (on) { pumpStart = millis(); runFor = ms; burstsToday++; }
  else lastBurstEnd = millis();
}

void setup() {
  Serial.begin(115200);
  pinMode(PUMP_PIN, OUTPUT); digitalWrite(PUMP_PIN, LOW);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Wire.begin(21, 22);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
}

void loop() {
  pct = readPct();
  if (millis() - dayStart > 86400000UL) { dayStart = millis(); burstsToday = 0; locked = false; }
  if (burstsToday >= MAX_BURSTS_PER_DAY) locked = true;

  // stop a running burst on time (hard limit)
  if (pumpOn && millis() - pumpStart >= runFor) pump(false);

  // manual watering
  if (!pumpOn && !locked && digitalRead(BUTTON_PIN) == LOW) pump(true, MANUAL_MS);

  // automatic cycle with hysteresis
  if (pct < START_BELOW) watering = true;
  if (pct > STOP_ABOVE)  watering = false;
  if (watering && !pumpOn && !locked && millis() - lastBurstEnd > SOAK_MS)
    pump(true, BURST_MS);

  oled.clearDisplay();
  oled.setTextSize(3); oled.setCursor(0, 0); oled.printf("%d%%", pct);
  oled.setTextSize(2); oled.setCursor(0, 40);
  oled.print(locked ? "CHECK WATER" : pumpOn ? "Watering" : watering ? "Soaking" : "OK");
  oled.display();
  Serial.printf("pct=%d pump=%d bursts=%d locked=%d\n", pct, pumpOn, burstsToday, locked);
  delay(200);
}

Troubleshooting

Symptom Likely cause Check
ESP32 resets when the pump starts Pump noise / shared supply Separate 5 V supply, diode fitted, GNDs joined
Pump runs but no water Air lock or dry pump Prime the tube; pump fully submerged
Floods the pot Sensor far from the tube outlet Place the outlet near the sensor; shorten BURST_MS
"CHECK WATER" every day Cap too low for a large pot Raise MAX_BURSTS_PER_DAY carefully

Safety and limitations

Low-voltage (5 V) pumps only; no mains pumps in this build. Keep electronics above and away from water and use a drip tray. Test in a sink first. Do not leave unattended for long periods until you have watched it run for several days.

Checklist

Next project

Wi-Fi Robot Car: drive motors instead of a pump.

Next project: Wi-Fi Robot Car