Home / Projects

Project: ESP32 plant watering reminder with deep sleep

A capacitive soil moisture sensor in a plant pot, read by an ESP32 that wakes every 30 minutes, measures, beeps if the soil is below 30 per cent, then goes back into deep sleep
Beginner

Forgetting to water plants is easy. This project puts a moisture sensor in the pot and an ESP32 checks it every 30 minutes. If the soil is too dry, a buzzer beeps, then again every two hours until you water it. Between checks the ESP32 goes into deep sleep, where almost everything inside it switches off. You'll learn how to calibrate a sensor, how the ESP32's analogue input works, and how deep sleep changes the way a sketch runs.

Difficulty: beginner. Time: about 45 minutes, plus a day to fine-tune. Works on: boards with the original ESP32 chip. Other ESP32s work with different pins.

What you need

  • An ESP32 development board, a USB data cable and a USB phone charger to run it from
  • A capacitive soil moisture sensor (v1.2 or similar, rated for 3.3 V)
  • A passive piezo buzzer
  • Two half-size breadboards and five jumper wires
Shopping list (6 items)

Some shop links are affiliate links (Amazon UK, Tooled Up): if you buy through one we may earn a small commission, at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. It never changes which products we recommend, and no shop has paid to be included.

Everything for this build, in one place. Each link goes to a page we checked had the part in stock.

  • 1×
    ESP32 development board (ESP32-WROOM-32, 30 pins)A pack of two ELEGOO boards with USB-C and a CP2102 USB chip, so the spare can go in your next project. The board is wide: put it across two breadboards side by side so both rows of pins have free holes.
  • 1×
    USB-A to USB-C data cableThe boards come without a cable. These Anker cables carry data as well as charge, which uploading needs.
  • 1×
    Capacitive soil moisture sensor (v1.2, 3.3 to 5.5 V)Capacitive, not the two-pronged resistive kind, which corrodes in weeks. This pack has five.
  • 1×
    Passive piezo buzzerPassive, not active: the code plays a tone, which needs a passive buzzer.
  • 2×
    Half-size breadboards (400 points)Two side by side, with the ESP32 straddling the gap between them.
  • 1×
    Jumper wire kit (male-male, male-female and female-female)40 of each kind. Male-female wires go from a Raspberry Pi's pins to a breadboard.
  • Free
    Arduino IDEThe free Arduino editor. Nothing to buy.

Each button opens that shop's page for the part, checked when we wrote the list; if one shop is out of stock, try the next. Any equivalent part with the same value works.

How the sensor works

A capacitive sensor has no bare metal in the soil. Its copper tracks are under a protective coating, and they form a capacitor whose value rises as the soil around them gets wetter, because water stores far more charge than dry soil or air. A small circuit on the board turns this into a voltage: higher in dry soil, lower in wet soil. Cheap two-pronged resistive sensors pass current through the soil instead, and their prongs corrode away within weeks.

The ESP32 reads the voltage with its ADC (analogue-to-digital converter) as a number from 0 to 4095. The sketch uses GPIO 34, which is on ADC1. That matters: ADC2 pins stop working while Wi-Fi is on, so ADC1 is the safe habit. Our guide to ADCs explains more.

The circuit

PartPinESP32 pin
Soil sensorVCCGPIO 25 (powers it only while measuring)
Soil sensorGNDGND
Soil sensorAOUTGPIO 34
Buzzer+ (or the longer leg)GPIO 26
Buzzer−GND

The sensor draws only a few milliamps, so a GPIO pin can power it directly. The sketch switches it on for half a second to measure and off again, which saves power and stops the sensor sitting powered in wet soil all day.

Setting up the Arduino IDE for the ESP32

If you've never used an ESP32 with the Arduino IDE, follow our ESP32 first steps guide first. In short: add Espressif's boards manager address in the IDE's preferences, install esp32 by Espressif Systems from the Boards Manager, then choose ESP32 Dev Module and your board's port. If an upload stops at "Connecting...", hold the board's BOOT button until the upload starts.

The sketch

// Plant watering reminder: wakes every 30 minutes, measures the soil with a capacitive sensor,
// and beeps when the plant needs water. Deep sleep in between keeps the ESP32 cool and frugal.
const int SENSOR_POWER = 25;   // powers the sensor only while measuring
const int SENSOR_PIN = 34;     // an ADC1 input
const int BUZZER_PIN = 26;

const int DRY_READING = 2600;  // your sensor in dry air (see step 1)
const int WET_READING = 1100;  // your sensor in a glass of water
const int WATER_BELOW = 30;    // beep below this moisture, in %
const uint64_t SLEEP_MINUTES = 30;
const int BEEP_EVERY = 4;      // when dry, beep on every 4th wake (every 2 hours)

