ESP8266IOT Projects

IOT Water level monitoring using Ultrasonic Sensor

IoT Water Level Monitoring:

 

IOT Water level monitoring using Ultrasonic Sensor- In today’s tutorial, you will learn how to make an IoT water level monitoring system using the HC-SR04 ultrasonic sensor, Nodemcu esp8266 wifi module, and Blynk application. This project is based on two-way communication, you can monitor the Water level in real-time and you can also control the water pump. The lamp indicator represents the water pump.

You might be wondering, why am I using the Arduino board with the Nodemcu ESP8266, if the same thing can be done using only the Nodemcu module. Well, in this tutorial, my main concern is to also explain how you can do Serial communication between the Arduino and Nodemcu Module.

 This is the 2nd version of the water level monitoring system. While in version 1 of the water level monitoring system I used only the LEDs to show the percentage of water available in the Water tank. As today’s episode is based on version 1,  so I highly recommend you should watch my previous tutorial on the water level monitoring system using led’s , because in this episode I will only explain the modifications.  This Tutorial covers

  1. Complete circuit diagram explanation.
  2. Blynk Application designing
  3. Arduino and Nodemcu programming and finally
  4. Testing

You can also ready my article on IoT based Water level monitoring and automatic water pump control system using the ESP32 WiFi + Bluetooth Module, Waterproof Ultrasonic Sensor, and the New Blynk V2.0 more…

read my article on Wireless water level indicator “communication range 1.5Km.


Amazon Links:

12v Adaptor:

Arduino Uno

Arduino Nano

Mega 2560:

HC-SR04 Ultrasonic Sensor:

Nodemcu ESP8266 WiFi Module:

LM7805 Voltage Regulator:

470uf capacitor:

330-ohm resistor:

DC Female Power Jack:

Female Headers:

Male Headers:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


Circuit Diagram:

iot water level

Circuit Diagram of the IoT water leveling monitoring system

This circuit is designed in Cadsoft eagle 9.1.0 version. if you want to learn how to make a schematic and PCB then watch my tutorial. As you can see the circuit diagram of the water level monitoring system is really simple. The trigger pin of the ultrasonic sensor is connected with pin number 7 of the Arduino. While the echo pin of the ultrasonic sensor is connected with pin number 6 of the Arduino. The VCC and GND pins of the ultrasonic sensor are connected with the Arduino’s 5v and ground pins. LED3 to LED7, These are the 5 led’s which will be used to display the percentage of water available in the water tank. The first led will show 20 %, the second led will show 40% and so on. So the last led will show 100%.  These are the current limiting resistors which are connected in series with these 2.5v LEDs. These LEDs are connected with the Arduino’s analog pins A0 to A4…

The circuit in the upper left corner is the 5v regulated power supply based on the lm7805 voltage regulator. This 5v power supply will be used to power up the Nodemcu esp8266 wifi module. Two 470 UF capacitors are connected at the input and output of the 7805 voltage regulator. A 330-ohm resistor is connected in series with the led. This is a current limiting resistor.  J1 is the dc female power jack where you can connect a 12v adaptor or battery. A wire from the output of the voltage regulator is connected with the Vin pin of the Nodemcu module.

The tx and Rx pin’s of the Nodemcu are connected with pin2 and pin3 of the Arduino. So the Nodemcu will communicate serially with the Arduino through pin2 and pin3. Pin2 is the Rx and pin3 is the tx, which will be defined in the programming using the software serial library.

This is a 12v SPDT type relay which will be used to control the water pump. But for the demonstration purposes, I will connect an indicator lamp. This relay will be controlled using pin number 13 of the Arduino.


Note: this old version of the Blynk app is no more functional. For the blynk mobile App setup and Blynk.cloud dashboard setup ready my article on the New Blynk V2.0. In this article I have explained how to migrate your projects from Blynk 1.0 to the new Blynk V2.0. You can also watch the video.

Blynk application for the water level monitoring:

IoT water level monitoring system, Blynk application designing

First of all, open the Blynk application…..

iot water level

set the project name as Water tank…..

iot water level

Click on the choose device and select nodemcu….

iot water level

make sure you set the connection type to wifi….

iot water level

then click on the create button…

iot water level

an authentication token will be sent on your email id, which will be then used in the programming, simply copy and paste it in programming …

iot water level

iot water level

iot water level

Now click on the screen and search for the level v widget and add it…..

iot water level

Click on the level v widget set the title as water level.

iot water level

now click on the pin and select virtual pin v2…

set the minimum value to 60 and maximum value to 0…

iot water level

then click on the push and select 1 second….

now again click on the screen and this time add the numeric input…

Click on the numeric input and set the title as water pump…

now click on the pin and select virtual pin v10…

iot water level

you can change the font size if you want, let’s select the large type…

set the minimum value to 10 and the maximum value to 11…

iot water level

so now our Blynk application is ready.

Iot water level Programming:

For the step by step program explanation watch video Tutorial given at the end of this article.

Water Level Monitoring Arduino Programming:

// iot water level monitoring

#include <SoftwareSerial.h>
SoftwareSerial nodemcu(2,3);

#define trigpin 7 // digital pin 7 
#define echopin 6 // digital pin 6

int wpump = 13; // water pump
int flag = 0; 
int led1 = A0; 
int led2 = A1; 
int led3 = A2; 
int led4 = A3; 
int led5 = A4; 
 int duration, distance;

int firstVal;
String myString; // complete message from Arduino, which consistors of snesors data
char rdata; // received charactors
String cdata; // complete data


