IoT Based Water Level Indicator Using Ultrasonic Sensor

IoT Based Water Level Indicator Using Ultrasonic Sensor and NodeMCU

Today we will be working on a Water level indicator whose data can be monitored through a webpage over a local area network. The water level will be detected by using an ultrasonic distance measurement sensor. We have previously built another IoT based tank water level monitoring system using a float sensor, but in this project, we will use an ultrasonic sensor for detecting the level of water. 

You must have seen several water level indicators but most of them use special float sensors that are not easily available and neither simple to use. But in this tutorial, we will be using an ultrasonic sensor for measuring the water level. You will come to know how further in the article.

For making the project more user friendly, we will be integrating it with a local webserver through which you can monitor the data from any device connected to the same Wi-Fi as your ESP board.

 

Ultrasonic Sensor Work and Use

Before understanding the working of the project, let us first see how an ultrasonic sensor works. A typical HC-SR04 ultrasonic sensor used in this project is shown below.

Ultrasonic Sensor

Ultrasonic sensor is an electronic device that measures the distance of a target object by emitting ultrasonic sound waves and converts the reflected sound into an electrical signal. 

Ultrasonic waves travel faster than the speed of audible sound (i.e. the sound that humans can hear). Ultrasonic sensors have two main components: the transmitter (which emits the sound using piezoelectric crystals) and the receiver (which encounters the sound after it has traveled to and from the target).

Ultrasonic Sensor Working

To calculate the distance between the sensor and the object, the sensor measures the time it takes between the emissions of the sound by the transmitter to its contact with the receiver. The formula for this calculation is-

D = ½ T x C (where D is the distance, T is the time, and C is the speed of sound ~ 343 meters/second).

Ultrasonic Sensor Distance Measurement

So where can we use these sensors? We have previously used these sensors in Contactless Temperature measurementIoT based Inventory management, and many other projects, apart from this, it can also be used in Robot navigation, as well as factory automation. Water-level sensing is another good use and can be accomplished by positioning one sensor above a water surface. Another aquatic application is to use these sensors to “see” the bottom of a body of water, traveling through the water, but reflecting off the bottom surface below. We have used the same principle in our Flood Detection and Monitoring system as well.  

 

Ultrasonic Sensor for Measuring Water Level

So, now as we know the working of the ultrasonic sensor, it is pretty straightforward to understand the working of the project. We just have to read the values from the sensor and convert it to CMs and after doing all that, we have to publish the data in a local webserver that will be created by our NodeMCU board after getting connected to Wi-Fi.

 

Required Components

For making this project, we will be needing a few basic components.

ESP8266 NodeMCU board: This will be the heart of our whole project.

ESP8266 NodeMCU

 

HC-SR04 Ultrasonic Sensor: This will be used for sensing the level through ultrasonic sound waves as explained earlier.

HC SR04 Ultrasonic Sensor

Breadboard: All the connections will be made on the breadboard itself for making it simple.

Jumper wires: As we are using a breadboard, jumper or hookup wires are the way to go for connections.

 

Interfacing Ultrasonic Sensor with NodeMCU

As we are not using many components, the IoT water level indicator circuit diagram used in this project is fairly simple. We just need to connect the HC-SR04 Ultrasonic sensor to our NodeMCU module. The rest of the work will be done via the software part itself.

Here is the circuit diagram for the connection.

IoT Water Level indicator Circuit Diagram

I used a breadboard to connect my ultrasonic sensor with the NodeMCU and then used connecting wires to make the connection. The testing set-up of mine looks like this below, later we will use longer jumper wires to mount the sensor over a demo water tank. 

Interfacing Ultrasonic Sensor with NodeMCU

 

Programming NodeMCU to Read Water Level and Display on the Webserver

Now as we have finished making the connections. We will be proceeding to the coding part of the project. Let me clear the algorithm that we will be following while making the code. The complete code can be found at the bottom of this page.

What our Arduino code needs to do is, first of all, connect to the network whose SSID and password are mentioned in the code. After successfully getting, it needs to spit out the Local IP address to the serial monitor, so that we may access the webpage created by it. After doing so, it should start dealing with the ultrasonic sensor and after calculating the distance, it must send the data to the webpage that we have created. 

 

