Anip Shah
Published © GPL3+

Measuring Distance

Using Ultrasonic sensor and Esp-32 to measure distance and obtaining result on blynk cloud.

IntermediateFull instructions provided1 hour510
Measuring Distance

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
NodeMCU esp32-dev
×1

Software apps and online services

Blynk
Blynk
Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Making connection

Code

Code for this project.

C/C++
IMPORTANT!!
Device name, Template id and Auth code will be given in Blynk console itself.
Enter your SSID and Password in the code before uploading it to your device.
#include <WiFi.h>
#include <WiFiClient.h>
#define BLYNK_TEMPLATE_ID "-------------"
#define BLYNK_DEVICE_NAME "-------------"
#define BLYNK_AUTH_TOKEN "--------------"
#include <BlynkSimpleEsp32.h>

#define  trig  5
#define  echo  18

long duration;
int distance;


char auth[] = "--------";
char ssid[] = "--------";  
char pass[] = "--------"; 

BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT); 
  pinMode(echo, INPUT);   
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

 timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   
  delayMicroseconds(2);       
  digitalWrite(trig, HIGH); 
  delayMicroseconds(10);      
  digitalWrite(trig, LOW);   
  duration = pulseIn(echo, HIGH);   
  distance = duration * 0.034 / 2;  

  Serial.print("Distance = ");        
  Serial.println(distance);

  if(distance <= 5)
  {
    Blynk.tweet("My Arduino project is tweeting using @blynk_app and its awesome!\n #arduino #IoT #blynk");
    Blynk.notify("Post has been twitted");
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                        //Pause for 3 seconds and start measuring distance again
}

Credits

Anip Shah

Anip Shah

9 projects • 4 followers
Tech enthusiast and programmer who also happens to be a Biomedical Engineer.

Comments