void setup()
{
Serial.begin(9600);
nodemcu.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(wpump, OUTPUT); 
digitalWrite(wpump, LOW); 

  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

  digitalWrite(led1, LOW); 
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  

 delay(1000); 
}

void loop()
{

      if(nodemcu.available() == 0 )
      {
       
        ultrasonic(); 
       
      }

      if ( nodemcu.available() > 0 ) 
{

      rdata = nodemcu.read(); 
    myString = myString+ rdata; 
    //Serial.print(rdata);
    if( rdata == '\n')
    {
Serial.println(myString);
// new code
String l = getValue(myString, ',', 0);
firstVal = l.toInt(); // 


  myString = "";


            if ( (firstVal == 10)&& ( flag == 0) ) // turn off pump
            {

             digitalWrite(wpump, LOW); 
              flag = 1; 
                
            } 

                        if ( (firstVal == 11)&& ( flag == 1) ) // turn ON pump
            {

             digitalWrite(wpump, HIGH); 
              flag = 0;                
            } 

            
    }
  
}
  
}

void ultrasonic()
{

   digitalWrite(trigpin, HIGH);

delayMicroseconds(1000);  
digitalWrite(trigpin, LOW);


duration = pulseIn(echopin,HIGH);

distance = ( duration / 2) / 29.1;
Serial.println("cm:"); 
nodemcu.println(distance); // send to nodemcu module


if(  (distance > 0) && (distance <= 10)   ) 
{
  digitalWrite(led1, HIGH); 
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH); 
  
} else
if(  (distance > 10) && (distance <= 20)  ) 
{

  digitalWrite(led1, LOW); 
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH); 

} else

if(  (distance > 20) && (distance <= 30)  ) 
{

  digitalWrite(led1, LOW); 
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
} else

if(  (distance > 30) && (distance <= 40)  ) 
{

  digitalWrite(led1, LOW); 
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
} else

if(  (distance > 50) && (distance <= 60)  ) 
{

  digitalWrite(led1, LOW); 
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
} else

if(  distance > 60 ) 
{

  digitalWrite(led1, LOW); 
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
}
}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}


IoT water level monitoring system Nodemcu Programming:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>

int pinValue1;


String v2Arduino; // values to Arduino

char auth[] = "dfb3d11fbe874cd69ab413c8bb45f613";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ZONG MBB-E8231-6E63";
char pass[] = "08659650";

SimpleTimer timer;

String myString; // complete message from Arduino, which consistors of snesors data
char rdata; // received charactors


int firstVal;  
// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000);
  
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

    timer.setInterval(1000L,sensorvalue1); 

}

void loop()
{

  
  
   if (Serial.available() == 0 ) 
   {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  toArduino();
   }
   
  if (Serial.available() > 0 ) 
  {
    rdata = Serial.read(); 
    myString = myString+ rdata; 
   // Serial.print(rdata);
    if( rdata == '\n')
    {

// new code
String l = getValue(myString, ',', 0);



firstVal = l.toInt();


  myString = "";
// end new code
    }
  }

}

void sensorvalue1()
{
int sdata = firstVal;
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, sdata);

}





String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

// in Blynk app writes values to the Virtual Pin V3
BLYNK_WRITE(V10)
{
   pinValue1 = param.asInt(); // assigning incoming value from pin V10 to a variable

   if ( pinValue1 > 60 ) 
   {

    pinValue1 = 60;
   }

}


void toArduino()
{
v2Arduino = v2Arduino + pinValue1 + ","; 
Serial.println(v2Arduino); 
delay(100); 
v2Arduino = ""; 
}

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

12 Comments

  1. Hi Sir Good Afternoon i am trying to interface the water level sensor using Arduino nano for harvesting the fish Aquarium automatic water level detecting and filling the water below average of water but, the tank depth was 45CM the sensor size only the 5 cm how to apply the same application using long height sensor OR if any chance to increase the size of sensor please reply this question sir thank you

    1. Ultrasonic Sensor has a long detection range. You can watch my other tutorial on water level monitoring system in which I have used LEDs for different levels. that tutorial will help you.

  2. Hi Sir Good Afternoon i am trying to interface the water level sensor using Arduino nano for harvesting the fish Aquarium automatic water level detecting and filling the water below average of water but, the tank depth was 45CM the sensor size only the 5 cm how to apply the same application using long height sensor OR if any chance to increase the size of sensor please reply this question sir thank you

  3. Hi, sir, I have tried your 1st water level indicator by pro mini. I have used only 3 nos of led for testing purposes. But it indicates only one led out of three led. The other two led indicates but very very dim and o/p voltage of Arduino for this two is 0.15 volt during on condition. Where’s 3.4 volt o/p of 1st led during on condition. The all three led off condition Arduino o/p is 0 voult. I have change by another new pro mini but same thing happened. Checked every loose connection but not found. I have upload the program to pro mini by uno. After uploading the tx led of uno is glowing continuesly. Now without your help I can’t compete this project. …… Susabhan.

  4. Hello, I’m trying to get more decimal places in my reading. Will changing “int distance” to a float do this? Is there anywhere in the code where changes would be needed aside from the first declaration I mentioned?

    Thanks!

  5. Saya mencoba nya tapi terdapat error di proram arduino Uno nya pesan error ‘getValue’ was not declared in this scope

  6. Arduino: 1.8.14 Hourly Build 2020/12/15 11:33 (Windows 10), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200”

    water_level_test:7:25: fatal error: SimpleTimer.h: No such file or directory

    #include <SimpleTimer.h>

                         ^
    

    compilation terminated.

    exit status 1

    SimpleTimer.h: No such file or directory

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

Leave a Reply

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

Back to top button