Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

💧 Smart Water Management System (Serial Monitor Version)

Using:

  • ESP32

  • Water Level Sensor (Analog)

  • Single Channel Relay

  • DC Water Pump

This version will work in:

  • 🔄 AUTO Mode (Automatic motor control)

  • 🎮 MANUAL Mode (Control via Serial commands)


🔌 Connections

1️⃣ Water Level Sensor (Analog Mode)

Sensor Pin ESP32 Pin
VCC 3.3V
GND GND
AO GPIO 34

2️⃣ Relay Module

Relay Pin ESP32 Pin
VCC 5V
GND GND
IN GPIO 26

⚠ Relay controls pump externally (12V line).


💻 COMPLETE FINAL CODE (Serial Monitor Version)

Copy & paste:

// Pin Definitions
#define WATER_SENSOR 34
#define RELAY_PIN 26

int waterValue = 0;
bool autoMode = true; // Default AUTO mode
bool motorState = false; // Motor OFF initially

// Set your tank thresholds (adjust after testing)
int lowLevel = 1000;
int fullLevel = 3000;

void setup() {
Serial.begin(115200);

pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Motor OFF (Relay active LOW)

Serial.println("=== Smart Water Management System ===");
Serial.println("Commands:");
Serial.println("AUTO -> Enable Auto Mode");
Serial.println("MANUAL -> Enable Manual Mode");
Serial.println("ON -> Motor ON (Manual Mode)");
Serial.println("OFF -> Motor OFF (Manual Mode)");
Serial.println("------------------------------------");
}

void loop() {

// Read water sensor
waterValue = analogRead(WATER_SENSOR);

Serial.print("Water Level Value: ");
Serial.println(waterValue);

// Check Serial Commands
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim();

if (command.equalsIgnoreCase("AUTO")) {
autoMode = true;
Serial.println("Switched to AUTO Mode");
}
else if (command.equalsIgnoreCase("MANUAL")) {
autoMode = false;
Serial.println("Switched to MANUAL Mode");
}
else if (command.equalsIgnoreCase("ON") && !autoMode) {
turnMotorOn();
}
else if (command.equalsIgnoreCase("OFF") && !autoMode) {
turnMotorOff();
}
}

// AUTO Mode Logic
if (autoMode) {

if (waterValue < lowLevel && !motorState) {
Serial.println("Water LOW -> Motor ON");
turnMotorOn();
}

if (waterValue > fullLevel && motorState) {
Serial.println("Tank FULL -> Motor OFF");
turnMotorOff();
Serial.println("WARNING: Tank Full!");
}
}

delay(2000);
}

void turnMotorOn() {
digitalWrite(RELAY_PIN, LOW); // Active LOW relay
motorState = true;
Serial.println("Motor is ON");
}

void turnMotorOff() {
digitalWrite(RELAY_PIN, HIGH);
motorState = false;
Serial.println("Motor is OFF");
}

🧠 CODE EXPLANATION (Clear & Simple)


1️⃣ Pin Definitions

 
 
#define WATER_SENSOR 34
#define RELAY_PIN 26
 
  • GPIO 34 → Water sensor analog input

  • GPIO 26 → Relay control


2️⃣ Threshold Values

 
 
int lowLevel = 1000;
int fullLevel = 3000;
 

These values decide:

  • Below 1000 → Tank Empty

  • Above 3000 → Tank Full

⚠ You must calibrate these using Serial Monitor readings.


3️⃣ Modes

 
 
bool autoMode = true;
 
  • true → Auto mode

  • false → Manual mode


4️⃣ Serial Commands

Available commands:

Command Function
AUTO Enable automatic control
MANUAL Enable manual control
ON Motor ON (Manual only)
OFF Motor OFF (Manual only)

5️⃣ Auto Mode Logic

 
 
if (waterValue < lowLevel)
 

If water is LOW → Motor ON

 
 
if (waterValue > fullLevel)
 

If tank FULL → Motor OFF + Warning


6️⃣ Relay Logic

Most relay modules are Active LOW:

  • LOW → Motor ON

  • HIGH → Motor OFF


📟 Example Serial Monitor Output

 
 
Water Level Value: 850
Water LOW -> Motor ON
Motor is ON

Water Level Value: 3200
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!

 

🎯 What Students Learn From This Version

  • Analog sensor reading

  • Threshold comparison

  • Auto vs Manual logic

  • Relay control

  • Real-time monitoring

  • Embedded automation


📌 Important Calibration Step

  1. Upload code

  2. Observe waterValue when tank empty

  3. Observe waterValue when tank full

  4. Adjust:

 
 
int lowLevel = ???;
int fullLevel = ???;
 

🚀 Next Upgrade Options

Shiv 👑 You can now upgrade to:

  • 📊 Percentage level calculation

  • 📱 Bluetooth version

  • 🌐 WiFi Dashboard version

  • ☁ Blynk IoT version

  • 📈 Water level graph

  • 🔔 Buzzer alert system



Scroll to Top