A rainwater tank, a well or a cesspool all raise the same question: how much is in there right now? An ESP32 can answer it, but the sensor decides whether you get a level you can trust or a graph that wanders by several centimetres while nothing changes.

There are three ways to measure a continuous level. You can measure the distance from the top down to the surface with sound or with light, or measure the water pressure at the bottom. A fourth group, point sensors, only tells you whether the liquid has reached one particular height. Each has a typical way of failing, and knowing those failures is most of the choice.

Ultrasonic sensors: the common choice, with a blind zone

An ultrasonic sensor mounted in the lid sends a 40 kHz pulse down and times the echo from the surface. The level is the tank depth minus the measured distance. Two waterproof modules dominate hobby builds.

JSN-SR04T. A single waterproof transducer on a 2.5 m cable, with the electronics on a separate board. Because the same element sends and then listens, it cannot hear echoes from close by. The v3.0 datasheet gives a range of 23 to 600 cm, and ESPHome's documentation says 25 to 600 cm. In practice this means the highest possible water level must stay at least 25 cm below the transducer. Mount it lower and a full tank reads as an error or as nonsense.

A02YYUW (DFRobot). Its blind zone is only 3 cm, with a range of 3 to 450 cm, a specified accuracy of ±1 cm and a 60° sensing angle. It runs on 3.3 to 5 V and sends the distance over UART at 9600 baud. DFRobot points out that IP67 does not make it an underwater sensor. It measures through air and must stay above the liquid.

How the module talks to the ESP32 matters

The JSN-SR04T can work like the classic HC-SR04: you send a trigger pulse and the ESP32 measures how long the echo pin stays high. It also has serial modes, selected with a resistor on the board. With 47 kΩ on the mode pad (R27 on v1.0 and v2.0), it measures every 100 ms on its own and sends the distance in millimetres at 9600 baud. With 120 kΩ it waits for the command byte 0x55 and then measures once. ESPHome's jsn_sr04t platform supports these two serial modes, while the generic ultrasonic platform handles trigger and echo.

The serial modes are worth the soldering. Mariusz Białończyk, who monitors his cesspool with an ESP32 and a JSN-SR04T-type module, first used trigger and echo. Once Wi-Fi was running, the distance timed by the ESP32 started to jump by a few centimetres, from 47.2 to 50.8 cm, with the sensor untouched. After he switched the module to serial output, where it times the echo itself, the readings settled on a constant value.

Power is another trap. ProtoSupplies, quoted in the same write-up, found 3.3 V operation of the JSN-SR04T v3.0 "spotty at best" and recommends 5 V with level shifting for the lines going to a 3.3 V microcontroller.

What goes wrong inside a tank

  • The beam hits something else. The sound leaves in a cone. In Białończyk's cesspool the sensor first reported roughly the same distance all day while he was filling the tank. It was measuring a fixed distance near the top, not the liquid. He moved the transducer lower, just above the liquid, with nothing in between.
  • Condensation. Twice in several months, water condensed on the ceiling of his tank and on the sensor face, and the readings went wrong until he opened the cover and wiped the drops off.
  • Foam and temperature. Foam absorbs the pulse, and the speed of sound changes by about 0.17% per °C. In a tank a few metres deep the temperature effect is small next to the other problems, but it can explain a slow drift between summer and winter.

A basic ESPHome configuration for the A02YYUW with a median filter and a level computed from a 200 cm deep tank looks like this:

uart:
  rx_pin: GPIO16
  baud_rate: 9600

sensor:
  - platform: a02yyuw
    id: tank_distance
    name: "Tank distance"
    filters:
      - median:
          window_size: 15
          send_every: 10

  - platform: template
    name: "Tank level"
    unit_of_measurement: "cm"
    update_interval: 10s
    lambda: |-
      // the A02YYUW reports millimetres; 200 cm from sensor face to tank bottom
      return 200.0 - id(tank_distance).state / 10.0;

ESPHome recommends filters for the A02YYUW and for the JSN-SR04T in continuous mode, because both send several readings a second. Note the units: the a02yyuw platform reports millimetres, while jsn_sr04t reports metres.

Laser time of flight: good on paper, awkward on clear water

ST's VL53L1X times a 940 nm infrared laser pulse instead of sound. The datasheet promises up to 4 m, but only in the dark, on a white target and with a long timing budget. In long-distance mode under strong ambient light the typical maximum falls to about 73 cm. The field of view is 27° and can be narrowed to 15° by reducing the region of interest, and the minimum distance at which ranging is accurate is 4 cm.

Water is the problem. ST's application note on liquid level measurement explains that some photons reflect from the surface, some are absorbed and some go through, bounce off the bottom and come back late. Clear, still water behaves like a mirror. Turbid, moving or foaming water reflects well from the surface and measures accurately. In an answer marked as the solution on ST's community forum, the VL53L1X is described as unable to tell the surface return from the bottom return. That takes one of ST's histogram sensors.

For a rainwater tank with clear, still water, a laser sensor is the least predictable option of the three. It makes more sense in a small container or with cloudy liquid. ESPHome has long had a VL53L0X driver, and a VL53L1X driver was merged in October 2026 for the 2026.11 release.

