💧 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 RELAY_PIN 26
-
GPIO 34 → Water sensor analog input
-
GPIO 26 → Relay control
2️⃣ Threshold Values
int fullLevel = 3000;
These values decide:
-
Below 1000 → Tank Empty
-
Above 3000 → Tank Full
⚠ You must calibrate these using Serial Monitor readings.
3️⃣ Modes
-
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 water is LOW → Motor ON
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 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
-
Upload code
-
Observe waterValue when tank empty
-
Observe waterValue when tank full
-
Adjust:
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