Water Level Indicator (Water Tank) using Ultrasonic Sensor (HC-SR04) for ESP8266, ESP32, Raspberry Pi Pico W for Alexa

In this part, we’ll guide you on building a Water Level Indicator (also known as Water Tank Indicator) using a Ultrasonic Sensor that can be connected to either an ESP32, ESP8266, or Raspberry Pi Pico W. Once it’s set up, you’ll be able to measure water levels using Amazon Alexa, ask Alexa for information about it, and also monitor it through the Sinric Pro app and receive a push notification when water level is low.

Sinric Pro HC-SR04 Ultrasonic Sensor

Prerequisites :

  1. ESP32, ESP8266 or Raspberry Pi Pico W x 1.
  2. Ultrasonic Sensor (HC-SR04) x 1.
  3. Plant with a pot x 1.
  4. Jumper Wires.

Quick introduction to Ultrasonic Sensor

The ultrasonic sensors are low-cost, easy-to-use sensor that can measure distance. It works by sending out a burst of ultrasonic sound and then measuring the time it takes for the echo to return. The distance to the object is then calculated based on the speed of sound.

It is powered by a 5V DC power supply and has four pins: VCC, GND, TRIG, and ECHO. Once the sensor is connected, you can use it to measure distance by sending a pulse to the TRIG pin and then measuring the time it takes for the ECHO pin to go high. The distance to the object can then be calculated using the following formula:

distance = (time * speed of sound) / 2

where:

According to the manufacturer notes, the HC-SR04 sensor works best between 2cm – 400 cm (1” - 13ft) within a 30 degree cone.

Sinric Pro HC-SR04 esp8266 ultrasonic sensor wiring

Wiring Ultrasonic Sensor

Sinric Pro HC-SR04 esp8266 ultrasonic sensor wiring

HC-SR04 Pin ESP8266 ESP32 Pico W
VCC 5V 5V 5V
GND GND GND GND
TRIG 12 (D6) GPIO 5 15
ECHO 14 (D5) GPIO 18 14

Let’s verify that sensor is wired correctly and working.

Sinric Pro HC-SR04 readings

Let’s try measuring the water level as a precentage.

Sinric Pro HC-SR04 readings

Step 1 : Connect to Sinric Pro

Step 1.1 : Creating a custom device type for Water Level Indicator.

Sinric Pro does not have a built-in device type for Water Level Indicator so we are going to create a custom device type to support showing Water Level Indicator using Device Template feature.

Note: You can use the device template import feature mentioned below to skip creating the full template.

sinricpro water tank new device template basic info

Here we must select the features of our Sensor. We want to know the Water level as a precentage. So let’s drag a Range and Push Notification capability.

sinricpro water tank new device template capabilities

Click on Configure button and setup the two capabilities like below.

sinricpro water tank device template range capability

Click on Save to save.

Click on Save to save the template.

Now you can see the template we just created.

Import an existing template?

If you are feeling lazy, you can use the import feature.

Sinric Pro import template json

Paste this Template:

Sinric Pro create device alexa

Sinric Pro copy device id

Sinric Pro copy device id

Step 2 : Connect to Sinric Pro

Step 2.1 Install Sinric Pro Library

Sinric Pro install SinricPro library

2.2 Complete Code

Now you should be able to see the tank water level via Alexa, Sinric Pro App

Alexa, What’s the water level(range name) in water tank(device name)

Sinric Pro Alexa HC-SR04 sensor

Sinric Pro Alexa HC-SR04 portal

Sinric Pro water tank app

Step 3 : Making the Full and Empty Tank heights configurable via UI

3.1 Update device template

In order to set the tank full and empty height configurable via UI, we need to go back to the custom device template and add two more Range Controllers for Full Tank Height and Empty Tank Height like below.

Full Empty

Full device template would look like this

Full Template

Here’s the template with full and empty heights. You can import this in the Device Template section.

3.2 Complete Code

Tank control with Alexa and SinricPro App

That’s It.

Troubleshooting

  1. Google Home or SmartThings are not supproted.

  2. Please refer to our Troubleshooting page for more details.

This document is open source. See a typo? Please create an issue