Difficulty: Intermediate
Time Required: 2-3 hours
Cost: $15-25
ESP Board: ESP32
What You’ll Need
| Component | Approximate Cost | Where to Buy |
|---|---|---|
| ESP32 Dev Board | $5-8 | Amazon |
| Capacitive Soil Sensor | $3-5 | Amazon |
| 5V Water Pump | $4-6 | Amazon |
| 5V Relay Module | $2-4 | Amazon |
| Jumper Wires | $2 | Amazon |
| Water Container | $2-3 | Local store |
Circuit Diagram
| ESP32 Pin | Component | Notes |
|---|---|---|
| 3.3V | Soil Sensor VCC | Power |
| GND | Soil Sensor GND | Ground |
| GPIO 34 | Soil Sensor OUT | Analog read |
| 5V | Relay VCC | Power |
| GND | Relay GND | Ground |
| GPIO 26 | Relay IN | Control pump |
The Complete Code
/*********************************************************************
* ESP32 Smart Plant Watering System
*
* Monitors soil moisture and waters plants automatically
* Hardware: ESP32, Capacitive Soil Sensor, 5V Pump, Relay
*********************************************************************/
#include
#include
// ============== CONFIGURATION ==============
#define BLYNK_AUTH "YOUR_AUTH_TOKEN"
const char* wifi_ssid = "YOUR_WIFI";
const char* wifi_password = "YOUR_PASSWORD";
// ============== PINS ==============
#define SOIL_SENSOR_PIN 34 // ADC pin
#define PUMP_RELAY_PIN 26 // GPIO 26
#define MOISTURE_THRESHOLD 40 // Dry when below 40%
// ============== TIMING ==============
#define CHECK_INTERVAL 60000 // Check every 60 seconds
#define PUMP_DURATION 5000 // Water for 5 seconds
#define DRYING_PERIOD 3600000 // Wait 1 hour after watering
unsigned long lastCheck = 0;
unsigned long pumpStartTime = 0;
bool isWatering = false;
unsigned long dryingEndTime = 0;
// ============== SETUP ==============
void setup() {
Serial.begin(115200);
Serial.println(F("\n======================================"));
Serial.println(F("Smart Plant Watering System Starting..."));
Serial.println(F("======================================"));
pinMode(SOIL_SENSOR_PIN, INPUT);
pinMode(PUMP_RELAY_PIN, OUTPUT);
digitalWrite(PUMP_RELAY_PIN, HIGH); // Pump OFF
analogReadResolution(12);
connectToWiFi();
Blynk.begin(BLYNK_AUTH, wifi_ssid, wifi_password);
Serial.println(F("Setup complete!"));
}
// ============== MAIN LOOP ==============
void loop() {
Blynk.run();
if (millis() - lastCheck > CHECK_INTERVAL) {
lastCheck = millis();
checkMoisture();
}
// Check if watering period is over
if (isWatering && millis() - pumpStartTime > PUMP_DURATION) {
stopWatering();
}
// Check if drying period is over
if (dryingEndTime > 0 && millis() > dryingEndTime) {
dryingEndTime = 0;
Serial.println(F("Drying period complete. Ready to check again."));
}
}
// ============== FUNCTIONS ==============
void checkMoisture() {
if (dryingEndTime > 0) {
return; // Still in drying period
}
int moisture = analogRead(SOIL_SENSOR_PIN);
int moisturePercent = map(moisture, 0, 4095, 100, 0);
Serial.print(F("Moisture: "));
Serial.print(moisturePercent);
Serial.print(F("% (Raw: "));
Serial.print(moisture);
Serial.println(F(")"));
Blynk.virtualWrite(V0, moisturePercent);
if (moisturePercent < MOISTURE_THRESHOLD && !isWatering) {
Serial.println(F("Soil is dry! Starting pump..."));
startWatering();
}
}
void startWatering() {
isWatering = true;
pumpStartTime = millis();
digitalWrite(PUMP_RELAY_PIN, LOW); // Pump ON
Blynk.virtualWrite(V1, 1);
Serial.println(F("Pump ON"));
}
void stopWatering() {
isWatering = false;
digitalWrite(PUMP_RELAY_PIN, HIGH); // Pump OFF
dryingEndTime = millis() + DRYING_PERIOD;
Blynk.virtualWrite(V1, 0);
Serial.println(F("Pump OFF. Entering drying period..."));
}
void connectToWiFi() {
Serial.print(F("Connecting to WiFi"));
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" Connected!"));
}
// ============== END OF CODE ==============
Troubleshooting
| Problem | Solution |
|---|---|
| Pump not starting | Check relay wiring; relay is active LOW |
| Soil readings always 0 | Check sensor connection; ensure VCC is 3.3V or 5V |
| Pump runs continuously | Increase threshold or pump duration |
Frequently Asked Questions
Q: How do I determine the threshold?
A: Read moisture when soil is dry and when wet. Set threshold between the two values.
Q: Can I use a 12V pump?
A: Use a relay rated for the pump voltage and a separate power supply.

