Skip to content
You Start with one site
Amounts in Start with one site
Build · Water and pumps

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.

About KSh 3,757USD 29 in parts Printing: 8 to 12 hours Building: a weekend
Found this useful?
The box from the model, with the probe holder for the tank lid on the right. Every cable enters from below, through a gland.

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.

Litres for3 tank shapes
Sensor sees25 cm to 4.5 m
Readingsone a minute
Kept offline12 hours
Leak check00:00 to 04:00
Dry-run guarda float switch
The cloud must not be the dry-run protection. A pump that runs dry can burn out in minutes, and the internet can be down for hours. The protection that matters is a float switch (or the pump controller's own dry-run cut-out) wired in the pump circuit, which works with no board, no Wi-Fi and no internet. This monitor is what tells you about it, and it keeps its own pump rule on the board as a second line.

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.

#PartQtyAbout
1ESP32 DevKitAny 30 or 38 pin boardFind it1KSh 650USD 5.00
2JSN-SR04T ultrasonic sensorWaterproof probe on a 2.5 m cable, with its driver board. The AJ-SR04M works the sameFind it1KSh 585USD 4.50
3Float switchFor the pump circuit itself, fitted by an electricianFind it1KSh 390USD 3.00
4Relay module, 1 channelFor the fill pump's control circuit; input that switches at 3.3 VFind it1KSh 195USD 1.50
5Flow meter (optional)YF-S201 or similar, 1 to 30 L/min, on the outletFind it1KSh 455USD 3.50
6Resistors1 k, 2 k, 10 k, 20 kFind it4KSh 26USD 0.20
7PG7 cable glandsFor 3 to 6.5 mm cablesFind it3KSh 195USD 1.50
8Jumper wiresFemale to femaleFind it15KSh 195USD 1.50
9USB cable and 5 V adapter1 A or more; the cable to match your boardFind it1KSh 520USD 4.00
10Screws4 x M3 x 10, 4 x M4 x 12, 2 for the wallFind it10KSh 130USD 1.00
11PETG or ASAOutdoors PLA softens in the sunFind it160 gKSh 416USD 3.20
TotalKSh 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).

The box open on the wall, and the probe holder beside it with its hood off, each part marked with its number from the parts list. In the box: 1, the ESP32 DevKit, on the left with its USB plug below it; 2, the ultrasonic driver board, at the top right; 4, the relay module, below it; 7, the three holes in the floor for the cable glands. On the right: 2, the ultrasonic probe, in the bore of the teal probe holder.
Where each part goes, numbered as in the list above. The sensor (2) comes in two pieces: its driver board in the box and its probe in the holder on the tank lid. The float switch (3) and the flow meter (5) sit at the tank and the pipe, and their cables come in through the glands (7).
The ultrasonic sensor suits open-top and plastic tanks. The probe is sealed and looks down from the lid. It cannot see the last 25 cm in front of it, so mount it at least that far above the overflow. Foam, ripples and drops on its face give odd echoes; the firmware takes the middle of seven pings and smooths the rest.
A pressure transmitter is the sturdier choice: a sealed 4 to 20 mA sensor that lies on the tank floor, for closed steel tanks, foam, condensation and deep tanks. It needs a 12 to 24 V supply and costs about ten times as much. Set 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.

The water tank monitor: the box, the lid, the probe holder, the hood and the three parts inside. Drag to turn, pinch or scroll to zoom. Open in the full viewer.
A3 assembly drawing, first angle, 1 to 1. The front view of the box with the lid taken off shows the ESP32 DevKit on the left and the ultrasonic driver board and the relay module on the right. To its right, the view from the left and section A-A through the middle gland hole and both ears, hatched, with the lid's skirt hanging over the walls. Below, the probe holder and hood from above and section B-B through the holder's axis with the probe in its bore. Sizes 74.8 by 146.8 by 36.8 mm, flange 80 mm, holder and hood 42 mm tall. Balloons match the item list: 1, 2, 4, then 12 to 15 for the box, the lid, the holder and the hood.
The A3 assembly drawing (click for the PDF). Sheets 2 to 4 give every size of the box, the lid, the probe holder and the hood.

Build it

Each step links to the lesson section that teaches it. Open one only when you need it.

Build track
Follow this build, one step at a time

Tick each step as you finish it. Your place is kept, so you can stop and come back.

1

Wire it on the desk

Wire it on the desk with jumper wires first, and fit it on the tank only when it works.

Wiring drawing. JSN-SR04T TRIG to GPIO 13; ECHO through 1 k to GPIO 14 with 2 k from GPIO 14 to ground; VCC to 5 V. Flow meter signal through 10 k to GPIO 27 with 20 k to ground; VCC to 5 V. Relay module IN to GPIO 26, VCC to 5 V. The 4 to 20 mA option: a 12 to 24 V supply to the transmitter, its output to a node with a 150 ohm resistor and a 100 nF capacitor to ground, and GPIO 34 on that node.
Open the drawing full size. The sensor and the flow meter run on 5 V, so their outputs go through a divider to reach 3.3 V.
  • 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.
2

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 floor
const 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.
3

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 full
horizontal 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 cm
flow: 10.00 L counted from 4500 pulses with 450 bounces dropped
use: 6 h at 60 L/h counted as 360 L, rate 60.0 L/h
night: dry night lost 4 L (no leak); a dripping valve lost 48 L (leak)
refill: 29 % to 95 % took 66 minutes
Hold the probe 100 cm above the floor. Check: it reads within 2 cm. Closer than 25 cm it says the water is in the dead zone, and sends no level.
4

Print 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.

PartLay it
BoxOn its back, open side up. 75 x 147 mm with its ears.
LidFront face down, skirt up.
Probe holderFlange down, tube up.
HoodClosed 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.

5

Fit it on the tank

  1. 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.
  2. Push the probe into the holder from the top, until its face is level with the underside of the flange. Seal it with silicone.
  3. 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.
  4. Measure MOUNT_CM through the hole, from the flange underside to the floor. The full water level must be at least 25 cm below it.
  5. 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.

Fill a bucket's worth into the tank. Check: the litres on the Serial Monitor rise by about that much.
6

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);
}
}
Mains pumps. The relay module switches low voltage. A mains pump runs through a contactor and its own protection, fitted by an electrician; this relay may switch only the contactor's low-voltage coil.
7

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 TLS
const char* DEVICE_ID = "SWD-XXXXXX"; // client ID, username, and the topic name
const 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 certificate

One 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.

8

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:

Running lowpercent 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.

9

Read the record

  • Use per day. used_today adds 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_left is 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_S to your time zone so the night is your night.
  • Refill time. last_refill_min is 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

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

Ways to take this build

Have it done for your site

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 site
Supervised, with an engineer

KSh 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 supervisor
On your own

Open to all

Every step, the firmware and the printable files. Tick the steps as you go.

Start step 1
For a team or a class

Priced for the group

Your staff or students build the water tank monitor together, with reviews and a record for each person.

Ask for a group

Plus 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.