💧 Smart Water Management System
(ESP32 + Ultrasonic Sensor + Relay – Serial Monitor Version)
In this lesson, we will:
-
Measure water level using Ultrasonic Sensor
-
Calculate tank water percentage
-
Implement AUTO mode logic
-
Implement MANUAL mode logic
-
Control motor using Serial Monitor commands
-
Prevent overflow
-
Prevent dry run
No Bluetooth ❌
No WiFi ❌
Only Serial Monitor ✔
📦 Components Used
-
ESP32
-
HC-SR04 Ultrasonic Sensor
-
Single Channel Relay
-
DC Water Pump
🔌 Pin Configuration
🔹 Ultrasonic Sensor
| Ultrasonic Pin | ESP32 Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| Trig | GPIO 5 |
| Echo | GPIO 18 |
⚠ Use voltage divider for Echo (5V → 3.3V)
🔹 Relay Module
| Relay Pin | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| IN | GPIO 26 |
Relay Active LOW
LOW → Motor ON
HIGH → Motor OFF
💻 COMPLETE CODE
(Serial Monitor Version – Ultrasonic)
Copy & paste into Arduino IDE:
#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 26
long duration;
float distance;
float tankHeight = 30.0; // Tank height in cm
float waterLevel;
float percentage;
bool autoMode = true;
bool motorState = false;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Motor OFF
Serial.println("=== Smart Water Management System ===");
Serial.println("Commands: AUTO, MANUAL, ON, OFF");
}
void loop() {
// Trigger Ultrasonic Pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
waterLevel = tankHeight - distance;
if (waterLevel < 0) waterLevel = 0;
if (waterLevel > tankHeight) waterLevel = tankHeight;
percentage = (waterLevel / tankHeight) * 100;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Water Level: ");
Serial.print(waterLevel);
Serial.print(" cm | Tank: ");
Serial.print(percentage);
Serial.println(" %");
// 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 (percentage < 20 && !motorState) {
Serial.println("Water LOW -> Motor ON");
turnMotorOn();
}
if (percentage > 90 && motorState) {
Serial.println("Tank FULL -> Motor OFF");
turnMotorOff();
Serial.println("WARNING: Tank Full!");
}
}
delay(2000);
}
void turnMotorOn() {
digitalWrite(RELAY_PIN, LOW);
motorState = true;
Serial.println("Motor is ON");
}
void turnMotorOff() {
digitalWrite(RELAY_PIN, HIGH);
motorState = false;
Serial.println("Motor is OFF");
}
🧠 Code Explanation (Step-by-Step)
1️⃣ Ultrasonic Trigger Logic
delayMicroseconds(10);
Sends 10µs pulse.
2️⃣ Measure Echo Time
Measures time taken for sound to return.
3️⃣ Calculate Distance
Converts time into centimeters.
4️⃣ Calculate Water Level
percentage = (waterLevel / tankHeight) * 100;
Gives tank water percentage.
5️⃣ Auto Mode Logic
If:
→ Motor ON
If:
→ Motor OFF + Warning
6️⃣ Manual Mode Commands
| Command | Function |
|---|---|
| AUTO | Enable automatic mode |
| MANUAL | Enable manual mode |
| ON | Motor ON |
| OFF | Motor OFF |
📟 Example Serial Output
Water LOW -> Motor ON
Motor is ON
Distance: 3 cm | Water Level: 27 cm | Tank: 90 %
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!
🎯 What Students Learn
-
Ultrasonic distance measurement
-
Percentage calculation
-
Tank height calibration
-
Auto + Manual control logic
-
Relay motor control
-
Embedded automation design
📌 Lesson Summary
In this lesson, students built a complete Ultrasonic-based Smart Water Management System using Serial Monitor.
This system:
✔ Measures water without contact
✔ Calculates tank percentage
✔ Automatically controls motor
✔ Prevents overflow and dry run