Switch only low-voltage loads yourself. For mains loads use a certified isolated module in a box or call an electrician.
The sensor that decides how long the plants live
Resistive moisture probes corrode in weeks. Use capacitive ones and record the two reference values yourself: the reading in dry air and the reading in a glass of water. Everything between those two numbers is your percentage.
A pump should never trust one sensor
If a probe slips out of the soil it reads "dry" forever and the pump floods the balcony. So combine rules: water only inside a time window, only if the moisture is below the threshold, never longer than a set number of seconds per run and a set number of runs per day. Add a tank check with a float switch or an ultrasonic sensor, because a pump running dry burns out.
Put a flyback diode across a 12 V pump and drive it through a MOSFET rather than a mechanical relay if you can.
Morning is the best time to water; evening watering invites fungus in humid weather.
If you use deep sleep, wake up once an hour, read the probe, and decide. Constant polling just wastes battery.
Components
ESP32 DevKit V1 (30 pin, WROOM-32)
Capacitive soil moisture sensor v1.2
Relay module 1-channel 5V (opto-isolated)
12V mini water pump / solenoid valve
USB charger 5V 2A
Inline fuse holder + fuses
Wiring
Module
Module pin
ESP32 pin
Note
Soil sensor AO
analog
GPIO34 (ADC1)
capacitive type lasts longer
Relay IN
signal
GPIO26
Pump
12 V
relay NO via a separate supply
common GND, fuse, flyback diode on a motor
Code
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);
}
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.
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.
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.
Comments
No comments yet. Share your experience or ask a question.