Let us understand it part by part, so first of all, we are including the necessary libraries and the global variables that will be used further in the code. Along with that, we are also providing the SSID and passwords of our Wi-Fi router and initiating the webserver at port number 80.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
int TRIGGER = D3;
int ECHO = D2;
// Replace with your network credentials
const char* ssid = "xxxxx";
const char* password = "xxxxxxx";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
int data;

 

Further, in the setup part, we have set the pin mode of the pins as input and output and have stored our page in the page variable using HTML. We have kept a single line HTML code to make up our webpage. In the setup part itself, we get connected to the Wi-Fi router and print the IP address to the serial monitor.

The HTML page that we have created refreshes itself every three seconds. You may change it in the code itself.

void setup(void){
  pinMode(TRIGGER, OUTPUT); 
  pinMode(ECHO, INPUT); 
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.on("/", [](){
    page = "<head><meta http-equiv=\"refresh\" content=\"3\"></head><center><h1>Web based Water Level monitor</h1><h3>Current water level is :-</h3> <h4>"+String(data)+"</h4></center>";
    server.send(200, "text/html", page);
  });
  server.begin();
  Serial.println("Web server started!");
}

 

Most of the work is done, now in the loop part, we just calculate the distance from the ultrasonic sensor, and after storing it, we publish the data over our webpage. Our webpage has been made in such a way that, it refreshes every 3 seconds.

void loop(void){
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER, LOW);
  long duration = pulseIn(ECHO, HIGH);
  data = (duration/2) / 29.09;
  server.handleClient();
}

This is pretty much about the code, Now just upload the code, and let us see the project in action.

 

IoT Based Water Level Indicator Testing and Working

This is the webpage that we have created using our NodeMCU board. It contains a heading that is, Web-based Water Level Indicator, below which is printing the live data coming through the HCSR04 Ultrasonic sensor. The water level values are printed in CMs, the less the value, the less empty the container, and vice versa.

Web based Water Level Indicator

 

Before directly testing the water level, we tested the distance measurement. It worked like charm as you may see from the above and below images.

Water Level Indicator Testing

After testing this, we set up a water level apparatus for demonstrating the working. We affixed the ultrasonic sensor on top of the jar and also placed a ruler for scale. This way, the monitor will show the data of how empty the jar is in CMs. Here is how the setup looks like

Water Level Indicator using Ultrasonic Sensor

Now here is the working, firstly, we kept the jar empty, so it should display 20 in the server as the jar is 20cm deep and is empty as of now.

Water Level Indicator Testing

Now we have filled the jar by half so now it should display half the value of 20.

Water Level Indicator Working

For seeing the working in detail, you may see the video we have attached below. Hope you enjoyed the article and learned something useful from it. If you have any questions, you can leave them in the comment section below. 

Code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
int TRIGGER = D3;
int ECHO   = D2;
// Replace with your network credentials
const char* ssid = "Comet";
const char* password = "evilzebra";
ESP8266WebServer server(80);   //instantiate server at port 80 (http port)
String page = "";
int data; 
void setup(void){
 pinMode(TRIGGER, OUTPUT); 
 pinMode(ECHO, INPUT); 
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.on("/", [](){
    page = "<head><meta http-equiv=\"refresh\" content=\"3\"></head><center><h1>Web based Water Level monitor</h1><h3>Current water level is :-</h3> <h4>"+String(data)+"</h4></center>";
    server.send(200, "text/html", page);
  });
  server.begin();
  Serial.println("Web server started!");
}
 void loop(void){
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER, LOW);
  long duration = pulseIn(ECHO, HIGH);
  data = (duration/2) / 29.09;
  server.handleClient();
}

Video

80 Comments

I in addition to my guys were actually taking note of the excellent advice from your web blog and then quickly developed a horrible feeling I never expressed respect to the site owner for those secrets. Most of the young men appeared to be absolutely stimulated to study them and have in effect pretty much been having fun with these things. Appreciate your simply being well accommodating and for picking this sort of tremendous subjects millions of individuals are really wanting to discover. Our sincere apologies for not expressing gratitude to you earlier.

