Water level indicator using Nodemcu and ultrasonic sensor – Step by step instructions

 Water level indicator using Nodemcu and ultrasonic sensor

        Hello, welcome back to the IoT-based tutorial from SriTu Hobby. Today we are going to talk about how to make a water level indicator using Nodemcu and the ultrasonic sensor. This project can be used mainly for the water tank in our house. It is also designed to monitor the water level online and turn the water pump ON / OFF even when we are not at home. Also, the Nodemcu ESP8266 board and the Blynk application are mainly used for this project. An ultrasonic sensor is used to measure the amount of water and a relay module is used to turn the water pump ON / OFF.

This project can be made more effective by using a waterproof ultrasonic sensor instead of the HC-05 ultrasonic sensor used in this project.
Water level indicator using Nodemcu and ultrasonic sensor

The process of this tutorial

When the project is running, the Nodemcu board is connected to the Blynk cloud via WIFI. The ultrasonic sensor also calculates the distance to the surface of the water. That is, the water level is calculated. These values are then displayed on the Blynk app and LCD display. You can also turn the relay module ON / OFF with the button created in this Blynk app. That is, if a water pump is connected to the relay module, it can be turned ON / OFF.
So, let’s do this project step by step. The required components are given below.
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Nodemcu ESP8266

Ultrasonic sensor

Relay module

Jumper wires

Breadboard

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.
Water level indicator using Nodemcu and ultrasonic sensor circuit diagram

Step 3

Thirdly, let’s set up the Blynk application. For that, download and install the Blynk app using the play store or apps store. After, run the Blynk app. Other steps are as follows.
  • First, click the “New Project” button. After, enter the project name as you like. Then select the device and connection type. Finally, click the “Confirm” button.
Water level indicator blynk app set up
  • Next, click the “+” button and includes a “Button” and “Level V” widget. OK, let’s set up this button. For that, click this “Button”. Now, enter the button name as you like. After, select “Virtual V0” for the PIN and drag the button below it to the “Switch” side.
  • OK, let’s set up the “level V” widget. For that, click this widget. Now, enter the widget name as you like. Then, select “Virtual V1” for the PIN and enter your water tank level. Then turns ON the flip axis button.
  • Lastly, arrange these widgets as you like.

Step 4

OK, now the Blynk app interface is done. Then, let’s create the program for this project. It is as follows.
First, download and install the libraries below.
WIF library — Download
Blynk library — Download
I2C library — Download
The complete program of this project – Download
/*Water level monitoring system.
 * This code creates by the SriTu Hobby team.
 * https://srituhobby.com
 */
 
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
char auth[] = "";//Enter your Auth token
char ssid[] = "";//Enter your WIFI name
char pass[] = "";//Enter your WIFI password
 
BlynkTimer timer;
bool pinValue = 0;
 
#define trig D3
#define echo D4
#define relay D5
 
void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  Wire.begin(D2, D1);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10L, Wlevel);
  digitalWrite(relay, HIGH);
}
 
BLYNK_WRITE(V0) {
  pinValue = param.asInt();
}
 
void loop() {
  Blynk.run();
  timer.run();
}
 
void Wlevel() {
  if (pinValue == 1) {
    digitalWrite(relay, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Motor is ON ");
  } else if (pinValue == 0) {
    digitalWrite(relay, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Motor is OFF");
  }
 
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
 
  Blynk.virtualWrite(V1, cm);
  Serial.println(cm);
  lcd.setCursor(0, 0);
  lcd.print("Water Level: ");
  lcd.print(cm);
  lcd.print("   ");
}

Code explanation

First, includes the Blynk, WIFI, and I2C libraries.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 
Next, create an object for the I2C library and enter the I2C address and LCD size.
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
OK, now enter your Blynk Auth token and WIFI connection information.
char auth[] = “”;//Enter your Auth token
char ssid[] = “”;//Enter your WIFI name
char pass[] = “”;//Enter your WIFI password
 
 
 
After, defines the Ultrasonic sensor PINs and relay PIN.
#define trig D3
#define echo D4
#define relay D5
 
In the setup function, these PINs as set INPUT and OUTPUT. Later, the LCD, serial monitor, and Blynk library began.
void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  Wire.begin(D2, D1);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10L, Wlevel);
  digitalWrite(relay, HIGH);
}
 
In the void loop, the Blynk library is run.
void loop() {
  Blynk.run();
  timer.run();
}
 
The “Wlevel” function contains the main code of this project.
This code turns the relay module ON and OFF and prints it on the LCD.
void Wlevel() {
  if (pinValue == 1) {
    digitalWrite(relay, LOW);
    lcd.setCursor(0, 1);
    lcd.print(“Motor is ON “);
  } else if (pinValue == 0) {
    digitalWrite(relay, HIGH);
    lcd.setCursor(0, 1);
    lcd.print(“Motor is OFF”);
  }
 
This code is used to obtain ultrasonic sensor readings. After, These values are sent to the Blynk app and to the LCD.
 
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
 
  Blynk.virtualWrite(V1, cm);
  Serial.println(cm);
  lcd.setCursor(0, 0);
  lcd.print(“Water Level: “);
  lcd.print(cm);
  lcd.print(”   “);
}
 
Now, select board and port, Afterward, upload this code to the Nodemcu board.


Step 6

Lastly, run your Blynk app interface we create earlier. OK, enjoy this project. The full video guide is given below, So, we will meet in the next tutorial.

Water level indicator using Nodemcu and ultrasonic sensor – Step by step instructions

9 thoughts on “Water level indicator using Nodemcu and ultrasonic sensor – Step by step instructions”

  1. ONE OF THE BEST TUTORIALS I HAVE SEEN IN A LONG TIME. WILL BE DOING THIS AT HTE WEEKEND FOR MY FISH TANK. NOT REALLY NEEDED BUT LOOKS FUN. THANK YOU.

  2. This is one of the simplest procedure i found. Nice job Sritu.
    could you clarify on the installation of libraries you mentioned in step 4 in to Arduiono IDE.
    I downloaded the ESP8266wifi library as you mentioned in step 4 , but unable to install this specific library in to Arduino IDE hence not able to see the ESP8266 board in Tools/Board.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Select your currency
USD United States (US) dollar
EUR Euro
Scroll to Top