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 the distance using HC-SR04.

Before building the circuit let me explain what is an HC-SR04 sensor??

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.

Now let's get into constructing the hardware.

Step 1: Components Required

Hardware Requirements
HC-SR04 (Ultra-sonic Sensor)
Bread Board
Jumper Wires
Micro USB Cable

Software Requirements
Arduino IDE
Let's start implementing it.


Step 2: Description


Power supply: 5v DC
Ranging distance : 2cm – 500 cm
Ultrasonic Frequency: 40k Hz

Step 3: Working of HC-SR04

HOW does IT work?
well, actually we have to figure out the distance because the sensor itself simply holds it's "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.

On the base of the distance, 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 not 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 to the 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
* ESP8266 Node MCU & Ultrasonic HCSR-04[ 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);
// 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: ");

Next, The best project using ESP8266


Product added to your Cart

We use cookies to ensure that we give you the best experience on our website. If you continue we'll assume that you understand this. Learn more

Got It