My husband and i got satisfied that Raymond managed to round up his reports from the ideas he had in your site. It's not at all simplistic to simply always be handing out points which many people could have been trying to sell. So we remember we've got the writer to be grateful to for that. All the explanations you've made, the easy site navigation, the friendships your site make it easier to instill - it's got many unbelievable, and it's really assisting our son and us imagine that this concept is cool, and that is especially mandatory. Thanks for all the pieces!

I not to mention my buddies were actually looking through the great thoughts from your site while the sudden I got a terrible suspicion I had not thanked the web blog owner for those strategies. My ladies came totally thrilled to read all of them and now have without a doubt been loving those things. Thank you for turning out to be indeed thoughtful and also for getting these kinds of really good information millions of individuals are really wanting to know about. My very own sincere apologies for not expressing appreciation to sooner.

I simply had to thank you so much all over again. I do not know the things that I could possibly have done without the actual points discussed by you over this subject. This was an absolute fearsome situation in my position, however , coming across a professional manner you dealt with the issue made me to cry for happiness. I'm just thankful for your work as well as hope you know what a great job you are always carrying out educating some other people by way of your blog post. I'm certain you've never met any of us.

A lot of thanks for all your hard work on this web site. Gloria loves doing research and it is simple to grasp why. Most of us know all regarding the dynamic ways you present effective steps via your blog and even improve response from the others on this point plus my girl has always been learning a great deal. Have fun with the rest of the new year. Your performing a powerful job.

I simply wanted to write down a small remark so as to thank you for those lovely secrets you are giving on this site. My considerable internet investigation has at the end of the day been compensated with good quality ideas to exchange with my partners. I would repeat that many of us readers are extremely fortunate to dwell in a magnificent network with many lovely professionals with interesting opinions. I feel really blessed to have encountered the webpages and look forward to really more pleasurable minutes reading here. Thank you once again for all the details.

Thank you so much for providing individuals with an extremely special opportunity to read articles and blog posts from this website. It is always very beneficial and full of fun for me personally and my office acquaintances to visit your blog no less than three times every week to study the fresh guidance you have. And lastly, I'm usually amazed for the eye-popping creative ideas served by you. Some two points on this page are rather the most effective we have all ever had.

I simply wanted to thank you so much again. I'm not certain what I would've tried in the absence of these techniques documented by you relating to such a topic. It absolutely was a very distressing dilemma in my opinion, but taking a look at this specialized fashion you handled it made me to cry for gladness. I am happier for this service and have high hopes you comprehend what an amazing job you are doing instructing many people through the use of your webpage. Most probably you have never come across any of us.

I have to express my love for your generosity giving support to women who absolutely need help on this important niche. Your real dedication to passing the message all over appears to be wonderfully advantageous and have specifically helped some individuals much like me to achieve their ambitions. Your interesting guidelines entails a great deal a person like me and even more to my office colleagues. Regards; from each one of us.

I must express some appreciation to this writer for rescuing me from this difficulty. As a result of browsing throughout the world wide web and seeing recommendations which were not productive, I figured my life was done. Existing minus the strategies to the difficulties you have resolved by means of this guide is a crucial case, and the kind which could have badly affected my entire career if I had not noticed the blog. Your own understanding and kindness in dealing with a lot of things was useful. I am not sure what I would have done if I hadn't come upon such a stuff like this. I'm able to at this moment look ahead to my future. Thanks for your time very much for the specialized and effective help. I will not hesitate to recommend your blog post to anybody who desires recommendations about this problem.

Thanks a lot for providing individuals with an extraordinarily nice opportunity to read articles and blog posts from this website. It really is very sweet and as well , jam-packed with a good time for me and my office fellow workers to visit your web site no less than three times in one week to read the fresh things you have got. And lastly, I'm usually pleased with your incredible hints you give. Some two tips in this posting are unequivocally the most suitable we've had.

