🌱 Smart Plant Monitoring + Automatic Watering System
Using ESP32 + Soil Moisture Sensor + Relay + Water Pump
ESP32 code + very detailed line-by-line explanation :
✅ Serial Monitor shows:
-
Moisture %
-
Status: DRY / OK
-
Pump: ON / OFF
✅ Condition:
-
Moisture < 10% → Pump ON
-
Moisture ≥ 10% → Pump OFF
✅ 1) ESP32 Auto Watering Code (Copy-Paste)
⚡ Connections used in this code:
-
Soil Sensor AO → GPIO34
-
Relay IN → GPIO26
/************************************************************
SMART PLANT MONITORING + AUTO WATERING SYSTEM
Board: ESP32
Sensor: Soil Moisture Sensor (Analog)
Relay: 1 Channel Relay
Pump: DC Water Pump
Condition:
Moisture < 10% -> Pump ON
Moisture >= 10% -> Pump OFF
Serial Monitor Output:
Moisture Value + Moisture Percentage + Pump Status
************************************************************/
// ----------- Pin Definitions -----------
#define SOIL_SENSOR_PIN 34 // Analog pin (ADC) for Soil Sensor AO
#define RELAY_PIN 26 // Digital pin for Relay IN
// ----------- Moisture Settings -----------
int dryValue = 3500; // ADC value when soil is fully DRY (you will calibrate)
int wetValue = 1500; // ADC value when soil is fully WET (you will calibrate)
int pumpThreshold = 10; // Moisture % threshold (10%)
// ----------- Pump Control Variables -----------
bool pumpState = false; // false = OFF, true = ON
void setup()
{
Serial.begin(115200); // Start Serial Monitor at 115200 baud rate
pinMode(RELAY_PIN, OUTPUT); // Relay pin as output
digitalWrite(RELAY_PIN, HIGH);
// Most relay modules are ACTIVE LOW:
// HIGH = OFF, LOW = ON
Serial.println("=======================================");
Serial.println("SMART PLANT MONITORING + AUTO WATERING");
Serial.println("ESP32 + Soil Sensor + Relay + Pump");
Serial.println("=======================================");
}
void loop()
{
// ----------- Step 1: Read Soil Sensor ADC Value -----------
int sensorValue = analogRead(SOIL_SENSOR_PIN); // Reads 0 to 4095
// ----------- Step 2: Convert ADC Value to Moisture % -----------
// We map DRY ADC to 0% and WET ADC to 100%
int moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);
// ----------- Step 3: Limit the moisture value between 0 and 100 -----------
if (moisturePercent > 100) moisturePercent = 100;
if (moisturePercent < 0) moisturePercent = 0;
// ----------- Step 4: Print Sensor Values on Serial Monitor -----------
Serial.print("Sensor ADC Value: ");
Serial.print(sensorValue);
Serial.print(" | Moisture: ");
Serial.print(moisturePercent);
Serial.print("%");
// ----------- Step 5: Pump Control Logic -----------
if (moisturePercent < pumpThreshold)
{
// Soil is DRY -> Pump ON
pumpState = true;
digitalWrite(RELAY_PIN, LOW); // ACTIVE LOW relay -> LOW = ON
Serial.print(" | Soil: DRY");
Serial.println(" | Pump: ON");
}
else
{
// Soil is OK -> Pump OFF
pumpState = false;
digitalWrite(RELAY_PIN, HIGH); // HIGH = OFF
Serial.print(" | Soil: OK");
Serial.println(" | Pump: OFF");
}
// ----------- Step 6: Delay -----------
delay(1000); // Update every 1 second
}
✅ 2) VERY IMPORTANT NOTE (Relay Active LOW / Active HIGH)
Most relay modules work like this:
| Relay Input | Pump |
|---|---|
| LOW | ON |
| HIGH | OFF |
That’s why I wrote:
If your relay is opposite, tell me I’ll change it.
✅ 3) FULL DETAILED CODE EXPLANATION (Line by Line)
✅ Section A: Header Comment
This is just a comment block for project information.
It does not affect code.
✅ Section B: Pin Definitions
Meaning:
-
Soil sensor AO connected to GPIO34
-
Relay IN connected to GPIO26
GPIO34 is a good analog pin because it is input-only and stable.
✅ Section C: Calibration Values
Meaning:
These values depend on your sensor + soil.
-
When sensor is in dry soil, ADC becomes high (~3500)
-
When sensor is in wet soil, ADC becomes low (~1500)
⚠ These are sample values.
✅ Section D: Threshold
This is your main condition:
If moisture < 10%
➡ Pump ON
If moisture >= 10%
➡ Pump OFF
✅ Section E: Pump State Variable
This stores pump status.
-
false = OFF
-
true = ON
(Used for clarity)
✅ setup() Function Explanation
1) Start Serial Monitor
This starts serial communication.
So we can see values in Serial Monitor.
2) Relay Pin Output
Relay is controlled by ESP32 output.
3) Pump OFF by Default
This turns relay OFF initially.
Because relay is Active LOW.
4) Print Project Title
This prints project name on Serial Monitor.
✅ loop() Function Explanation (Main Logic)
✅ Step 1: Read Soil Sensor Value
This reads analog voltage from soil sensor.
ESP32 gives value between:
-
0 to 4095
✅ Step 2: Convert to Moisture %
This is most important line.
It converts ADC into percentage.
-
dryValue → 0%
-
wetValue → 100%
So it gives moisture percent.
✅ Step 3: Fix Moisture Range
Sometimes map() gives:
-
negative value
-
above 100 value
So we clamp it.
✅ Step 4: Print on Serial Monitor
This prints:
Example:
✅ Step 5: Pump Control Logic
Main condition:
Means:
If moisture < 10%
➡ Soil is dry
➡ Pump ON
Pump ON Block
-
LOW triggers relay
-
Pump starts
Print ON Status
So output becomes:
Pump OFF Block
This turns pump OFF.
Print OFF Status
Output:
✅ Step 6: Delay
Updates serial monitor every 1 second.
✅ 4) Serial Monitor Output Example
Dry soil:
Wet soil:
✅ 5) IMPORTANT: Calibration (Must Do)
To get correct moisture %:
Step 1: Put sensor in dry soil
Note sensor ADC value → put in dryValue
Step 2: Put sensor in water / wet soil
Note sensor ADC value → put in wetValue