The water tank monitor: know how much water is left, and hear about it before the tank runs dry.
A tank runs dry at night. The pump keeps running and burns out, and in the morning the school or the clinic has no water. Or the float valve sticks and the tank overflows for hours. Nobody knows until someone opens a tap.
Build a small monitor for that tank. It measures the water every second, turns it into litres, and warns the right person in time. It also shows what nobody measures today: how much water the site uses each day, and how much it loses at night.
What you end with
A tank that reports its litres every minute and sends an alert when it runs low, when it overflows, when it leaks at night, and when the monitor itself goes quiet. The record shows the water used each day, the hours left at today's rate, and how long the pump takes to fill it.
What you need
Rough prices for single parts bought online, in
Get the parts wherever suits you: a local electronics shop, or online. Pick a shop and each Find it link searches there.
| # | Part | Qty | About |
|---|---|---|---|
| 1 | ESP32 DevKitAny 30 or 38 pin boardFind it | 1 | KSh 650USD 5.00 |
| 2 | JSN-SR04T ultrasonic sensorWaterproof probe on a 2.5 m cable, with its driver board. The AJ-SR04M works the sameFind it | 1 | KSh 585USD 4.50 |
| 3 | Float switchFor the pump circuit itself, fitted by an electricianFind it | 1 | KSh 390USD 3.00 |
| 4 | Relay module, 1 channelFor the fill pump's control circuit; input that switches at 3.3 VFind it | 1 | KSh 195USD 1.50 |
| 5 | Flow meter (optional)YF-S201 or similar, 1 to 30 L/min, on the outletFind it | 1 | KSh 455USD 3.50 |
| 6 | Resistors1 k, 2 k, 10 k, 20 kFind it | 4 | KSh 26USD 0.20 |
| 7 | PG7 cable glandsFor 3 to 6.5 mm cablesFind it | 3 | KSh 195USD 1.50 |
| 8 | Jumper wiresFemale to femaleFind it | 15 | KSh 195USD 1.50 |
| 9 | USB cable and 5 V adapter1 A or more; the cable to match your boardFind it | 1 | KSh 520USD 4.00 |
| 10 | Screws4 x M3 x 10, 4 x M4 x 12, 2 for the wallFind it | 10 | KSh 130USD 1.00 |
| 11 | PETG or ASAOutdoors PLA softens in the sunFind it | 160 g | KSh 416USD 3.20 |
| Total | KSh 3,757USD 29 |
Also: a 3D printer, a 35 mm hole saw for the tank lid, a tape measure, silicone sealant, and an account on siliconwit.io (free).

LEVEL_ULTRASONIC to 0 and wire it as the drawing shows.The design, in 3D and on paper
Turn it, open Parts to hide the lid or the hood, and use Section to cut through the box and the probe holder. The parts carry the numbers of the parts list.

