Ukraine: we buy and sell these parts ourselves, order them on this site.
Some links are affiliate links: we may earn a commission at no extra cost to you.
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
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);
}
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.