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:
Contents
Aim of Experiment
To calculate distance of the object using ultrasonic sensor.
Components Required
- NodeMCU – 1
- Ultrasonic Sensor – 1
- Breadboard – 1
Connections Diagram (Schematic)
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);
}
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply