zerohop/blog

$ cat arduino-water-tank-meter.md · 08 Nov 2026 · 3 min read

Measuring a rain water tank with an Arduino and a 4-20 mA sensor

#arduino#homelab#loxone#home-assistant

We have a rain water tank, and "how much is left?" is a question you can't answer by looking at it. So I built a meter.

It uses a hydrostatic pressure sensor: it lies on the bottom of the tank and measures the pressure of the water above it. More water, more pressure. No moving parts and nothing that has to look at the water surface.

The inside: Arduino UNO R4 WiFi, the LCD on the lid, WAGO connectors for the 24 V loop, and the push button.
The inside: Arduino UNO R4 WiFi, the LCD on the lid, WAGO connectors for the 24 V loop, and the push button.

Why 4-20 mA

Industrial sensors like this one don't output a voltage, they output a current between 4 and 20 mA. That's a feature, not a hassle:

An Arduino can't measure current directly, so a 220 Ω resistor turns it into a voltage:

text4 mA  x 220 Ω = 0.88 V   (empty)
20 mA x 220 Ω = 4.40 V   (full)

That fits nicely inside the 0-5 V range of the analog input. The sensor runs on its own 24 V supply, sharing a ground with the Arduino.

Getting a stable number

A raw analog reading is noisy. Two things make it stable:

  1. Trimmed bursts. Every 5 seconds the Arduino takes 100 samples, sorts them, throws away the 10 highest and the 10 lowest (the Wi-Fi radio causes spikes) and averages the rest.
  2. A rolling average. The last 60 bursts, so 5 minutes, are averaged again. A water level doesn't change in seconds anyway, and it smooths out movement of the water surface.

On top of that, a trend compares the level with 10 minutes ago and shows an arrow: filling, emptying or steady.

The display

The LCD is normally off. A short press on the button turns it on for 5 minutes; holding it for 5 seconds shows Wi-Fi diagnostics. Useful when you're standing next to it with a phone that has no idea what IP address it got.

Percentage, litres and water height on the 16x2 LCD, in a 3D printed case.
Percentage, litres and water height on the 16x2 LCD, in a 3D printed case.

Talking to the house

The Arduino runs a tiny web server. Ask it anything on port 80 and it answers with JSON:

json{
  "sensor": { "current_mA": 5.743, "active": true },
  "water": {
    "percentage": 14,
    "height_cm": 34,
    "liters": 1362,
    "trend": "steady"
  }
}

The Loxone Miniserver polls it with a Virtual HTTP Input and picks out the fields it needs. Home Assistant reads the same endpoint and sends a push notification when the tank drops below 20 percent for half an hour, and another one when it's filled up again.

The part I like most: failing safely

If the loop current drops below 3 mA (cable break, no 24 V) or goes above 21 mA (short circuit), the sensor is marked as not active, every water value becomes -1, and the LCD shows SENSOR ERROR with the measured current. A broken sensor never reports an empty tank, and the house can raise an alarm on -1.

It's the same rule I apply at work: a monitoring system that can't tell "nothing is wrong" from "I can't see anything" is worse than no monitoring at all.

Build it yourself

The code, the wiring, the parts list and the 3D printable case are on GitHub. Calibration takes two readings: one with the tank empty, one at the level you call full.

← all posts