We will learn about HC-SR04 widely known as Ultrasonic Sensor, how it works, and how to interface with the NodeMCU. And also how to measure distance using ultrasonic sensor & ESP8266.
Before building the circuit let me explain what an HC-SR04 Ultrasonic sensor is??
As the name indicates, ultrasonic sensors measure distance by using ultrasonic waves.
But how? The sensor head emits an ultrasonic wave and receives the wave reflected back from the target.
Ultrasonic Sensors measure the distance to the target by measuring the time between the emission and reception. So there ends the technical definition, I don’t know how many of you understood.

In simple words, An Ultrasonic sensor is a device that can measure the distance of an object by using sound waves.
It measures distance by sending out a sound wave at a specific frequency and waits for that sound wave to bounce back.
By recording the time taken between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sensor and the object.
And now the question is how to measure distance using an ultrasonic sensor?
Table of Contents
HC-SR04 Ultrasonic Sensor Pinout

HCSR04 specifications
Operating Voltage | 5V DC |
Operating Current | 15mA |
Operating Frequency | 40KHz |
Min Range | 2cm / 1 inch |
Max Range | 400cm / 13 feet |
Accuracy | 3mm |
Measuring Angle | <15° |
Dimension | 45 x 20 x 15mm |
Now let’s get into constructing the hardware.
Step 1: Components Required
✔ Hardware Requirements
NodeMCU – —————————- Amazon
HC-SR04 (Ultra-sonic Sensor) — Amazon
Bread Board ————————– Amazon
Jumper Wires ———————— Amazon
Micro USB Cable ——————– Amazon
✔ Software Requirements
Arduino IDE ————————- Free Download
Let’s start implementing it.
Step 2: Description
✔ SPECIFICATION of HC-SR04
Power supply: 5v DC
Ranging distance : 2cm – 500 cm
Ultrasonic Frequency: 40k Hz
Step 3: Working on HC-SR04
HOW does IT work?
well, actually we have to figure out the distance because the sensor itself simply holds its “ECHO” pin HIGH for a duration of time corresponding to the time it took to receive the reflection (echo) from a wave is sent.
The module sends out a burst of sound waves, at the same time it applies voltage to the echo pin.
The module receives the reflection back from the sound waves and removes voltage from the echo pin.

For that purpose we are using the following basic formula for calculating distance:
Distance = Speed x Time
Let’s say the Echo pin was HIGH for 2ms. If we want the get the distance result in cm, we can convert the speed of sound value from 340m/s to 34cm/ms.
Distance = (Speed x Time) / 2 = (34cm/ms x 1.5ms) / 2 = 25.5cm.
So, if the Echo pin was HIGH for 2ms (which we measure using the pulseIn() function), the distance from the sensor to the object is 34cm.
On the distance base, a pulse is generated in the ultrasonic sensor to send the data to NodeMCU or any other micro-controller.
The starting pulse is about 10us and the PWM signal will be 150 us-25us on the base of the distance.

If no obstacle is there, then a 38us pulse is generated for Node MCU to confirm that there are no objects detected.
Before getting the reading of the HC-SR04 knows about the calculation.
Step 4: Interface HC-SR04
✔ The circuit connections are made as follows:
The HC-SR04 sensor attaches to the Breadboard
The sensor Vcc is connected externally to +5v
The sensor GND is connected to the Node MCU GND
The sensor Trigger Pin is connected to the Node MCU Digital I/O D1
The sensor Echo Pin is connected to the Node MCU Digital I/O D2

Before you get started with coding you need Arduino IDE.
Step 5: Coding Time of How to measure distance using ultrasonic sensor
/* * How to measure distance using ultrasonic sensor HCSR-04 & ESP8266 Node MCU[ Accurate measurement ] * * Crated by Abhay Kalmegh * www.pictorobo.com * */ // defines pins numbers const int trigPin = 5; // pin no. 5 is D1 ESP8266 const int echoPin = 4; // pin no. 5 is D2 ESP8266 // defines variables long duration; int dist; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication @9600 } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance dist= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(dist); }
Step 6: Watch Video on how to measure distance using an ultrasonic sensor & ESP8266 step-by-step guide
Other Useful Links For You

I’m Abhay Kalmegh coding and electronics hobbyist professionally web developer, software engineer, and SEO Analyst currently living in Nagpur, India. My interests range from technology to web development. I am also interested in programming, entrepreneurship, and education.
You can click the button above to visit my website. If you’d like to get in touch, feel free to say hello through any of the social links below.
Hi there, I enjoy reading through your post. I like to write a little comment to support you.
I like it when people come together and share ideas.
Great blog, keep it up!