How to measure distance using Ultrasonic HCSR04 & ESP8266 Node MCU

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.

How to measure distance using ultrasonic sensor
Ultrasonic HCSR04

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?

HC-SR04 Ultrasonic Sensor Pinout

How to measure distance using Ultrasonic HCSR04 & ESP8266 Node MCU
HCSR-04 Ultrasonic sensor pinout

HCSR04 specifications

Operating Voltage5V DC
Operating Current15mA
Operating Frequency40KHz
Min Range2cm / 1 inch
Max Range400cm / 13 feet
Measuring Angle<15°
Dimension45 x 20 x 15mm
 HCSR04 specifications

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


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.

operation of ultrasonic hcsr04
Operation of Ultrasonic HCSR04

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.

operational waveform of ultrasonic hcsr04
The operational waveform of Ultrasonic HCSR04

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

interfacing esp and ultrasonic
Interfacing diagram of ESP8266 and Ultrasonic HCSR-04

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
// 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);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
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: ");

Step 6: Watch Video on how to measure distance using an ultrasonic sensor & ESP8266 step-by-step guide

Other Useful Links For You

2 thoughts on “How to measure distance using Ultrasonic HCSR04 & ESP8266 Node MCU”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.