IOT Water level monitoring using Ultrasonic Sensor
Table of Contents
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
- Complete circuit diagram explanation.
- Blynk Application designing
- Arduino and Nodemcu programming and finally
- 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:
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Circuit Diagram:
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…..
set the project name as Water tank…..
Click on the choose device and select nodemcu….
make sure you set the connection type to wifi….
then click on the create button…
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 …
Now click on the screen and search for the level v widget and add it…..
Click on the level v widget set the title as water level.
now click on the pin and select virtual pin v2…
set the minimum value to 60 and maximum value to 0…
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…
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…
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
// 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#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 = ""; } |
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
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.
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
can i get the flowchart of this project ?
can u send the PCB work ?
How to send the notification for the water level in mobile
In this project
Use the notification widget. if you don’t know. visit my YouTube channel “Electronic Clinic” and search for the notification widget.
what if I just uses Arduino uno, nodeMCU ,ultrasonic sensor with led and buzzer.. I hope you understand what I mean
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.
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!
Saya mencoba nya tapi terdapat error di proram arduino Uno nya pesan error ‘getValue’ was not declared in this scope
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.