Although there are several water level indicators and devices available to monitor the level in an overhead water tank, these devices often come at a hefty price and with limited functionality. Besides, they require dipping electrodes or a switch into the water to operate; this may contaminate the water and corrode with time.

But what if you could build a wireless and contactless Wi-Fi-based water level sensor that reports the remaining water in your tank directly on your smartphone? In this DIY guide, we will build such a water level indicator using a NodeMCU board and an ultrasonic sensor that sits at the top of the water tank cover and reports the data to your smartphone via Home Assistant.

How Does It Work?

An ultrasonic water level sensor works by sending sound waves at a specific frequency (also known as ultrasonic waves) and receiving the reflected wave from the targeted object. The sensor calculates and reports the distance between the sensor and the object based on the time it takes the sound wave or ultrasonic wave to travel and reflect.

how ultrasonic water level sensor works

The ultrasonic sensor reports the distance value in centimeters (cm) by default. Along with the tank depth, this value reported by the ultrasonic sensor can be used to find the remaining water in the tank by calculating the distance between the tank water level and the ultrasonic sensor.

Things You’ll Need

To build this smart water level sensor, you need the following:

  • An ESP8266-based MCU, such as NodeMCU, D1 Mini, ESP01, etc.
  • An SR04 ultrasonic sensor
  • Jumper wires

For this project, it is recommended that you use a JSN-SR04 waterproof ultrasonic sensor to prevent sensor damage due to moisture.

jsn sr04 waterproof ultrasonic sensor with nodemcu

Steps to Build a Smart Water Level Sensor

You can follow the steps below to build a smart Wi-Fi-based contactless ultrasonic water level sensor to monitor water consumption and save yourself from climbing the stairs or water tank to check it manually. You can also use this sensor to monitor a salt tank level.

Step 1: Install and Set Up Home Assistant

Home Assistant (HA) is preferred but not required to build a smart Wi-Fi-based water level sensor or controller. However, if you want to receive notifications, track daily/weekly/monthly consumption, or add automation based on the tank level, you must set up Home Assistant.

You may follow our previous guide to install Home Assistant on Raspberry Pi or an old laptop. If you don’t want to install or set up Home Assistant, you can flash the Tasmota firmware and use it to check the water level. Although it’s not as intuitive as Home Assistant, it does the job and reports the raw value via a web page.

Step 2: Measure Tank Depth

You need to measure the tank depth. You can do so by measuring the height of the tank from the outside or inside using a measuring tape.

measure tank height to find the depth

Once you have the measurement, measure the distance between the tank cover where the ultrasonic sensor will be installed and the brim of the water tank. This is the distance that you need to subtract from the total depth.

For instance, if the tank height is 120cm and the distance between the sensor on the cover and brim of the water is 10cm, then the depth is 120 - 10 = 110cm.

Step 2: Compile the Firmware

To compile firmware, open Home Assistant and install the ESPHome add-on if you haven’t already. Then follow these steps:

  1. Click the +New Device button and then click Continue.
  2. Enter the name for the firmware file, such as waterlevelsensor, and click Next.
  3. Select the MCU you have, such as NodeMCU, ESP32, or D1 Mini, and click Next. Then click Skip.
select the microcontroller from the options

Now find the waterlevelsensor project and click Edit. This opens the waterlevelsensor.yaml file. In the file, copy and paste this YAML code. In the code, edit the following:

  • Enter your Wi-Fi SSID and password.
  • Edit the values in the formulae under filters with the distance between sensor and water brim and the full tank height.
  • Click Save.
  • Connect your NodeMCU, ESP32, or D1 mini to your PC using a micro USB cable and then click Install.
  • Choose the Plug into this computer option. This will start compiling the YAML code and generate a firmware file (waterlevelsensor.bin). This can take a while.
  • After the firmware is compiled, click Download Project to download the waterlevelsensor.bin file.

Step 3: Flash the Firmware on NodeMCU

To flash the waterlevelsensor.bin firmware, you can download the ESPHome-Flasher tool or use the ESPHome web. The steps are as follows:

After downloading the firmware, click Open ESPHome Web.

