Meta Description: Learn how to build a smart water tank level monitoring system using ESP32 and an ultrasonic sensor. Measure water levels, monitor tank capacity and receive real-time updates through .
1. Introduction
Water management is an important aspect of homes, schools, industries, farms and other institutions. One common challenge is knowing how much water remains inside a storage tank without physically opening it or climbing to inspect the water level.
The Water Tank Level Monitoring System Using ESP32 and Ultrasonic Sensor is an innovative IoT-based project designed to solve this problem.
The system uses an HC-SR04 ultrasonic sensor to measure the distance between the sensor and the water surface. An ESP32 microcontroller processes the measurements and calculates the percentage of water available in the tank.
Through its built-in Wi-Fi capability, the ESP32 can also transmit water-level information to a web dashboard, allowing users to monitor their tanks using a smartphone or computer.
2. Objectives of the Project
The main objectives are:
- To design a smart water tank monitoring system using ESP32.
- To measure water levels using an ultrasonic sensor.
- To calculate the percentage of water available in a tank.
- To display water-level information in real time.
- To transmit water-level information through Wi-Fi.
- To reduce water wastage caused by tank overflow.
- To improve water management in homes, schools and agricultural institutions.
3. Components and Materials Required
The following components are required to construct the project.
| No. | Component | Quantity | Purpose |
|---|---|---|---|
| 1 | ESP32 DevKit | 1 | Main controller |
| 2 | HC-SR04 ultrasonic sensor | 1 | Measures distance to water |
| 3 | Breadboard | 1 | Circuit assembly |
| 4 | Jumper wires | As needed | Electrical connections |
| 5 | LEDs (red, yellow, green) | 3 | Water-level indicators |
| 6 | 220 Ω resistors | 3 | Limit LED current |
| 7 | 1 kΩ resistor | 1 | Echo voltage divider |
| 8 | 2 kΩ resistor | 1 | Echo voltage divider |
| 9 | USB cable | 1 | Programming and power |
| 10 | Water storage tank | 1 | Demonstration tank |
Optional components include an LCD display, buzzer and relay-controlled water pump.
4. How the Water Tank Monitoring System Works
The system operates using ultrasonic sound waves.
The HC-SR04 ultrasonic sensor is mounted at the top of the water tank, facing downwards towards the water surface.
The sensor sends an ultrasonic pulse towards the water. When the pulse encounters the water surface, it is reflected back to the sensor.
The time taken for the sound wave to travel to the water surface and return is measured.
The ESP32 uses this information to calculate the distance between the sensor and the water surface.
The water level is then determined using the known height of the tank.
Working process:
- The ultrasonic sensor transmits sound waves.
- The sound waves hit the water surface.
- The reflected waves return to the sensor.
- The sensor calculates the travel time.
- The ESP32 calculates the water level.
- The water percentage is displayed on a web dashboard.
- LEDs indicate whether the tank is low, normal or full.
5. Circuit Diagram and ESP32 Connections
The ultrasonic sensor is connected to the ESP32 through its trigger and echo pins.
Pin connection table
| HC-SR04 / Component | ESP32 Pin |
|---|---|
| HC-SR04 VCC | VIN / 5V |
| HC-SR04 GND | GND |
| HC-SR04 TRIG | GPIO 5 |
| HC-SR04 ECHO | GPIO 18 through voltage divider |
| Green LED | GPIO 25 through 220 Ω |
| Yellow LED | GPIO 26 through 220 Ω |
| Red LED | GPIO 27 through 220 Ω |
Important electrical precaution: The HC-SR04 Echo pin produces a 5V signal, while ESP32 GPIO pins are designed for 3.3V logic. Never connect Echo directly to GPIO 18.
Use a voltage divider:
- Connect a 1 kΩ resistor between HC-SR04 Echo and GPIO 18.
- Connect a 2 kΩ resistor between GPIO 18 and GND.
- Ensure the sensor and ESP32 share a common ground.
This reduces the Echo signal to approximately 3.3V.
6. Mathematical Calculations
The system calculates the water level using the distance measured by the ultrasonic sensor.
Formula 1: Distance measurement
d=\frac{v\times t}{2}
Where:
- = Distance between sensor and water surface.
- = Speed of sound in air (approximately 343 m/s).
- = Time taken for the ultrasonic pulse to travel to the water and return.
Formula 2: Water height
L=H-d
Where:
- = Height of water.
- = Effective tank height measured from the sensor reference point to the tank bottom.
- = Distance from the sensor to the water surface.
Formula 3: Water level percentage
\text{Water Level}=\frac{H-d}{H}\times100
Example calculation
Suppose:
- Tank height = 200 cm.
- Distance from sensor to water surface = 50 cm.
Therefore:
L=200-50=150\text{ cm}\text{Percentage}=\frac{150}{200}\times100\boxed{75\%}
The tank is therefore 75% full.
7. ESP32 Arduino IDE Programming Code
The following program measures the water level, controls three indicator LEDs and creates a simple Wi-Fi web dashboard.
Before uploading the code:
- Install the ESP32 board package in Arduino IDE.
- Select the correct ESP32 development board.
- Replace the Wi-Fi name and password with your network credentials.
- Set the tank height according to your actual installation.
- Upload the program and open the Serial Monitor.
#include <WiFi.h>
#include <WebServer.h>
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
// Sensor pins
#define TRIG_PIN 5
#define ECHO_PIN 18
// LED pins
#define GREEN_LED 25
#define YELLOW_LED 26
#define RED_LED 27
// Tank height in centimetres
const float tankHeight = 200.0;
WebServer server(80);
float waterDistance = 0;
float waterLevel = 0;
float waterPercent = 0;
void measureWaterLevel() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if (duration == 0) {
return;
}
waterDistance = duration * 0.0343 / 2.0;
waterLevel = tankHeight - waterDistance;
waterLevel = constrain(
waterLevel, 0, tankHeight
);
waterPercent =
(waterLevel / tankHeight) * 100.0;
// LED indicators
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(RED_LED, LOW);
if (waterPercent <= 20) {
digitalWrite(RED_LED, HIGH);
}
else if (waterPercent <= 70) {
digitalWrite(YELLOW_LED, HIGH);
}
else {
digitalWrite(GREEN_LED, HIGH);
}
Serial.print("Water Level: ");
Serial.print(waterPercent);
Serial.println("%");
}
void handleRoot() {
measureWaterLevel();
String page = "<html><head>";
page += "<meta name='viewport' ";
page += "content='width=device-width, initial-scale=1'>";
page += "<meta http-equiv='refresh' content='5'>";
page += "<title>Water Tank Monitor</title>";
page += "<style>";
page += "body{font-family:Arial;text-align:center;";
page += "background:#eaf3fa;padding:25px;}";
page += ".box{background:white;padding:25px;";
page += "border-radius:15px;max-width:400px;";
page += "margin:auto;}";
page += ".level{font-size:45px;color:#0879c9;}";
page += "</style></head><body>";
page += "<div class='box'>";
page += "<h2>Smart Water Tank Monitor</h2>";
page += "<div class='level'>";
page += String(waterPercent, 1);
page += "%</div>";
page += "<h3>Water Level</h3>";
page += "<p>Water Height: ";
page += String(waterLevel, 1);
page += " cm</p>";
page += "<p>Distance: ";
page += String(waterDistance, 1);
page += " cm</p>";
page += "</div></body></html>";
server.send(200, "text/html", page);
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi Connected!");
Serial.print("Open dashboard: http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
static unsigned long previousTime = 0;
if (millis() - previousTime >= 2000) {
previousTime = millis();
measureWaterLevel();
}
}
Note: This version provides a local Wi-Fi dashboard. Your smartphone or computer must be connected to the same Wi-Fi network as the ESP32. The sensor reading is also available through the Serial Monitor. For accurate measurements, position the sensor above the maximum water surface and calibrate the effective tank height.
8. How to Set Up the Water Monitoring Dashboard
After uploading the program:
- Open Arduino IDE Serial Monitor.
- Set the baud rate to 115200.
- Wait for the ESP32 to connect to Wi-Fi.
- Copy the IP address displayed in the Serial Monitor.
- Connect your smartphone to the same Wi-Fi network.
- Enter the ESP32 IP address in your browser.
The dashboard displays:
- Water level percentage.
- Actual water height in centimetres.
- Distance between the sensor and water surface.
- Automatically refreshed readings.
9. Water Level Indication System
Three LEDs can be used to show the amount of water remaining in the tank.
| LED colour | Water level | Meaning |
|---|---|---|
| Red | 0–20% | Low water level |
| Yellow | Above 20–70% | Moderate water level |
| Green | Above 70% | High water level |
This allows users to identify the water condition without opening the monitoring dashboard.
10. Possible Project Improvements
The basic project can be upgraded with additional components to improve its performance.
A. LCD Display Integration
An I2C 16×2 LCD can be added to display water level percentage and water height directly on the monitoring unit.
B. Automatic Water Pump Control
A relay module can be introduced to switch a water pump ON when the water level falls below a selected threshold and OFF when the tank reaches its upper limit.
The pump should have appropriate electrical protection and independent high- and low-level safety controls.
C. Mobile Notifications
The system can be connected to platforms such as Blynk or ThingSpeak to enable remote monitoring and notifications.
D. Water Overflow Alerts
A buzzer can be added to alert users when the tank reaches a critically high water level.
E. Multiple Tank Monitoring
Additional sensors and suitable network communication can be used to monitor several water storage tanks from one dashboard.
11. Applications of the Project
The Water Tank Level Monitoring System has several practical applications.
- Homes: Monitoring domestic water storage tanks.
- Schools: Monitoring water tanks in learning institutions.
- Hospitals: Improving water storage management.
- Agriculture: Monitoring water available for irrigation.
- Industries: Monitoring water storage facilities.
- Hostels: Monitoring shared water storage tanks.
- Commercial buildings: Reducing water overflow and unnecessary inspections.
12. Advantages of the System
- Provides real-time water-level information.
- Reduces the need for manual tank inspection.
- Helps prevent water overflow.
- Uses affordable and readily available electronic components.
- Supports wireless monitoring through Wi-Fi.
- Can be expanded with automatic pump control.
- Promotes efficient water conservation.
- Provides an excellent practical STEM learning project.
13. Limitations and Troubleshooting
Although the system is effective for demonstration and suitable installations, some challenges may affect its performance.
| Problem | Possible cause | Solution |
|---|---|---|
| Incorrect readings | Poor sensor positioning | Mount sensor vertically |
| No distance reading | Incorrect Echo wiring | Check voltage divider and connections |
| ESP32 cannot connect to Wi-Fi | Incorrect credentials | Verify SSID and password |
| Water percentage exceeds 100% | Incorrect tank height | Calibrate tank dimensions |
| Unstable readings | Water movement or reflections | Use multiple readings and averaging |
| Dashboard unavailable | Wrong IP address or network | Check Serial Monitor and Wi-Fi connection |
Safety and accuracy: The HC-SR04 is not waterproof. Keep the electronics protected from moisture and condensation. For permanent outdoor or industrial installations, use a suitable waterproof ultrasonic sensor. The simple height calculation also assumes a regular-sided tank; irregular tanks require volume calibration.
14. Conclusion
The Water Tank Level Monitoring System Using ESP32 and Ultrasonic Sensor demonstrates how modern electronics and Internet of Things technology can improve water management.
By combining an ultrasonic sensor with the ESP32 microcontroller, the project provides an affordable solution for measuring water levels, calculating tank capacity and monitoring water availability remotely.
The system can be further developed into a fully automated water management solution by integrating water pumps, mobile notifications, LCD displays and cloud-based monitoring platforms.
It is an excellent STEM project for students interested in electronics, programming, automation and environmental conservation.