I want to express appreciation to the writer for bailing me out of such a circumstance. Just after scouting throughout the the net and finding advice which were not helpful, I assumed my life was well over. Being alive without the presence of answers to the difficulties you have resolved as a result of your short article is a critical case, as well as the ones which might have badly affected my entire career if I hadn't discovered your web blog. The training and kindness in handling all the details was vital. I am not sure what I would have done if I hadn't come across such a stuff like this. I'm able to at this moment look forward to my future. Thanks so much for this professional and sensible help. I will not think twice to suggest the blog to any individual who should have guidance on this subject matter.

I wanted to post you a bit of remark to be able to give thanks over again for these precious advice you've discussed here. It was simply particularly open-handed with people like you to give openly just what many people could possibly have supplied as an e book to help make some cash for their own end, specifically given that you could possibly have tried it in case you desired. Those thoughts in addition served like the easy way to fully grasp that many people have the same dream just like my personal own to grasp significantly more with regards to this problem. Certainly there are several more enjoyable opportunities ahead for many who read through your blog post.

I want to express my appreciation to the writer just for rescuing me from this problem. Right after browsing through the world wide web and obtaining basics which were not powerful, I assumed my entire life was over. Being alive minus the approaches to the issues you have solved through your good article content is a critical case, as well as those which might have negatively affected my career if I had not discovered your blog. Your actual know-how and kindness in handling all the stuff was vital. I don't know what I would've done if I had not come upon such a subject like this. I'm able to at this time look ahead to my future. Thanks a lot so much for this expert and amazing help. I won't hesitate to suggest your blog to anyone who should get guidance on this subject matter.

I have to show some thanks to this writer for bailing me out of this difficulty. Because of scouting throughout the world-wide-web and seeing solutions which were not powerful, I assumed my entire life was done. Being alive without the presence of solutions to the difficulties you have sorted out as a result of this posting is a crucial case, and ones which may have badly affected my entire career if I hadn't come across your web blog. Your main ability and kindness in touching all the stuff was helpful. I'm not sure what I would've done if I had not encountered such a step like this. It's possible to at this point relish my future. Thanks a lot so much for the skilled and results-oriented guide. I will not hesitate to suggest your blog to any individual who needs and wants assistance about this issue.

I wish to get across my gratitude for your kindness supporting men who actually need guidance on this one issue. Your personal commitment to getting the message all through appears to be certainly good and has in every case allowed workers like me to reach their dreams. Your own valuable suggestions denotes a whole lot to me and a whole lot more to my peers. Thanks a lot; from everyone of us.

I needed to draft you this little word to help say thank you as before for all the magnificent pointers you have discussed in this case. It is simply extremely open-handed of people like you to present openly precisely what a number of people could have sold as an electronic book to help make some money for themselves, notably since you could possibly have tried it if you decided. Those strategies in addition acted as the good way to be sure that other people online have a similar dream much like my personal own to realize somewhat more related to this problem. Certainly there are millions of more fun occasions in the future for individuals who examine your blog.

My wife and i were happy that Louis could carry out his research through your precious recommendations he grabbed through the weblog. It is now and again perplexing to just possibly be giving freely methods others might have been trying to sell. Therefore we take into account we have got you to be grateful to for this. The type of explanations you've made, the simple website menu, the friendships your site help to create - it's got many unbelievable, and it is facilitating our son in addition to us do think the issue is thrilling, which is rather vital. Thanks for everything!

I wanted to draft you a tiny word so as to thank you the moment again on your pleasing suggestions you have documented here. It is so tremendously generous with you to allow extensively all a number of people could have distributed for an e-book in making some dough on their own, most notably given that you might well have done it in the event you decided. These smart ideas additionally acted to become fantastic way to know that the rest have the identical dream just as my personal own to realize good deal more concerning this condition. I am certain there are millions of more pleasurable opportunities up front for those who looked at your blog.

