Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Computer Science » IoT Lab » Ultrasonic Sensor with NodeMCU ESP8266
Suryateja Pericherla Categories: IoT Lab. No Comments on Ultrasonic Sensor with NodeMCU ESP8266
0
(0)

In this article we will learn how to implement Ultrasonic Sensor with NodeMCU ESP8266. If your are new to Internet of Things (IoT), learn about IoT by visiting our Internet of Things tutorial for beginners.

 

Watch this video to learn using ultrasonic sensor with NodeMCU and ESP8266:

 

Aim of Experiment

To calculate distance of the object using ultrasonic sensor.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Components Required

  • NodeMCU – 1
  • Ultrasonic Sensor – 1
  • Breadboard – 1

 

Connections Diagram (Schematic)

Ultrasonic Sensor with NodeMCU ESP8266

 

Code (C++/Arduino IDE)

const int trigger_pin = D6;
const int echo_pin = D7;
long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  pinMode(trigger_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //Clear trigger pin
  digitalWrite(trigger_pin, LOW);
  delayMicroseconds(2);

  //Set trigger pin to HIGH for 10 microseconds
  digitalWrite(trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin, LOW);

  //Read time taken by sound wave to travel in microseconds using echo pin
  duration = pulseIn(echo_pin, HIGH);

  //Calcuate the distance in cm
  distance = duration * 0.034/2;

  Serial.print("Distance = ");
  Serial.println(distance);
  delay(1000);
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory