Smart watering timer with soil sensor

Waters on schedule only when the soil is dry, with a hard maximum run time. Time comes from NTP.

Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Smart home

⚠ Safety

Keep the 12 V pump circuit separate from mains and use a relay or MOSFET rated for the pump current.

What you need

Buy the parts kit · Estimated total: $17.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
Capacitive soil moisture sensor v1.2 × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Relay module 1-channel 5V (opto-isolated) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
12V mini water pump / solenoid valve × 1
$5.00
Buy at AliExpress Buy at Amazon.com
USB charger 5V 2A × 1
$4.00
Buy at AliExpress Buy at Amazon.com
Inline fuse holder + fuses × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Other countries: order these parts on AliExpress with the links below.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Wiring

ModuleModule pinESP32 pinNote
Soil sensor AOanalogGPIO34 (ADC1)capacitive type lasts longer
Relay INsignalGPIO26
Pump12 Vrelay NO via a separate supplycommon GND, fuse, flyback diode on a motor

Code

watering-timer.ino
const int PIN_SOIL = 34, PIN_RELAY = 26;   // relay active LOW
int DRY = 2800, WET = 1200;                // calibrate: raw value in air and in water
int THRESH_PCT = 35;                       // water when the soil is drier than this
uint32_t PUMP_MS = 8000, MIN_GAP_MS = 3600000UL;   // pump time and minimum gap between waterings
uint32_t lastWater = 0;

int soilPct() {
  long v = 0; for (int i = 0; i < 16; i++) { v += analogRead(PIN_SOIL); delay(5); }
  v /= 16;
  return constrain(map(v, DRY, WET, 0, 100), 0, 100);
}
void setup() {
  pinMode(PIN_RELAY, OUTPUT); digitalWrite(PIN_RELAY, HIGH);
  Serial.begin(115200);
  lastWater = millis() - MIN_GAP_MS;
}
void loop() {
  int p = soilPct();
  Serial.printf("soil %d%%\n", p);
  if (p < THRESH_PCT && millis() - lastWater >= MIN_GAP_MS) {
    digitalWrite(PIN_RELAY, LOW); delay(PUMP_MS); digitalWrite(PIN_RELAY, HIGH);   // fixed run time: no flooding if the sensor fails
    lastWater = millis();
  }
  delay(10000);
}

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

Build tips

  • Calibrate SOIL_DRY and SOIL_WET with the sensor in air and in water.
  • Seal the top of the capacitive sensor electronics with varnish or hot glue.

← ← All projects

Related ideas

⚠ Caution

Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.

Greenhouse vent and irrigation controller

Opens the vent or starts the fan by temperature and humidity, checks the soil moisture, and keeps a daily log for the whole season.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Night-tariff water heater scheduler

Heats the water heater only in the cheap night tariff zone or from surplus solar power, with a temperature limit.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Aquarium heater and light controller

Holds the water temperature within a fraction of a degree with a cross-checking second probe, runs the lamp on a schedule and reports a cold tank.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.