Build a Wi-Fi water tank level monitor with a Wemos D1 Mini and a waterproof JSN-SR04T sensor that shows the level on an OLED and a phone web page and beeps when the tank is low or full.
Need it ready-made? Buy Ready Project
This Wemos D1 Mini water tank level monitor tells you how much water is in your overhead tank without climbing up to check. A waterproof JSN-SR04T ultrasonic sensor sits in the tank lid and measures the distance down to the water. The D1 Mini turns that into a percentage and litres, shows it on a small OLED display, serves a live tank gauge to any phone on your Wi-Fi, and beeps when the tank is nearly empty or about to overflow.
It is a practical intermediate project for homes, hostels and shops in India where rooftop tanks are filled by a motor. Because nothing touches the water except a sealed sensor head, there are no probes to rust, and the same build works for a 500 litre or a 2000 litre tank after you enter its depth.

How It Works
- Measuring the water: every 2 seconds the D1 Mini sends a 20 µs pulse on TRIG. The JSN-SR04T fires an ultrasonic ping and holds ECHO high until the echo comes back from the water surface. The pulse width divided by 58 gives the distance in centimetres. The sketch takes five readings and uses the middle one, which ignores ripples and stray echoes.
- Distance to level: you enter two numbers: the distance from the sensor to the water when the tank is full, and the distance to the tank floor. The code maps the measured distance between those two points to 0 to 100 percent and then to litres using the tank capacity.
- Blind zone: the JSN-SR04T cannot see anything closer than about 25 cm. Mount the sensor so the full water line stays at least 25 cm below its face, for example on a short pipe above the lid.
- Level shifting: the sensor runs on 5V and its ECHO output is 5V. The D1 Mini pins are 3.3V only, so a 1 kΩ and 2 kΩ divider drops the echo signal to about 3.3V. TRIG works directly from the 3.3V pin.
- Display and web page: the OLED shows the percentage in large digits, the litres, and a filling bar. The D1 Mini also runs a small web server at its IP address or
http://watertank.local. The page draws a tank that fills up and asks/dataevery 3 seconds for fresh numbers. - Alarm: below 15 percent the buzzer gives a slow beep, and above 95 percent it beeps fast so you can switch off the pump. The button or the Mute button on the web page silences it, and the alarm turns itself back on the next time the level state changes.
Components Required
- Wemos D1 Mini (ESP8266) – 1
- JSN-SR04T (or AJ-SR04M) waterproof ultrasonic sensor with 2.5 m cable – 1
- 0.96 inch SSD1306 I2C OLED display (128×64) – 1
- Active 5V buzzer – 1
- BC547 NPN transistor and 1 kΩ base resistor – 1 each
- 1 kΩ resistor and 2 kΩ resistor (echo voltage divider) – 1 each
- Push button (alarm mute) – 1
- 5V 1A USB power adapter and long USB cable – 1
- Small waterproof enclosure, cable glands and PVC pipe cap for the sensor – 1 set
- Perfboard or breadboard and jumper wires – 1 set
Wiring Table
| Part / pin | Wemos D1 Mini pin | Notes |
|---|---|---|
| JSN-SR04T 5V / GND | 5V / GND | Sensor board needs 5V |
| JSN-SR04T TRIG | D5 (GPIO14) | 3.3V trigger is enough |
| JSN-SR04T ECHO | D6 (GPIO12) through 1 kΩ, 2 kΩ from D6 to GND | Never connect ECHO directly |
| OLED VCC / GND | 3V3 / GND | |
| OLED SDA / SCL | D2 (GPIO4) / D1 (GPIO5) | I2C address 0x3C |
| BC547 base | D8 (GPIO15) through 1 kΩ | D8 has a pull-down, so the buzzer stays quiet at boot |
| BC547 emitter / collector | GND / buzzer – | Buzzer + goes to 5V |
| Mute button | D7 (GPIO13) and GND | Uses the internal pull-up |
Connection Diagram
The illustration at the top shows how the parts are arranged. It is a layout guide only, and the pin labels in it are not exact. Follow the pin-accurate drawing below, which matches the wiring table.
Mounting on the Tank
- Drill a hole in the tank lid that fits the JSN-SR04T head, or fix the head in a PVC end cap glued to a short pipe so it points straight down at the water.
- Keep the sensor away from the inlet pipe and the tank walls so falling water and the walls do not cause false echoes.
- Measure from the sensor face to the overflow level and to the tank floor with a tape, and put those numbers in
SENSOR_TO_FULL_CMandSENSOR_TO_EMPTY_CM. - Put the D1 Mini, OLED and buzzer in a waterproof box near the tank or run the sensor cable down to a box inside the house. The JSN-SR04T cable can be extended to about 5 m with shielded wire.
Code
Install the esp8266 by ESP8266 Community board package in the Arduino IDE and select LOLIN(WEMOS) D1 R2 & mini. From the Library Manager install Adafruit SSD1306 and Adafruit GFX Library. Replace the Wi-Fi placeholders with your own network name and password, set the tank distances and capacity, then upload.
/*
Wemos D1 Mini Wi-Fi Water Tank Level Monitor
JSN-SR04T waterproof ultrasonic sensor + 0.96" SSD1306 OLED + buzzer alarm + web gauge
Arduino Project Hub - ardiunoprojecthub.online
Pins (Wemos D1 Mini):
JSN-SR04T TRIG -> D5 (GPIO14)
JSN-SR04T ECHO -> 1k -> D6 (GPIO12), 2k from D6 to GND (5V echo -> 3.3V)
OLED SDA / SCL -> D2 (GPIO4) / D1 (GPIO5)
Buzzer driver -> D8 (GPIO15) -> 1k -> BC547 base (buzzer on collector)
Mute button -> D7 (GPIO13) to GND (internal pull-up)
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ---------- Wi-Fi (replace the placeholders) ----------
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
// ---------- Pins ----------
const uint8_t PIN_TRIG = D5;
const uint8_t PIN_ECHO = D6;
const uint8_t PIN_BUZZ = D8;
const uint8_t PIN_MUTE = D7;
// ---------- Tank settings (measure your own tank) ----------
const float SENSOR_TO_FULL_CM = 25.0; // distance from sensor face to water when the tank is FULL (>= 25 cm, the JSN-SR04T blind zone)
const float SENSOR_TO_EMPTY_CM = 125.0; // distance from sensor face to the tank floor (EMPTY)
const float TANK_LITRES = 1000.0; // total capacity, used to show litres
const int LOW_ALARM_PCT = 15; // beep when the level falls below this
const int HIGH_ALARM_PCT = 95; // beep when the tank is almost overflowing
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
ESP8266WebServer server(80);
float lastDistance = -1; // cm, -1 = no echo
int levelPct = 0;
bool muted = false;
int lastAlarmState = 0; // 0 none, 1 low, 2 high
unsigned long lastRead = 0, lastBeep = 0;
// One distance reading in cm, or -1 if no echo
float readOnce() {
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(5);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(20); // JSN-SR04T likes a longer trigger pulse
digitalWrite(PIN_TRIG, LOW);
unsigned long us = pulseIn(PIN_ECHO, HIGH, 30000UL); // ~5 m max
if (us == 0) return -1;
return us * 0.0343f / 2.0f;
}
// Median of 5 readings to ignore stray echoes from ripples
float readDistance() {
float v[5]; int n = 0;
for (int i = 0; i < 5; i++) {
float d = readOnce();
if (d > 0) v[n++] = d;
delay(60);
}
if (n == 0) return -1;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (v[j] < v[i]) { float t = v[i]; v[i] = v[j]; v[j] = t; }
return v[n / 2];
}
int distanceToPercent(float d) {
float span = SENSOR_TO_EMPTY_CM - SENSOR_TO_FULL_CM;
float pct = (SENSOR_TO_EMPTY_CM - d) * 100.0f / span;
if (pct < 0) pct = 0;
if (pct > 100) pct = 100;
return (int)(pct + 0.5f);
}
void drawOled() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("WATER TANK");
oled.setCursor(80, 0);
oled.print(muted ? "MUTED" : WiFi.isConnected() ? "WiFi" : "noWiFi");
if (lastDistance < 0) {
oled.setTextSize(2);
oled.setCursor(0, 24);
oled.print("NO ECHO");
} else {
oled.setTextSize(3);
oled.setCursor(0, 16);
oled.print(levelPct);
oled.print("%");
oled.setTextSize(1);
oled.setCursor(0, 46);
oled.print((int)(TANK_LITRES * levelPct / 100));
oled.print(" L");
if (levelPct <= LOW_ALARM_PCT) oled.print(" LOW!");
if (levelPct >= HIGH_ALARM_PCT) oled.print(" FULL!");
}
// vertical tank bar on the right
oled.drawRect(104, 12, 22, 50, SSD1306_WHITE);
int h = (lastDistance < 0) ? 0 : 46 * levelPct / 100;
oled.fillRect(106, 60 - h, 18, h, SSD1306_WHITE);
oled.display();
}
void handleRoot() {
String ip = WiFi.localIP().toString();
String page =
"<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width,initial-scale=1'>"
"<title>Water Tank</title><style>body{font-family:Arial;text-align:center;background:#eef4fb;margin:0;padding:20px}"
".tank{width:140px;height:260px;border:4px solid #1565c0;border-radius:10px;margin:20px auto;position:relative;background:#fff;overflow:hidden}"
"#w{position:absolute;bottom:0;width:100%;background:#42a5f5;transition:height 1s}"
"#p{font-size:42px;font-weight:bold}button{padding:10px 18px;font-size:16px;border:0;border-radius:6px;background:#1565c0;color:#fff}</style></head>"
"<body><h2>Water Tank Level</h2><div class='tank'><div id='w' style='height:0'></div></div>"
"<div id='p'>--%</div><div id='l'></div><p id='s'></p><button onclick=\"fetch('/mute')\">Mute / Unmute alarm</button>"
"<script>function u(){fetch('/data').then(r=>r.json()).then(d=>{"
"document.getElementById('w').style.height=d.pct+'%';"
"document.getElementById('p').textContent=d.ok?d.pct+'%':'No echo';"
"document.getElementById('l').textContent=d.litres+' litres, '+d.cm+' cm to water';"
"document.getElementById('w').style.background=d.pct<=" + String(LOW_ALARM_PCT) + "?'#e53935':'#42a5f5';"
"document.getElementById('s').textContent=(d.muted?'Alarm muted':'Alarm on');})}"
"setInterval(u,3000);u();</script></body></html>";
server.send(200, "text/html", page);
}
void handleData() {
String j = "{\"ok\":" + String(lastDistance > 0 ? "true" : "false") +
",\"pct\":" + String(levelPct) +
",\"litres\":" + String((int)(TANK_LITRES * levelPct / 100)) +
",\"cm\":" + String(lastDistance, 1) +
",\"muted\":" + String(muted ? "true" : "false") + "}";
server.send(200, "application/json", j);
}
void handleMute() {
muted = !muted;
server.send(200, "text/plain", muted ? "muted" : "alarm on");
}
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(PIN_BUZZ, OUTPUT);
digitalWrite(PIN_BUZZ, LOW);
pinMode(PIN_MUTE, INPUT_PULLUP);
Wire.begin(D2, D1);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) Serial.println("OLED not found at 0x3C");
oled.clearDisplay(); oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0); oled.print("Connecting Wi-Fi..."); oled.display();
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < 15000) delay(250);
if (WiFi.isConnected()) {
Serial.print("Open http://"); Serial.println(WiFi.localIP());
MDNS.begin("watertank"); // http://watertank.local
} else {
Serial.println("Wi-Fi not connected, running offline (OLED + buzzer still work)");
}
server.on("/", handleRoot);
server.on("/data", handleData);
server.on("/mute", handleMute);
server.begin();
}
void loop() {
server.handleClient();
MDNS.update();
// Mute button (simple debounce)
static bool lastBtn = HIGH;
bool btn = digitalRead(PIN_MUTE);
if (lastBtn == HIGH && btn == LOW) { muted = !muted; delay(30); }
lastBtn = btn;
if (millis() - lastRead > 2000) {
lastRead = millis();
lastDistance = readDistance();
if (lastDistance > 0) levelPct = distanceToPercent(lastDistance);
Serial.printf("Distance %.1f cm, level %d %%\n", lastDistance, levelPct);
int state = 0;
if (lastDistance > 0 && levelPct <= LOW_ALARM_PCT) state = 1;
if (lastDistance > 0 && levelPct >= HIGH_ALARM_PCT) state = 2;
if (state != lastAlarmState) { muted = false; lastAlarmState = state; } // un-mute when the alarm changes
drawOled();
}
// Alarm: short beeps for LOW, fast beeps for FULL
if (lastAlarmState != 0 && !muted) {
unsigned long period = (lastAlarmState == 2) ? 400 : 1500;
if (millis() - lastBeep > period) {
lastBeep = millis();
digitalWrite(PIN_BUZZ, HIGH); delay(120); digitalWrite(PIN_BUZZ, LOW);
}
}
}
Testing
- Open the Serial Monitor at 115200 baud and note the IP address after “Open http://”.
- Before fitting the sensor on the tank, point it at a wall 50 cm and 100 cm away and check that the distance readings are close.
- Mount it on the tank and compare the percentage with the real water level you see.
- Let the level drop below 15 percent (or temporarily raise
LOW_ALARM_PCT) and check the slow beep, then press the button to mute it. - Open the web page on your phone and make sure the tank picture follows the OLED.
Troubleshooting
| Problem | Fix |
|---|---|
| OLED or web page shows No echo | Check the 5V supply to the sensor and the ECHO divider. Make sure the sensor head points straight down and is not touching anything. |
| Readings stuck near 20 to 25 cm | The water is inside the blind zone. Raise the sensor on a pipe so the full level is at least 25 cm below it. |
| Level jumps while the pump runs | Falling water and splashes reflect the sound. Move the sensor away from the inlet or ignore readings while filling. |
| D1 Mini does not boot with the buzzer circuit | Use D8 only through the 1 kΩ resistor to the transistor base. Do not add a pull-up on D8, as it must be low at boot. |
| Web page does not open | The phone must be on the same 2.4 GHz Wi-Fi. Use the IP address if watertank.local does not work on Android. |
| Wrong percentage | Re-measure the full and empty distances from the sensor face, not from the lid. |
Ideas to Extend
- Add a relay module to switch the pump on at 20 percent and off at 95 percent for a fully automatic tank.
- Send a phone notification with ntfy when the tank is low.
- Log daily water use by recording the level every hour.