This project uses an ESP32 and JSN-SR04T ultrasonic sensor to monitor water tank levels in real time. The sensor measures the distance from the water surface to a fixed point above the tank, then calculates the fill percentage based on calibrated empty and full distances. The guide includes a wiring diagram showing how to connect the sensor to the ESP32, a complete parts list with resistor values, and step-by-step assembly instructions for mounting the sensor head above the water.
The firmware provided takes multiple distance measurements, filters out unreliable readings, and outputs both the raw distance and calculated water level percentage to the serial monitor. Builders will learn how to calibrate the sensor for their specific tank dimensions, handle ultrasonic echo timing, and implement median filtering for stable readings. This setup works for tanks up to 4 meters deep and requires only basic soldering skills.
Wiring diagram

Gather all the parts
Assemble it in 2 steps
1. توصيل الحساس مع فصل USB
استخدم الحساس في وضع TRIG/ECHO. صل VCC إلى 5V وGND إلى GND وTRIG إلى GPIO25. صل ECHO إلى مقاومة 10 kΩ ثم إلى GPIO26. صل مقاومة 15 kΩ بين GPIO26 وGND.
- لا تصل ECHO مباشرة باللوحة؛ الإشارة 5 فولت وقد تتلف المدخل.
2. تثبيت رأس الحساس فوق الماء
وجّه الرأس عمودياً إلى سطح الماء بعيداً عن الجدار ومدخل التعبئة. أبقِ الرأس على بعد 30 سم على الأقل من أعلى مستوى للماء. قس المسافة من وجه الحساس عند مستويي الفراغ والامتلاء لتستخدمها في المعايرة.
- أبقِ لوحة الحساس وESP32 ومصدر USB في مكان جاف خارج الخزان. لا تغمر رأس الحساس. لا تدخل الخزان للتركيب.
Review all connections
1. Connections between "sensor" and "ESP32"
2. Connections between "r1" and "ESP32"
3. Connections between "r2" and "ESP32"
Deploy the firmware
#include <Arduino.h>
constexpr int TRIG=25, ECHO=26;
// Example only: measure from the sensor face to each reference level.
constexpr float EMPTY_CM=150, FULL_CM=35;
static_assert(FULL_CM>=25 && EMPTY_CM<=400 && EMPTY_CM>FULL_CM,"Invalid calibration");
void setup(){
Serial.begin(115200);
pinMode(TRIG,OUTPUT); digitalWrite(TRIG,LOW);
pinMode(ECHO,INPUT);
}
void loop(){
float samples[5]; int n=0;
for(int i=0;i<5;i++){
digitalWrite(TRIG,HIGH); delayMicroseconds(20); digitalWrite(TRIG,LOW);
float cm=pulseIn(ECHO,HIGH,30000UL)*0.01715f;
if(cm>=25 && cm<=400){
int j=n++; while(j>0 && samples[j-1]>cm){samples[j]=samples[j-1]; --j;}
samples[j]=cm;
}
delay(100);
}
if(n<3) Serial.println("No reliable echo - water level unknown");
else {
float cm=(samples[(n-1)/2]+samples[n/2])*0.5f;
float percent=constrain(100.0f*(EMPTY_CM-cm)/(EMPTY_CM-FULL_CM),0.0f,100.0f);
Serial.printf("Distance: %.1f cm | Water level: %.0f %%\n",cm,percent);
}
delay(1000);
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