download the waterlevelsensor firmware

Click Connect and then select the USB serial COM port where your NodeMCU, D1 Mini, or ESP32 is connected.

choose connected microcontroller to flash the esphome firmware

Click Install. Click Choose File, browse to the waterlevelsensor.bin firmware file, and click Install. Once the firmware is flashed, disconnect the USB cable.

Step 4: Connect Ultrasonic Sensor with NodeMCU

Refer to the diagram below to connect the SR04 ultrasonic sensor with the MCU board.

nodemcu connected to sr04 ultrasonic sensor

NodeMCU/ESP32/D1 Mini

SR04 Ultrasonic Sensor

3V3

Vcc

D5

Trig

D6

Echo

GND

GND

If you are using the waterproof JSN SR04 M-2 ultrasonic module, connect the pins as shown in the diagram.

jsnsr04-connected-to-nodemcu

NodeMCU/ESP32/D1 Mini

JSN-SR04 Ultrasonic Sensor

Vin

5V

D5

Trig

D6

Echo

GND

GND

Once the ultrasonic sensor is connected to the MCU, reconnect the USB cable. The device will auto-connect to the Wi-Fi network based on the details you entered in the code and will be shown as Online in the ESPHome Dashboard.

Click Logs under the waterlevelsensor. This will display the logs and data received by the sensor.

logs generated by waterlevelsensor displaying data

If the logs display the information as shown in the screenshot, you can go ahead and configure the sensor in Home Assistant.

If you see "Distance measurement timed out", do the following:

  • Check you have connected the ultrasonic sensor pins with the MCU correctly.
  • The JSN SR04 M-2 module can measure 20-400cm. So, keep the object at least 20cm away. You also need to make changes in the code accordingly.
  • The ultrasonic sensor we have used may fail to measure distance beyond 400cm. If your tank is deeper than 400cm (unusual), this may not work for you.

Step 5: Add Smart Water Level Sensor to Home Assistant

To add the ultrasonic water level sensor to Home Assistant for monitoring, follow these steps:

  1. In Home Assistant, go to Configuration > Devices and Services.
  2. The waterlevelsensor should be auto-discovered and listed. If not, click Add Integration.
  3. Search for and select ESPHome.
  4. Enter the IP address of the waterlevelsensor. You can find this from the logs or your Wi-Fi router. After entering the IP address, click Submit.
  5. Select an area and click Finish.
  6. CThe sensor will be listed under ESPHome. Click waterlevelsensor and select the entity.
  7. Click Add to Lovelace.
  8. Choose a room and click Next.
  9. This will add the waterlevelsensor to the dashboard.

You may further customize the default card by adding the following in the code editor:

        type: gauge
name: Water Level Sensor
unit: '%'
entity: sensor.waterlevelsensor
   green: 0
   yellow: 45
   red: 85

Step 6: Install the Sensor to Your Water Tank Cover

You may now install the sensor in your water tank. For the project, we used the SR04 sensor, which isn’t waterproof. To make it waterproof, we used a transparent case and sufficient nail polish to insulate the electrical components on the board.

We drilled two tiny holes and used a metal wire to keep the SR04 module attached to the cover. Another bigger hole was made for the wires connected to the ultrasonic sensor. We used a long 4-core wire to interface the ultrasonic sensor with the NodeMCU as the tank is located on the rooftop and the temperature here can rise to 40-45°C.

ultrasonic sensor installed on water tank cover

You may keep both in one case and supply power to the NodeMCU. Also, ensure good Wi-Fi signal strength when you are installing the NodeMCU or the sensor. Read our guide on how to boost your Wi-Fi signal to extend the Wi-Fi range.

Control the Water Pump to Keep the Tank Full

By integrating the smart water levels sensor with Home Assistant, you can add automation to get alerts on your smartphone or via Alexa/Google Assistant when the tank level is low or full. Similarly, you can add automation to switch on the water pump to fill the tank when it’s running low and switch it off automatically once the tank level reaches a certain level, such as 90-100%.

In addition, you can add a waterproof temperature sensor probe, such as a DS18B20, to your smart water level sensor to check and monitor the tank water temperature.