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 Bluetooth Terminal Code + VERY detailed line-by-line explanation.

This code will:

✅ Show on Serial Monitor + Bluetooth Terminal both
✅ Print:

  • Sensor ADC value

  • Moisture %

  • Soil: DRY / OK

  • Pump: ON / OFF

✅ Condition:

  • Moisture < 10% → Pump ON

  • Moisture ≥ 10% → Pump OFF


✅ 1) Bluetooth App (Use This)

📱 Android App:

Serial Bluetooth Terminal (best)

ESP32 Bluetooth Name:

ESP32_PLANT_MONITOR


✅ 2) ESP32 Bluetooth Auto Watering Code (COPY-PASTE)

⚡ Connections:

  • Soil Sensor AO → GPIO34

  • Relay IN → GPIO26

/************************************************************
SMART PLANT MONITORING + AUTO WATERING SYSTEM (BLUETOOTH)
Board: ESP32
Sensor: Soil Moisture Sensor (Analog)
Relay: 1 Channel Relay
Pump: DC Water Pump

Condition:
Moisture < 10% -> Pump ON
Moisture >= 10% -> Pump OFF

Output:
Serial Monitor + Bluetooth Terminal
************************************************************/

#include "BluetoothSerial.h" // Library for ESP32 Bluetooth Serial

BluetoothSerial SerialBT; // Create Bluetooth Serial object

// ----------- Pin Definitions -----------
#define SOIL_SENSOR_PIN 34 // Analog pin (ADC) for Soil Sensor AO
#define RELAY_PIN 26 // Digital pin for Relay IN

// ----------- Moisture Calibration Values -----------
int dryValue = 3500; // ADC value when soil is fully DRY (calibrate)
int wetValue = 1500; // ADC value when soil is fully WET (calibrate)

// ----------- Moisture Threshold -----------
int pumpThreshold = 10; // Moisture % threshold

// ----------- Pump State Variable -----------
bool pumpState = false; // false = OFF, true = ON

void setup()
{
// Serial Monitor Start
Serial.begin(115200);

// Bluetooth Start (ESP32 Name)
SerialBT.begin("ESP32_PLANT_MONITOR");

// Relay pin setup
pinMode(RELAY_PIN, OUTPUT);

// Pump OFF by default (Active LOW Relay)
digitalWrite(RELAY_PIN, HIGH);

// Print startup message in Serial Monitor
Serial.println("=======================================");
Serial.println("SMART PLANT MONITORING + AUTO WATERING");
Serial.println("Serial + Bluetooth Output Enabled");
Serial.println("Bluetooth Name: ESP32_PLANT_MONITOR");
Serial.println("=======================================");

// Print startup message in Bluetooth Terminal
SerialBT.println("=======================================");
SerialBT.println("SMART PLANT MONITORING + AUTO WATERING");
SerialBT.println("Serial + Bluetooth Output Enabled");
SerialBT.println("Bluetooth Name: ESP32_PLANT_MONITOR");
SerialBT.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 % -----------
// Map dryValue -> 0% and wetValue -> 100%
int moisturePercent = map(sensorValue, dryValue, wetValue, 0, 100);

// ----------- Step 3: Clamp Moisture % between 0 and 100 -----------
if (moisturePercent > 100) moisturePercent = 100;
if (moisturePercent < 0) moisturePercent = 0;

// ----------- Step 4: Create Output Message -----------
String message = "";
message += "ADC: ";
message += sensorValue;
message += " | Moisture: ";
message += moisturePercent;
message += "%";

// ----------- Step 5: Pump Control Logic -----------
if (moisturePercent < pumpThreshold)
{
// Soil is DRY -> Pump ON
pumpState = true;
digitalWrite(RELAY_PIN, LOW); // Active LOW relay -> ON

message += " | Soil: DRY";
message += " | Pump: ON";
}
else
{
// Soil is OK -> Pump OFF
pumpState = false;
digitalWrite(RELAY_PIN, HIGH); // Active LOW relay -> OFF

message += " | Soil: OK";
message += " | Pump: OFF";
}

// ----------- Step 6: Print Message to Serial + Bluetooth -----------
Serial.println(message);
SerialBT.println(message);

// ----------- Step 7: Delay -----------
delay(1000); // Update every 1 second
}

✅ 3) Output Example in Bluetooth Terminal

Dry soil:

 
ADC: 3820 | Moisture: 6% | Soil: DRY | Pump: ON

