Blynk Ultrasonic sensor | HC-SR04

What’s up guys, I’m back with another interesting article on IoT project i.e., Blynk Ultrasonic Sensor. Today we’re going to talk about how can you check distance with Ultrasonic Sensor HC-SR04.  Also, we’ll directly display this on the Blynk App. This sensor is easily available in the market also it is very common and easy to use. But like every coin has two sides. Sometimes it shows the wrong distance. There is also a way to alter the library and change the distance to amend the error. So let’s begin!!!.

HC-SR04:

HC-SR04 is a really common distance sensor available in the market. It is very easy to use and gives almost correct detection of distance. But as stated above, it sometimes gives some error in the distance. Beginners requested to first view the Arduino tutorial on HC-SR04. This sensor works on the principle of reflection of sound. This sensor works flawlessly with any device like Arduino.

It works like this, first, a sensor sends a beam of sound for some time and then receives the sound back after it reflects it from any obstacle. Basically, it records the time in milliseconds during which ultrasonic sound has been sent and received. the pulse function is used for this purpose. But in this tutorial, we’ll use some manual commands to control the Ultrasonic sensor. You may also use the library for simplicity.

NOTE: This sensor is suitable for distances within 4 m. As the range of this sensor is 4 m. But in the libraries available on the internet you will see that there is a limit of 50 cm. This can be rectified, but you need to make some changes in the .h & .cpp files of the library.

Material Required:

  • NodeMCU (ESP8266 MOD)
  • HC-SR04 Ultrasonic sensor
  • Jumper wire
  • Breadboard
  • Phone with Blynk App installed and Wi-Fi connection

Circuit Design:

Nodemcu esp8266Ultrasonic Sensor
Vin , VV ,+5VCC ( Positive + )
G , GNDGND ( Ground – )
D1 PinTrig Pin
D2 PinEcho Pin

Code & Explanation:

Explanation:

First, we include two main libraries needed, i.e., ESP8266WiFi & BlynkSimpleesp8266. Then we define Echo & Trig pins of Ultrasonic sensor to D1 & D2. Now we enter our auth code and Wi-Fi credentials. Also, we create two object LCD(v1), which is connected to a virtual pin of Blynk App & BLYNK_PRINT which is used to print text on the Blynk app virtual LCD.

 //TECHATRONIC.COM  
 // BLYNK LIBRARY  
 // https://github.com/blynkkk/blynk-library  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #define BLYNK_PRINT Serial  
 #include <ESP8266WiFi.h>  
 #include <BlynkSimpleEsp8266.h>  
 #define TRIGGERPIN D1  
 #define ECHOPIN  D2  
 // You should get Auth Token in the Blynk App.  
 // Go to the Project Settings (nut icon).  
 char auth[] = "your-auth-code";  
 // Your WiFi credentials.  
 // Set password to "" for open networks.  
 char ssid[] = "your-wifi-ssid";//your wifi name  
 char pass[] = "your-wifi-password"; //your wifi password  
 WidgetLCD lcd(V1);

In the setup section, we define the pinMode of pins of the Ultrasonic sensor and start communication with Blynk App.

In the loop section, we start our main code and print the distance on the virtual screen.

Code:

Here is the main code

 //TECHATRONIC.COM  
 // BLYNK LIBRARY  
 // https://github.com/blynkkk/blynk-library  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #define BLYNK_PRINT Serial  
 #include <ESP8266WiFi.h>  
 #include <BlynkSimpleEsp8266.h>  
 #define TRIGGERPIN D1  
 #define ECHOPIN  D2  
 // You should get Auth Token in the Blynk App.  
 // Go to the Project Settings (nut icon).  
 char auth[] = "your-auth-code";  
 // Your WiFi credentials.  
 // Set password to "" for open networks.  
 char ssid[] = "your-wifi-ssid";//your wifi name  
 char pass[] = "your-wifi-password"; //your wifi password  
 WidgetLCD lcd(V1);  
 void setup()  
 {  
  // Debug console  
  Serial.begin(9600);  
 pinMode(TRIGGERPIN, OUTPUT);  
  pinMode(ECHOPIN, INPUT);  
  Blynk.begin(auth, ssid, pass);  
  // You can also specify server:  
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);  
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);  
  lcd.clear(); //Use it to clear the LCD Widget  
  lcd.print(0, 0, "Distance in cm"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")  
  // Please use timed events when LCD printintg in void loop to avoid sending too many commands  
  // It will cause a FLOOD Error, and connection will be dropped  
 }  
 void loop()  
 {  
  lcd.clear();  
  lcd.print(0, 0, "Distance in cm"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")  
  long duration, distance;  
  digitalWrite(TRIGGERPIN, LOW);   
  delayMicroseconds(3);   
  digitalWrite(TRIGGERPIN, HIGH);  
  delayMicroseconds(12);   
  digitalWrite(TRIGGERPIN, LOW);  
  duration = pulseIn(ECHOPIN, HIGH);  
  distance = (duration/2) / 29.1;  
  Serial.print(distance);  
  Serial.println("Cm");  
  lcd.print(7, 1, distance);  
  Blynk.run();  
  delay(1000);  
 }  

Blynk App:

Now for displaying the distance, we have to install and configure Blynk App. PlayStore | Apple Appstore

Now after installing and logging into the App create a new project with suitable name.

After creation, you’ll receive the auth-code on your registered email-id.

Later it will be visible like this (main screen).

Now use widget tray to create a virtual display to display distance from ultrasonic sensor.

Afterwards on adding display the main project screen will look something like this.

Configure the settings of the display as shown above.

Now the screen will be visible like this, after configuring settings like above.

And it’s done, you are ready to go to get the distance.

Afterward, connect NodeMCU to the power supply and wait for output on Serial Monitor of Arduino IDE. The output will seem like this:

Blynk Ultrasonic Sensor

With this, we come to the end of this article on the Blynk Ultrasonic sensor. I hope you like it, But if you need my help in any of the steps, then comment to me below.

Leave a Comment