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

💧 Smart Water Management System

(ESP32 + Ultrasonic Sensor + Relay + Bluetooth)

In this lesson, we upgrade the previous Serial Monitor version to Bluetooth control.

This system will:

  • 📏 Measure water level using ultrasonic sensor

  • 📊 Calculate tank percentage

  • 🔄 Support AUTO mode

  • 🎮 Support MANUAL mode

  • 📱 Control motor from mobile app

  • 🚨 Send FULL tank warning

No WiFi required ❌
No Internet required ❌
Bluetooth control ✔


📦 Components Used

  • ESP32

  • HC-SR04 Ultrasonic Sensor

  • Single Channel Relay

  • DC Water Pump

  • Android Phone

  • Serial Bluetooth Terminal App

Recommended App:
👉 Serial Bluetooth Terminal (Android)


🔌 Pin Configuration

🔹 Ultrasonic Sensor

Ultrasonic Pin ESP32
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

(Bluetooth + Ultrasonic + Auto/Manual)

Copy & paste into Arduino IDE:

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 26

long duration;
float distance;
float tankHeight = 30.0;
float waterLevel;
float percentage;

bool autoMode = true;
bool motorState = false;

void setup() {
Serial.begin(115200);
SerialBT.begin("Smart_Water_System");

pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);

digitalWrite(RELAY_PIN, HIGH);

SerialBT.println("=== Smart Water Management ===");
SerialBT.println("Commands: AUTO, MANUAL, ON, OFF");
}

void loop() {

// Ultrasonic trigger
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;

SerialBT.print("Tank Level: ");
SerialBT.print(percentage);
SerialBT.println(" %");

// Read Bluetooth command
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n');
command.trim();

if (command.equalsIgnoreCase("AUTO")) {
autoMode = true;
SerialBT.println("Switched to AUTO Mode");
}
else if (command.equalsIgnoreCase("MANUAL")) {
autoMode = false;
SerialBT.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) {
SerialBT.println("Water LOW -> Motor ON");
turnMotorOn();
}

if (percentage > 90 && motorState) {
SerialBT.println("Tank FULL -> Motor OFF");
turnMotorOff();
SerialBT.println("WARNING: Tank Full!");
}
}

delay(2000);
}

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

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

🧠 Code Explanation


1️⃣ Bluetooth Initialization

 
 
BluetoothSerial SerialBT;
SerialBT.begin(“Smart_Water_System”);
 

Creates Bluetooth device named:

👉 Smart_Water_System


2️⃣ Ultrasonic Distance Calculation

 
 
distance = duration * 0.034 / 2;
 

Converts echo time into centimeters.


3️⃣ Tank Percentage Calculation

 
 
percentage = (waterLevel / tankHeight) * 100;
 

Gives water percentage inside tank.


4️⃣ AUTO Mode Logic

If tank < 20%
→ Motor ON

If tank > 90%
→ Motor OFF + Warning


5️⃣ MANUAL Mode Logic

Manual mode works only if:

 
 
autoMode = false
 

Commands:

Command Function
AUTO Enable automatic mode
MANUAL Enable manual mode
ON Motor ON
OFF Motor OFF

📱 How To Use Bluetooth App

1️⃣ Upload code
2️⃣ Turn ON Bluetooth
3️⃣ Open Serial Bluetooth Terminal
4️⃣ Connect to:

 
 
Smart_Water_System
 

5️⃣ Send commands:

 
 
AUTO
MANUAL
ON
OFF
 

📟 Example Output in App

 
 
Tank Level: 15 %
Water LOW -> Motor ON
Motor is ON

Tank Level: 92 %
Tank FULL -> Motor OFF
Motor is OFF
WARNING: Tank Full!

 

🎯 What Students Learn

  • Bluetooth communication

  • Ultrasonic distance measurement

  • Tank percentage calculation

  • Wireless motor control

  • Auto + Manual system design

  • Real-time monitoring


📌 Lesson Summary

In this lesson, students built a Bluetooth-controlled Ultrasonic Smart Water Management System.

The system:

✔ Measures water level without contact
✔ Calculates tank percentage
✔ Supports Auto + Manual control
✔ Prevents overflow
✔ Prevents dry run

Scroll to Top