Wet soil:

 
ADC: 1750 | Moisture: 62% | Soil: OK | Pump: OFF

✅ 4) VERY IMPORTANT: Relay Working

Most relay modules are Active LOW:

ESP32 Output Relay Pump
LOW ON ON
HIGH OFF OFF

So we use:

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

✅ 5) FULL DETAILED CODE EXPLANATION (Line-By-Line)

Now I explain every part in detail.


✅ A) Bluetooth Library Include

 
#include "BluetoothSerial.h"

This line adds ESP32 Bluetooth Serial support.

Without this, ESP32 cannot send data to Bluetooth Terminal.


✅ B) Create Bluetooth Object

 
BluetoothSerial SerialBT;

This creates a Bluetooth communication object.

Just like Serial Monitor uses Serial,
Bluetooth uses SerialBT.


✅ C) Pin Definitions

 
#define SOIL_SENSOR_PIN 34
#define RELAY_PIN 26

Meaning:

  • Soil sensor analog output is connected to GPIO34

  • Relay input is connected to GPIO26


✅ D) Calibration Values

 
int dryValue = 3500;
int wetValue = 1500;

These values depend on:

  • soil type

  • sensor type

  • moisture

  • sensor quality

Meaning:

  • dryValue → sensor reading when soil is completely dry

  • wetValue → sensor reading when soil is fully wet


✅ E) Moisture Threshold

 
int pumpThreshold = 10;

This is your main condition.

  • Moisture < 10% → Pump ON

  • Moisture ≥ 10% → Pump OFF


✅ F) Pump State Variable

 
bool pumpState = false;

This is used to store pump ON/OFF state.


✅ setup() Function Explanation


1) Serial Start

 
Serial.begin(115200);

Starts serial monitor.


2) Bluetooth Start

 
SerialBT.begin("ESP32_PLANT_MONITOR");

Starts Bluetooth and sets ESP32 Bluetooth name.

In mobile Bluetooth list, you will see:

ESP32_PLANT_MONITOR


3) Relay Pin Output

 
pinMode(RELAY_PIN, OUTPUT);

Relay pin must be output because ESP32 sends signal.


4) Pump OFF by Default

 
digitalWrite(RELAY_PIN, HIGH);

This ensures pump is OFF when ESP32 starts.


5) Print Startup Messages

Serial Monitor

 
Serial.println("SMART PLANT MONITORING...");

Bluetooth Terminal

 
SerialBT.println("SMART PLANT MONITORING...");

So both will show the project title.


✅ loop() Function Explanation (Main Logic)


Step 1: Read Sensor ADC

 
int sensorValue = analogRead(SOIL_SENSOR_PIN);

Reads soil sensor analog value.

ESP32 gives ADC output:
0 to 4095


Step 2: Convert ADC to Moisture %

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

This converts ADC value into percentage.

  • dryValue → 0%

  • wetValue → 100%


Step 3: Clamp moisture

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

Because map() can produce negative or >100.

So we fix it.


Step 4: Create message string

 
String message = "";
message += "ADC: ";
message += sensorValue;
...

This builds one full line message.

Example:

 
ADC: 2980 | Moisture: 23%

Step 5: Pump ON/OFF decision

Condition line:

 
if (moisturePercent < pumpThreshold)

Meaning:
If moisture is less than 10% → soil is dry.


Pump ON block

 
digitalWrite(RELAY_PIN, LOW);
message += " | Pump: ON";

LOW triggers relay → pump ON.


Pump OFF block

 
digitalWrite(RELAY_PIN, HIGH);
message += " | Pump: OFF";

HIGH stops relay → pump OFF.


Step 6: Print to Serial + Bluetooth

 
Serial.println(message);
SerialBT.println(message);

This prints same output in:

  • Serial Monitor

  • Bluetooth Terminal


Step 7: Delay

 
delay(1000);

Update output every 1 second.


✅ 6) Calibration Steps (Must Do)

If you want accurate moisture %:

Step 1: Dry soil reading

  • Keep sensor in dry soil

  • Note ADC value

  • Put in dryValue

Step 2: Wet soil reading

  • Put sensor in water / wet soil

  • Note ADC value

  • Put in wetValue


✅ 7) If You Want Pump OFF Only After Reaching 20% (Better)

Right now, pump will stop at 10%.

But in real projects, we use:

  • Pump ON at 10%

  • Pump OFF at 25%

This prevents relay ON/OFF fast.

If you want this improved version, tell me 😄

Scroll to Top