Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

🌱 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:

 
digitalWrite(RELAY_PIN, LOW); // Pump ON
digitalWrite(RELAY_PIN, HIGH); // Pump OFF

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

 
#define SOIL_SENSOR_PIN 34
#define RELAY_PIN 26

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

 
int dryValue = 3500;
int wetValue = 1500;

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

 
int pumpThreshold = 10;

This is your main condition:

If moisture < 10%

➡ Pump ON

If moisture >= 10%

➡ Pump OFF


✅ Section E: Pump State Variable

 
bool pumpState = false;

This stores pump status.

  • false = OFF

  • true = ON

(Used for clarity)


✅ setup() Function Explanation

1) Start Serial Monitor

 
Serial.begin(115200);

This starts serial communication.

So we can see values in Serial Monitor.


2) Relay Pin Output

 
pinMode(RELAY_PIN, OUTPUT);

Relay is controlled by ESP32 output.


3) Pump OFF by Default

 
digitalWrite(RELAY_PIN, HIGH);

This turns relay OFF initially.

Because relay is Active LOW.


4) Print Project Title

 
Serial.println("SMART PLANT MONITORING + AUTO WATERING");

This prints project name on Serial Monitor.


✅ loop() Function Explanation (Main Logic)


✅ Step 1: Read Soil Sensor Value

 
int sensorValue = analogRead(SOIL_SENSOR_PIN);

This reads analog voltage from soil sensor.

ESP32 gives value between:

  • 0 to 4095


✅ Step 2: Convert to Moisture %

 
int moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);

This is most important line.

It converts ADC into percentage.

  • dryValue → 0%

  • wetValue → 100%

So it gives moisture percent.


✅ Step 3: Fix Moisture Range

 
if (moisturePercent > 100) moisturePercent = 100;
if (moisturePercent < 0) moisturePercent = 0;

Sometimes map() gives:

  • negative value

  • above 100 value

So we clamp it.


✅ Step 4: Print on Serial Monitor

 
Serial.print("Sensor ADC Value: ");
Serial.print(sensorValue);
Serial.print(" | Moisture: ");
Serial.print(moisturePercent);
Serial.print("%");

This prints:

Example:

 
Sensor ADC Value: 2980 | Moisture: 23%

✅ Step 5: Pump Control Logic

Main condition:

 
if (moisturePercent < pumpThreshold)

Means:

If moisture < 10%
➡ Soil is dry
➡ Pump ON


Pump ON Block

 
pumpState = true;
digitalWrite(RELAY_PIN, LOW);
  • LOW triggers relay

  • Pump starts


Print ON Status

 
Serial.print(" | Soil: DRY");
Serial.println(" | Pump: ON");

So output becomes:

 
Sensor ADC Value: 3900 | Moisture: 5% | Soil: DRY | Pump: ON

Pump OFF Block

 
pumpState = false;
digitalWrite(RELAY_PIN, HIGH);

This turns pump OFF.


Print OFF Status

 
Serial.print(" | Soil: OK");
Serial.println(" | Pump: OFF");

Output:

 
Sensor ADC Value: 1800 | Moisture: 60% | Soil: OK | Pump: OFF

✅ Step 6: Delay

 
delay(1000);

Updates serial monitor every 1 second.


✅ 4) Serial Monitor Output Example

Dry soil:

 
Sensor ADC Value: 3800 | Moisture: 6% | Soil: DRY | Pump: ON

Wet soil:

 
Sensor ADC Value: 1700 | Moisture: 65% | Soil: OK | Pump: OFF

✅ 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







Scroll to Top