How to make Water Tank Level Monitoring System Using ESP32 and Ultrasonic Sensor

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.

IoT Based Smart Water Level Monitoring System using ESP32 - Hackster.io

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.ComponentQuantityPurpose
1ESP32 DevKit1Main controller
2HC-SR04 ultrasonic sensor1Measures distance to water
3Breadboard1Circuit assembly
4Jumper wiresAs neededElectrical connections
5LEDs (red, yellow, green)3Water-level indicators
6220 Ω resistors3Limit LED current
71 kΩ resistor1Echo voltage divider
82 kΩ resistor1Echo voltage divider
9USB cable1Programming and power
10Water storage tank1Demonstration 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.

Learn HC-SR04 Wiring and Voltage Divider with ESP32

Pin connection table

HC-SR04 / ComponentESP32 Pin
HC-SR04 VCCVIN / 5V
HC-SR04 GNDGND
HC-SR04 TRIGGPIO 5
HC-SR04 ECHOGPIO 18 through voltage divider
Green LEDGPIO 25 through 220 Ω
Yellow LEDGPIO 26 through 220 Ω
Red LEDGPIO 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.

Ultrasonic Sensor Working Principle, Uses & Arduino Guide – Robocraze

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.
iOS, Android, Web | Auckland |TankMate

9. Water Level Indication System

Three LEDs can be used to show the amount of water remaining in the tank.

LED colourWater levelMeaning
Red0–20%Low water level
YellowAbove 20–70%Moderate water level
GreenAbove 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.

Jual Paket IoT Monitoring Tangki Air + Pompa Otomatis ESP32 | Kontrol via Android | Shopee Indonesia
Water Tank and Water Pump Automation by Using ESP32 - Arduino Expert
TankMate R3 Tank Level Sensor - WiFi – Artesian & Solway Water
ESP32 Smart Water Level Dashboard
Water Tank Management System using ESP32
Smart Monitoring | Mobi-Water
Water Level Monitoring With Automated Pump System - Wokwi ESP32, STM32, Arduino Simulator
IoT Based Water Level Controller Using ESP32 Blynk | Details | Hackaday.io
IoT Based Water Level Indicator using Ultrasonic Sensor 2026
TankMate R3 Tank Level Sensor - WiFi
ESP32 Water Tank Monitoring
Wireless Water Tank Monitor - Aquatel - Aquatel - Wireless Fluid Management Technologies

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.

ProblemPossible causeSolution
Incorrect readingsPoor sensor positioningMount sensor vertically
No distance readingIncorrect Echo wiringCheck voltage divider and connections
ESP32 cannot connect to Wi-FiIncorrect credentialsVerify SSID and password
Water percentage exceeds 100%Incorrect tank heightCalibrate tank dimensions
Unstable readingsWater movement or reflectionsUse multiple readings and averaging
Dashboard unavailableWrong IP address or networkCheck 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.

  • mbeva

    Dominic Mbeva is a science teacher, experienced researcher, innovator, and creative technologist with expertise in STEM education, digital media, and scientific research. As a Kenya Science and Engineering Fair (KSEF) advisor and projects manager, he mentors young scientists, guiding them in developing award-winning innovations. He is also an IC Technorat, leading advancements in science and technology. Beyond education, Dominic is a skilled photographer and video editor, using visual storytelling to make science more engaging. His philosophy, “If you take care of minutes, hours will take care of themselves,” reflects his belief in consistent effort, strategic thinking, and innovation to drive success in both research and creativity.

    Related Posts

    RFID Smart Attendance System Using Arduino and Google Sheets (Cloud-Based Attendance Monitoring)

    RFID Smart Attendance System Using Arduino and Google Sheets (Cloud-Based Attendance Monitoring) Meta description: Learn how to build an RFID smart attendance system using Arduino, an RFID RC522 reader, ESP8266…

    Read more

    How to Build a Smart Garbage Monitoring System Using Arduino, Ultrasonic Sensor and IoT

    Learn how to build a smart garbage monitoring system using Arduino, an ultrasonic sensor and IoT. Includes circuit connections, source code and ThingSpeak dashboard setup. Main components: Arduino-compatible ESP8266 NodeMCU,…

    Read more

    Leave a Reply

    Your email address will not be published. Required fields are marked *