Build a Water Level Sensor With an Ultrasonic Sensor and Home Assistant

2022-06-25 18:40:06 By : Mr. Minghua Shen

Monitor the level in your water tank with an ultrasonic sensor in the lid, and add optional automation.

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.

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.

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.

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

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

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.

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.

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.

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.

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

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:

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.

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

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

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

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

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.

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:

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

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

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.

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.

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.

Ravi is an expert tech explainer, an IoT enthusiast and Linux lover with a background in big data and app development. He spends most of his weekends working with IoT devices and playing games on the Xbox. He is also a solo traveler who loves hiking and exploring new trails.

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!