// RTC memory keeps its value through deep sleep. Everything else starts again from scratch.
RTC_DATA_ATTR int wakeCount = 0;
RTC_DATA_ATTR int wakesSinceBeep = 0;

int moisturePercent(int raw) {
  int percent = map(raw, DRY_READING, WET_READING, 0, 100);
  return constrain(percent, 0, 100);
}

void beep(int times) {
  for (int i = 0; i < times; i++) {
    tone(BUZZER_PIN, 2000);
    delay(150);
    noTone(BUZZER_PIN);
    delay(150);
  }
}

void setup() {
  Serial.begin(115200);
  wakeCount++;

  pinMode(SENSOR_POWER, OUTPUT);
  digitalWrite(SENSOR_POWER, HIGH);
  delay(500);  // give the sensor time to settle
  long sum = 0;
  for (int i = 0; i < 16; i++) {
    sum += analogRead(SENSOR_PIN);
    delay(5);
  }
  digitalWrite(SENSOR_POWER, LOW);
  int raw = sum / 16;
  int percent = moisturePercent(raw);
  Serial.printf("Wake %d: reading %d, moisture %d%%\n", wakeCount, raw, percent);

  if (percent < WATER_BELOW) {
    if (wakesSinceBeep == 0) beep(3);
    wakesSinceBeep = (wakesSinceBeep + 1) % BEEP_EVERY;
  } else {
    wakesSinceBeep = 0;  // watered: beep straight away next time it gets dry
  }

  esp_sleep_enable_timer_wakeup(SLEEP_MINUTES * 60 * 1000000ULL);
  esp_deep_sleep_start();
}

void loop() {
  // never reached: the ESP32 restarts from setup() each time it wakes
}

Calibrate your sensor

Every sensor, and every soil, gives slightly different numbers, so set yours up before you rely on it.

  1. Change SLEEP_MINUTES to 1 for now, upload, and open the Serial Monitor at 115200 baud. Each wake prints the raw reading.
  2. With the sensor held in the air, note the reading. Put it in DRY_READING.
  3. Stand the sensor in a glass of water up to the line printed on it, never deeper, and note the reading. Put it in WET_READING.
  4. Push the sensor into the plant's soil, water the plant, and watch the percentage over a few days. Water when the plant needs it, note the percentage, and set WATER_BELOW a little above it. Then set SLEEP_MINUTES back to 30.

How it works

  • Deep sleep restarts the program. After deep sleep, the ESP32 starts again from setup(), as if it had been reset. That's why all the work is in setup() and loop() is empty.
  • Remembering between wakes. Ordinary variables are lost in deep sleep. Variables marked RTC_DATA_ATTR are kept in a small memory that stays powered, so the sketch can count wakes and know when it last beeped. They are lost if the power is cut.
  • Averaging. Each measurement is the average of 16 readings, which smooths out noise.
  • Converting to a percentage. map() turns the raw reading into 0 to 100 % between your dry and wet readings, and constrain() keeps it inside that range.
  • Not nagging all night. When the soil is dry it beeps on the first wake, then every fourth wake (every two hours). Watering resets the count, so it beeps straight away the next time the soil dries out.
  • Tested. We ran this sketch on a simulated ESP32 through 14 wakes with the soil drying out, being watered and drying again, checking the percentages, the beep pattern and that the sensor is only powered while measuring.

Power: what deep sleep does and doesn't do

The ESP32 chip itself draws about 10 µA in deep sleep, according to Espressif's datasheet. A development board draws much more, because its USB chip, power LED and voltage regulator stay on. Most boards like this one draw several milliamps even while the ESP32 sleeps, which would flatten AA batteries in weeks. So run this project from a USB phone charger. Deep sleep still keeps the board cool and quiet. If you want to run from batteries, use a board designed for low power, and check its sleep current in its datasheet. Our battery life calculator does the sums.

Some USB power banks switch themselves off when the current is very low, so they may turn the project off after a while.

Troubleshooting

  • Always 0 % or always 100 %: your dry and wet readings are the wrong way round or not set. Repeat the calibration.
  • Nothing in the Serial Monitor after a wake: the board takes a moment to reconnect. Press the EN button to restart it while the Serial Monitor is open.
  • Readings drift over weeks: water may be getting into the electronics at the top of the sensor. Keep the board above the soil line, and coat the cut edges with nail varnish or hot glue.
  • An upload fails while the board is asleep: hold BOOT, press and release EN, let go of BOOT, then upload.

Take it further

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.