My spouse and i got glad when John managed to conclude his preliminary research using the ideas he acquired from your very own blog. It is now and again perplexing to just choose to be offering steps which some other people could have been making money from. And we fully understand we've got the blog owner to thank for this. The specific explanations you made, the simple site menu, the relationships you will make it possible to engender - it's mostly amazing, and it is helping our son and us recognize that this subject matter is cool, and that is extraordinarily pressing. Many thanks for all!

I precisely wanted to thank you so much yet again. I'm not certain the things I might have gone through without the recommendations shared by you over such a topic. Entirely was an absolute distressing scenario in my position, but spending time with the very skilled manner you processed the issue made me to jump with contentment. Now i am happy for the help and thus pray you really know what an amazing job you have been getting into instructing most people with the aid of a site. I am certain you've never encountered all of us.

I simply wanted to write down a simple message so as to appreciate you for those fabulous techniques you are giving here. My prolonged internet investigation has at the end of the day been paid with sensible content to write about with my companions. I would assume that most of us readers actually are quite blessed to live in a remarkable place with so many wonderful people with insightful tips. I feel really privileged to have used your entire web site and look forward to really more excellent times reading here. Thanks a lot once again for a lot of things.

I wish to show my thanks to this writer just for bailing me out of such a issue. Just after looking out throughout the online world and seeing solutions which are not powerful, I was thinking my entire life was gone. Existing minus the solutions to the problems you have solved as a result of your good article content is a crucial case, and those which may have adversely affected my career if I had not noticed your blog post. Your main know-how and kindness in playing with all areas was priceless. I don't know what I would've done if I had not come upon such a thing like this. I can at this point relish my future. Thanks a lot so much for your expert and results-oriented help. I will not think twice to propose the sites to any person who should get assistance about this subject.

I simply had to appreciate you once more. I do not know the things I might have carried out in the absence of these information provided by you concerning such a problem. Completely was a fearsome circumstance for me, nevertheless viewing this specialized manner you dealt with that took me to jump with delight. I'm happier for the assistance and then trust you comprehend what a great job you have been getting into instructing the rest by way of your webblog. I'm certain you've never encountered any of us.

I am only commenting to make you be aware of what a incredible experience our child gained checking your webblog. She picked up a wide variety of things, with the inclusion of what it's like to have an ideal teaching nature to let many others just know a variety of advanced subject matter. You really exceeded our expected results. Many thanks for offering those useful, trusted, explanatory and even easy guidance on this topic to Sandra.

I simply needed to appreciate you once more. I am not sure the things that I would've carried out without the ideas documented by you directly on this topic. It became a real challenging scenario in my position, nevertheless observing your specialised avenue you processed the issue made me to cry over fulfillment. I will be grateful for your service and then have high hopes you find out what a great job you happen to be carrying out teaching the rest by way of your webblog. I know that you've never encountered any of us.

Thanks for your own hard work on this web page. My mom really likes managing research and it's really easy to understand why. A lot of people learn all relating to the lively mode you offer simple things via your web blog and as well improve response from people on the subject while our favorite child is certainly starting to learn a great deal. Take pleasure in the rest of the new year. You are always performing a terrific job.

I in addition to my guys ended up viewing the good information and facts from your site then all of the sudden developed a horrible feeling I had not thanked the web site owner for those strategies. My men are already happy to read them and have definitely been taking advantage of them. Thank you for simply being really kind and also for getting variety of useful information millions of individuals are really desirous to be aware of. Our honest apologies for not expressing appreciation to you earlier.

Thanks for each of your efforts on this web site. My daughter really loves doing internet research and it's really easy to understand why. I learn all regarding the compelling mode you create very helpful ideas through the web blog and in addition attract contribution from others on this subject matter then our girl is undoubtedly studying so much. Take pleasure in the remaining portion of the new year. You are always carrying out a great job.

I precisely needed to thank you very much yet again. I am not sure the things I could possibly have gone through without these pointers provided by you concerning my subject. It seemed to be the difficult situation in my circumstances, nevertheless discovering this professional way you managed the issue made me to cry over gladness. I'm thankful for this service and in addition pray you know what an amazing job you are always carrying out teaching many people thru your webpage. Probably you've never encountered all of us.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.