Building a Water Level Sensor Using Ultrasonic Sensors and Home Assistant

While there are a variety of water level indicators and devices that can be used to monitor the water level in overhead tanks, these devices are often expensive and have limited functionality. Additionally, they require immersion of electrodes or switches into water to operate; this can contaminate the water and corrode over time.

But what if you could build a wireless and contactless Wi-Fi based water level sensor that could report the amount of water remaining in the 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 located on top of the tank lid, reporting the data to your smartphone via Home Assistant.

How does it work?

Ultrasonic water level sensors work by sending sound waves of a specific frequency (also called ultrasonic waves) and receiving reflected waves from a target object. The sensor calculates and reports the distance between the sensor and the object based on the time it takes for a sound or ultrasonic wave to propagate and reflect.

How ultrasonic water level sensors work

Ultrasonic sensors report distance values ​​in centimeters (cm) by default. Along with the tank depth, this value reported by the ultrasonic sensor can be used to find the amount of water remaining in the tank by calculating the distance between the tank water level and the ultrasonic sensor.

what you need

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

Make a video of the day
  • ESP8266-based MCU, such as NodeMCU, D1 Mini, ESP01, etc.
  • SR04 Ultrasonic Sensor
  • jumper

For this project, it is recommended that you use the JSN-SR04 waterproof ultrasonic sensor to prevent the sensor from being damaged by 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 non-contact ultrasonic water level sensor to monitor water consumption and avoid climbing stairs or manual tank inspections. You can also use this sensor to monitor the 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 tank level, you’ll have to set up Home Assistant.

You can install Home Assistant on a Raspberry Pi or old laptop by following our previous guide. 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. While it’s not as intuitive as Home Assistant, it does the job and reports raw values ​​via a web page.

Step 2: Measure the Tank Depth

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

Measure tank height to find depth

After completing the measurement, measure the distance between the tank cover where the ultrasonic sensor will be installed and the edge of the tank. This is the distance you need to subtract from the total depth.

For example, if the tank height is 120 cm and the distance between the sensor on the lid and the edge of the water is 10 cm, the depth is 120 – 10 = 110 cm.

Step 2: Compile the firmware

To compile the firmware, open Home Assistant and install the ESPHome plugin (if not already installed). Then follow these steps:

  1. click + new equipment button then click continue.
  2. Enter the name of the firmware file, e.g. Water level sensor, and click Next.
  3. Select the MCU you have, such as NodeMCU, ESP32 or D1 Mini, and click Next. then click jump over.

Choose a microcontroller from the options

find now Water level sensor item and click edit. this will open water level sensor.yaml document.In the file, copy and paste this YAML code. In the code, edit the following:

  • Enter your Wi-Fi SSID and password.
  • Edit the value in the formula below filter Distance from sensor and water edge and full tank height.
  • click save.
  • Connect the NodeMCU, ESP32 or D1 mini to the PC with a micro USB cable and click Install.
  • choose plug into this computer options. This will start compiling the YAML code and generate the firmware file (waterlevelsensor.bin). This may take a while.
  • After the firmware is compiled, click Download the project download water level sensor.bin document.

Step 3: Flash the firmware on NodeMCU

To flash the waterlevelsensor.bin firmware, you can download ESPHome-Flasher tool or use the ESPHome website. Proceed as follows:

After downloading the firmware, click Open the ESPHome website.

Download water level sensor firmware

click connect Then select the USB serial COM port to which the NodeMCU, D1 Mini or ESP32 is connected.

Select the connected microcontroller to flash the esphome firmware

click Install. click Select the fileBrowse to water level sensor.bin firmware file and click Install. After the firmware is flashed, disconnect the USB cable.

Step 4: Connect Ultrasonic Sensor with NodeMCU

Refer to the figure below to connect the SR04 ultrasonic sensor to the MCU board.

nodemcu connected to sr04 ultrasonic sensor

NodeMCU/ESP32/D1 Mini SR04 Ultrasonic Sensor
D5 trigger
D6 echo
ground ground

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


NodeMCU/ESP32/D1 Mini JSN-SR04 Ultrasonic Sensor
wine 5V
D5 trigger
D6 echo
ground ground

After connecting the ultrasonic sensor to the MCU, reconnect the USB cable.The device will automatically connect to the Wi-Fi network based on the details you enter in the code and will appear as online inside ESPHome Dashboard.

click log under Water level sensor. This will display the logs and data received by the sensor.

Log display data generated by the water level sensor

If the log shows the information shown in the screenshot, you can proceed to configure the sensor in Home Assistant.

If you see “distance measurement timed out,” do the following:

  • Check that you have correctly connected the ultrasonic sensor pins to the MCU.
  • JSN SR04 M-2 module can measure 20-400cm. Therefore, keep objects at least 20 cm away. You also need to change the code accordingly.
  • The ultrasonic sensors we use may not be able to measure distances over 400 cm. If your tank is more than 400cm deep (unusual) this may not be for you.

Step 5: Add the Smart Water Level Sensor to Home Assistant

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

  1. In Home Assistant, go to Configuration > Devices and Services.
  2. This Water level sensor Should be discovered and listed automatically.If not, click Add integration.
  3. search and select ESP Home.
  4. Enter IP address Water level sensor. You can find it from the log or from the Wi-Fi router.After entering the IP address, click submit.
  5. Select an area and click end.
  6. The C sensor will be listed under ESPHome.click Water level sensor and select the entity.
  7. click Add to Lovelace.
  8. Select a room and click Next.
  9. this will add Water level sensor to the dashboard.

You can 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: Mount the Sensor to the Tank Cover

You can now install the sensor in the tank. In this project, we used the SR04 sensor which is not waterproof. To make it waterproof, we used a clear case and enough nail polish to insulate the electrical components on the board.

We drilled two small holes and used wire to attach the SR04 module to the lid. Another bigger hole is for the wires to connect to the ultrasonic sensor. We use a long 4-conductor wire to connect the ultrasonic sensor to the NodeMCU, since the water tank is located on the roof, where the temperature can rise to 40-45°C.

Ultrasonic sensor mounted on the tank cover

You can put the two together and power the NodeMCU. Also, make sure a good Wi-Fi signal strength when installing NodeMCU or sensors. Read our guide on how to boost your Wi-Fi signal to extend Wi-Fi range.

Control the water pump to keep the tank full

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

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

Smart Home Energy Monitoring Panel Home Assistant Features

How to Build a DIY Home Energy Monitor Using the ESP8266

Read next

About the author

About the author


Leave a Comment