Build it
Each step links to the lesson section that teaches it. Open one only when you need it.
Tick each step as you finish it. Your place is kept, so you can stop and come back.
Kept on this device. Sign in to keep it on every device.Kept with your account.
Wire it on the desk
Wire it on the desk with jumper wires first, and fit it on the tank only when it works.
- The JSN-SR04T's echo is a 5 V pulse. The 1 k and 2 k divider brings it to 3.3 V for the ESP32.
- The transmitter's 4 to 20 mA through 150 ohm gives 0.6 to 3.0 V: inside what the ESP32's ADC reads well. Below 3.6 mA the firmware calls it a broken wire, not an empty tank.
- The ESP32's ADC is not straight at its ends. Read 32 times and average, and check two points: the tank empty, and one level you measure with a tape.
Set your tank
Measure the inside of the tank, and set its shape and size at the top of the sketch. Litres follow from the shape:
// --- your tank (cm) ---Tank tankShape() { Tank t; t.shape = VERTICAL_CYLINDER; // VERTICAL_CYLINDER, HORIZONTAL_CYLINDER or BOX t.diameterCm = 108; // cylinders: inside diameter t.heightCm = 109; // vertical cylinder or box: the depth of water when full (to the overflow) t.lengthCm = 0; // horizontal cylinder or box: inside length t.widthCm = 0; // box: inside width return t;}const float MOUNT_CM = 140; // ultrasonic: from the sensor face down to the tank floorconst float DEAD_CM = 25; // closer than this the sensor cannot see (JSN-SR04T: 20 to 25 cm)const float AIR_C = 25; // the air in the tank, for the speed of sound (10 degrees off is 1.7 %)const long UTC_OFFSET_S = 0; // your time zone in seconds, for "today" and the night check (UTC+3: 3 * 3600)- Upright cylinder and box: the litres grow in a straight line with the depth.
- Lying cylinder: they do not. A quarter of the depth holds only 19.6 % of the water, so a straight scale would say "a quarter full" when the tank is a fifth full. The sketch uses the area of the circular segment: r² acos((r - h) / r) - (r - h) √(2rh - h²), times the length.
- MOUNT_CM is from the sensor face to the tank floor. Measure it through the hole with a tape and a weight.
- AIR_C: sound travels faster in warm air. 10 degrees wrong is 1.7 % of the distance: 1 m reads 102.5 cm if you assume 35 C at 20 C.
Flash and check on the desk
Open tank_monitor/tank_monitor.ino in the Arduino IDE, choose ESP32 Dev Module, install PubSubClient and ArduinoJson (version 7), and upload. Compiled for the ESP32 with the esp32 core 4.0.0: 1.10 MB of flash (84 % of the default app space) and 66 KB of RAM for global data, with no warnings, for the ultrasonic, the transmitter, and with or without the flow meter.
Point the probe at the floor from a known height and compare the Serial Monitor with a tape. The logic was tested on a laptop first; these lines come from those tests:
vertical 108 x 109.2 cm: 1000.4 L fullhorizontal 100 x 200 cm: 1570.8 L full; at a quarter of the depth it holds 19.6 %sound: 343.2 m/s at 20 C; 1 m read with 35 C is 102.5 cmflow: 10.00 L counted from 4500 pulses with 450 bounces droppeduse: 6 h at 60 L/h counted as 360 L, rate 60.0 L/hnight: dry night lost 4 L (no leak); a dripping valve lost 48 L (leak)refill: 29 % to 95 % took 66 minutesPrint the box and the probe holder
PETG or ASA outdoors. 0.2 mm layers, 4 walls, 20 % infill, no supports. About 160 g in all.
| Part | Lay it |
|---|---|
| Box | On its back, open side up. 75 x 147 mm with its ears. |
| Lid | Front face down, skirt up. |
| Probe holder | Flange down, tube up. |
| Hood | Closed top down. |
The box keeps the rain out by its shape: every cable enters from below through a gland, the lid's skirt hangs 6 mm over the walls, and a small weep hole lets condensation out. The holder's bore is 22.5 mm: measure your probe first, and set ProbeD in the model if it differs.
Fit it on the tank
- Cut a 35 mm hole in the tank lid, away from the inlet pipe, the ladder and the walls, so the beam sees only water.
- Push the probe into the holder from the top, until its face is level with the underside of the flange. Seal it with silicone.
- Screw the flange over the hole with four M4 screws and a bead of silicone. Put the hood on; the cable leaves through its slot.
- Measure
MOUNT_CMthrough the hole, from the flange underside to the floor. The full water level must be at least 25 cm below it. - Mount the box on a wall or post beside the tank, out of the sun if you can. Every cable goes in from below, through a gland. Close the lid on a strip of foam tape.
Checked in FreeCAD 1.1: every module fits the box, with room for the USB plug below the board, and the probe fits its holder.
The local rule: float switch first, relay second
Have an electrician fit the float switch in the pump's control circuit, so the pump cannot run when the tank (or the sump it draws from) is low. That works with no board at all.
The board's relay is the second line. It starts the fill pump below 30 % and stops it at 95 %. When the level is unknown (a broken sensor, foam, water in the dead zone) it switches the pump off, so a fault cannot overflow the tank. Commands can change it, and each gets a reply:
d/SWD-XXXXXX/c {"command":"pump","value":true,"session":"a1"}d/SWD-XXXXXX/k/a1 {"status":"ok"}d/SWD-XXXXXX/c {"command":"stop_at","value":90,"session":"a2"}d/SWD-XXXXXX/k/a2 {"status":"ok"}d/SWD-XXXXXX/c {"command":"pump","value":true,"session":"a3"} (the tank is full)d/SWD-XXXXXX/k/a3 {"status":"error","message":"tank full or level unknown"}// a command on d/<id>/c: run it, then reply on d/<id>/k/<session>void onCommand(char* t, byte* data, unsigned int len) { JsonDocument msg; if (deserializeJson(msg, data, len)) return; // not JSON: ignore it const char* session = msg["session"] | ""; const char* error = nullptr; JsonVariant v = msg["value"]; bool yes = v.is<bool>() ? v.as<bool>() : (v == "on" || v == 1); float n = v | -1.0f; if (msg["command"] == "pump") { // by hand; never past the stop mark pump.command(yes); drivePump(); if (yes && !pump.on) error = "tank full or level unknown"; } else if (msg["command"] == "auto") { pump.autoMode = yes; drivePump(); } else if (msg["command"] == "start_at") { if (n >= 5 && n < pump.stopPct - 5) pump.startPct = n; else error = "start_at must be 5 and at least 5 below stop_at"; } else if (msg["command"] == "stop_at") { if (n > pump.startPct + 5 && n <= 98) pump.stopPct = n; else error = "stop_at must be 98 or less and at least 5 above start_at"; } else { error = "unknown command"; } if (*session) { char reply[128]; if (error) snprintf(reply, sizeof(reply), "{\"status\":\"error\",\"message\":\"%s\"}", error); else snprintf(reply, sizeof(reply), "{\"status\":\"ok\"}"); mqtt.publish((topic("k/") + session).c_str(), reply); }}Connect it
On siliconwit.io, our monitoring service: Add Device, then Define my own device, with the fields of the reading below. Set the data interval to 60 seconds and add a Toggle command named pump. Copy the Device ID and Access Token into these lines, with your Wi-Fi above them, and upload again:
const char* MQTT_HOST = "mqtt.siliconwit.io";const int MQTT_PORT = 8883; // MQTT over TLSconst char* DEVICE_ID = "SWD-XXXXXX"; // client ID, username, and the topic nameconst char* MQTT_PASS = "your-access-token"; // the Access Token#define USE_TLS 1 // 0 only for a test broker on your own network: then port 1883, and no certificateOne reading a minute, on d/<device-id>/t:
{"level_cm":71.4,"percent":65.5,"litres":654,"pump":0,"sensor":1,"used_today":212, "hours_left":19.8,"last_refill_min":66,"flow_lpm":3.2,"night_loss":4,"leak":0,"ts":1795000630}The link is MQTT over TLS on port 8883, with the ISRG Root X1 certificate in the sketch. After a failed connect it waits 5, 10, 20, 40, then 60 seconds. While the network is down it keeps up to 12 hours of readings and sends them later, each with its own time. A watchdog restarts the board if the code ever gets stuck. Your own Mosquitto broker works the same: change the broker lines, as on the live sensor node.
Set the alerts
This is where the tank stops being a surprise. Email alerts come with the free account; SMS with the Business plan. Send each one to the person who can act on it:
percent below 20, held for 10 minutes Overflowingpercent above 98, held for 5 minutes Leak at nightleak equal to 1 Sensor faultsensor equal to 0, held for 15 minutes Not reportingthe device silent for 10 minutes The last one matters most. A monitor that has lost its power or its network cannot say so itself; only the service can notice the silence.
Read the record
- Use per day.
used_todayadds every fall in the tank; refills do not count. A school that uses 2,000 L a day with a 5,000 L tank has two days of water, not "plenty". - Hours left.
hours_leftis the litres now at the recent rate of use: the number to read before a weekend. - The night loss. Between 00:00 and 04:00 almost nobody draws water. If the tank still loses more than 20 L in those hours with the pump off, water is going somewhere: a dripping float valve, a running toilet, a cracked pipe. Set
UTC_OFFSET_Sto your time zone so the night is your night. - Refill time.
last_refill_minis how long the pump took from 30 % to full. When it grows week by week, the pump, the borehole or the pipe is getting weaker.
You are done when
- The litres on the device page match the tank within a few per cent, checked with a bucket.
- Lifting the float switch by hand stops the pump, with the board unplugged.
- The relay starts the pump at the low mark and stops it at the high mark, with the Wi-Fi off.
- An alert reaches your phone when you unplug the monitor for ten minutes.
- After a night, the reading carries
night_loss, and you know what that number means for this tank.
Downloads
- Build sheet: parts, wiring and the steps on two A4 pageswater-tank-monitor-build-sheet.pdf · 99 KB
- Firmware: Arduino sketch, logic, testswater-tank-monitor-firmware.zip · 19 KB
- Print files (STL) and print noteswater-tank-monitor-stl.zip · 91 KB
- Drawings: the assembly, then the box, the lid, and the probe holder with its hood, with every size in mm (4 A3 sheets, 1:1)water-tank-monitor-drawings.pdf · 145 KB
- FreeCAD 1.1 model, with the spreadsheetWaterTankMonitor.FCStd · 33 KB
- STEP, every part and moduleWaterTankMonitor.step · 221 KB
- Wiring drawingwater-tank-monitor-wiring.svg · 22 KB
Checked: the firmware compiles for the ESP32 with no warnings in all its variants (ultrasonic or 4 to 20 mA, with or without the flow meter and TLS). Its logic (the three tank shapes, the ping median, the 4 to 20 mA checks, the flow count, the pump rule, the daily use, the night check and the refill time) passes 55 checks on a laptop. The parts were checked in FreeCAD 1.1 with every module in place. A build on a real tank is next: if yours needs a change, tell us in the discussion below.
References
- Pressure, Level and Flow Measurement: a borehole, a pump and a tank, measured properly.
- 4 to 20 mA Current Loops: the transmitter option, its sense resistor and its failure levels.
- Borehole Pumps, Drawdown and Yield: why a pump runs dry, and how to protect it.
- JSN-SR04T data sheet: 5 V, a trigger of at least 10 µs, a blind zone of about 25 cm, and a range of about 4.5 m.
- NAMUR NE 43: the signal levels that tell a broken loop from an empty tank.
- Build: the live sensor node: the same firmware family, with one sensor.
Ways to take this build
Fitted and watched by us
Our engineers fit the sensors, watch the readings and send a monthly record of what it found and saved.
Start with one siteKSh 15,000USD 150 for the build
Four video meetings over about six weeks: plan it, show progress, show it working. Ends in a project record anyone can check.
Build it with a supervisorOpen to all
Every step, the firmware and the printable files. Tick the steps as you go.
Start step 1Priced for the group
Your staff or students build the water tank monitor together, with reviews and a record for each person.
Ask for a groupPlus opens the worked answers in the lessons each step links to: see Plus.
Made by
Engineers who build what they teach, and answer for it.



My notes on this page
Kept on this device. To have them on every device, turn on keeping in My notes.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.