Hydrostatic pressure probe: the most stable reading

A submersible pressure probe lies on the bottom and measures the pressure of the water column above its membrane. About 1 m of water gives about 0.1 bar. Foam, condensation and obstacles in the tank do not affect it.

The vent tube. The probe also feels the air pressure acting on the water surface, which changes with the weather. TE Connectivity's application note explains how vented gauge probes deal with that: a thin tube inside the cable connects the back of the sensing element to the atmosphere, so air pressure acts on both sides and cancels out. This is the most common type for level measurement. The tube must not be blocked or flooded. In MarkLabs' build the cable end with the tube sits in a dry place.

The 4-20 mA loop. Probes such as DFRobot's KIT0139 (0-5 m range, 0.5% accuracy, IP68, 316L stainless steel membrane) or the TL-136 used by MarkLabs output a current: 4 mA at zero and 20 mA at full scale. They need a 12-36 V supply in the case of the DFRobot probe, and MarkLabs powers the TL-136 from 24 V. A current loop tolerates long cables well. MarkLabs runs about 15 m from the tank to the controller.

To read the current, pass it through a resistor and measure the voltage. With 120 Ω, the value DFRobot uses in its SEN0262 converter, 4 mA gives 0.48 V and 20 mA gives 2.4 V. The level is then:

level = (I - 4 mA) / 16 mA × probe range

The 24 V supply belongs only in the loop. Only the voltage across the resistor goes to the ADC, and the grounds of the loop supply and the ESP32 must be connected.

Which ADC. MarkLabs used an external 16-bit ADS1115 over I²C to keep the measurement predictable. The built-in ADC works too. ESPHome has applied the manufacturer's calibration since 2021.11, and with attenuation: auto its tests showed a usable range of about 0.075 to 3.12 V, which covers 0.48-2.4 V. On the original ESP32, use an ADC1 pin (GPIO32-GPIO39), because Espressif's documentation notes that ADC2 is also used by Wi-Fi. DFRobot recommends at least a 12-bit ADC for its converter.

sensor:
  - platform: adc
    pin: GPIO34
    name: "Tank level"
    unit_of_measurement: "cm"
    attenuation: auto
    update_interval: 5s
    filters:
      - median:
          window_size: 7
          send_every: 6
      - calibrate_linear:
          - 0.48 -> 0
          - 2.40 -> 500
      - offset: 5.0

Here 500 is a 5 m probe in centimetres, and the offset adds the height of the probe's membrane above the tank bottom. MarkLabs found 5.3 cm in its tank, and only after adding it did the ESP32 reading match the tape measure at 48.0 cm. Check the two calibration points against your own measured voltages at an empty and a known level instead of trusting the nominal values.

Point sensors: floats and non-contact switches

Sometimes you only need to know "almost full" or "too low for the pump", with no calibration at all.

Float switches are cheap and simple, but the wiring has to survive the tank. Białończyk ran three float switches in his cesspool for about seven years. The weak point was a junction box inside the tank: liquid got in, and the readings flip-flopped in humid weather.

Non-contact capacitive switches such as the XKC-Y25 stick to the outside of the tank and sense the liquid through non-metallic walls up to 20 mm thick. They do not work on metal containers. The NPN version has an open-collector output, runs from 5-12 V (or 24 V in another variant), draws about 5 mA and responds in 500 ms. The black wire selects whether the output is normally open or normally closed. Because the output is open collector, you can connect it to an ESP32 input with a pull-up to 3.3 V.

A point sensor is also a good second opinion. A continuous sensor that freezes on a wrong value will not raise an alarm by itself. An independent high-level switch will.

Height is not volume

Litres follow from height directly only in a tank with vertical walls. MarkLabs' 3000-litre rainwater tank is shaped, so 50% of the height is not 1500 litres, and the author shows only centimetres and a percentage of height until he has a height-to-volume table for that tank. Do the same, or build the table yourself by filling the tank in known steps.

Which one to choose

Situation Good choice Watch out for
Rainwater tank, sensor can sit 25 cm above the top level JSN-SR04T in serial mode Blind zone, beam hitting pipes
Shallow tank or little headroom A02YYUW Wide 60° cone, keep it above the liquid
Cesspool Ultrasonic sensor, mounted clear of obstacles Condensation on the sensor face
Long cable run, stable reading wanted 4-20 mA pressure probe Vent tube, probe offset, loop supply
Pump protection or overflow alarm Float or capacitive switch Junction boxes inside the tank
Small container, cloudy liquid VL53L1X Clear still water, ambient light

Common mistakes

  • Mounting an ultrasonic sensor inside its own blind zone. A full tank then reads as empty, as an error or as random values.
  • Timing echoes on a busy ESP32. Use a module that measures by itself and sends the result over UART.
  • Feeding the 4-20 mA loop voltage into the ESP32. Only the voltage across the shunt resistor goes to the ADC.
  • Blocking the probe's vent tube. The reading then drifts with the weather.
  • Showing litres from height in a shaped tank. Start with centimetres and add volume when you have the tank's curve.