Tuesday, October 18, 2022

Node MCU with Blynk IOT to calculate distance using ultrsonic sensor

 


Circuit Diagram:


Program Code:

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>


#define  trig  D2

#define  echo  D1


long duration;

int distance;


#define BLYNK_TEMPLATE_ID "**********"

#define BLYNK_DEVICE_NAME "************"

#define BLYNK_AUTH_TOKEN "*******************"

char auth[] = BLYNK_AUTH_TOKEN;         // You should get Auth Token in the Blynk App.

char ssid[] = "******";   //Enter your WIFI name

char pass[] = "********";   //Enter your WIFI password

BlynkTimer timer;

WidgetLCD lcd(V1);

void setup()

{

  // Debug console

  pinMode(trig, OUTPUT);  // Sets the trigPin as an Output

  pinMode(echo, INPUT);   // Sets the echoPin as an Inpu

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);

  timer.setInterval(1000L, sendSensor);

}


void loop()

{

  Blynk.run();

  timer.run();

}

void sendSensor()

{

  digitalWrite(trig, LOW);   // Makes trigPin low

  delayMicroseconds(2);       // 2 micro second delay


  digitalWrite(trig, HIGH);  // tigPin high

  delayMicroseconds(10);      // trigPin high for 10 micro seconds

  digitalWrite(trig, LOW);   // trigPin low


  duration = pulseIn(echo, HIGH);   //Read echo pin, time in microseconds

  distance = duration * 0.034 / 2;   //Calculating actual/real distance


  Serial.print("Distance = ");        //Output distance on arduino serial monitor

  Serial.println(distance);


  Blynk.virtualWrite(V0, distance);

  

  lcd.print(0, 0, "trendy_coding"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")

  lcd.print(0, 1, "Distance: " + String(distance) + "cm  ");

  delay(1000); 


}


No comments:

Post a Comment

GPS sensor interface with ESP8266 using Blynk IoT cloud

   Circuit diagram: Source Code: #include <TinyGPS++.h> #include <SoftwareSerial.h> #define BLYNK_PRINT Serial #include <ESP8...