Distance Measurement Using HC-SR04 Via NodeMCU




About: " Work until you no longer have to introduce yourself " Show some love on Instagram @it.is.prem & Motivate me on YouTube @TheCircuit

Hey Maker's,

I'm back with a new instructable.

In this Instructable 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 a 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

List of components required for the instructable :

Hardware Requirements

  • NodeMCU
  • HC-SR04 (Ultra-sonic Sensor)
  • Bread Board
  • Jumper Wires
  • Micro USB Cable

Software Requirements

  • Arduino IDE

Let's start implementing it.

Step 2: Description


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

Step 3: Working of HC-SR04


Hmm, 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 it sent.

  1. The module sends out a burst of sound waves, at the same time it applies voltage to the echo pin.
  2. 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 NodeMCU to confirm that there are not objects detected.

Before getting the reading of the HC-SR04 know about the calculation.


D = 1/2 × T × C
where D is the distance, T is the time between the Emission and Reception, and C is the sonic speed.
(The value is multiplied by 1/2 because T is the time for go-and-return distance.)

Step 4: Interface HC-SR04

The circuit connections are made as follows:

The HC-SR04 sensor attach to the Breadboard

The sensor Vcc is connected to the NodeMCU +3.3v

The sensor GND is connected to the NodeMCU GND

The sensor Trigger Pin is connected to the NodeMCU Digital I/O D4

The sensor Echo Pin is connected to the NodeMCU Digital I/O D3

Before you get started with coding you need Arduino IDE.

To download Arduino IDE and for NodeMCU setup, you can check my previous instructacle.

Interfacing Servo Motor With NodeMCU

Step 5: Coding Time

// defines pins numbers

const int trigPin = 2; //D4
const int echoPin = 0; //D3

// defines variables
long duration; int distance;

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 }

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 distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); delay(2000); }

Download the "Ultrasonic_Sensor.ino" file and open it up in the Arduino IDE.

Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload.

You can tinker with it if you like based on the application, or just use it as it is.

Step 6: Output Demonstration

Now, you're done with your interface.

You can see the readings of the ultrasonic sensor in the serial monitor.

Well, now lets measure the distance of the object.

Note : You can also interface LED's and Buzzer to make your project look crazy.

Step 7: Measurement of Distance

We can determine the distance because we know how long it took for the sound waves to travel out from and back to the module (by how long the echo pin was HIGH) and we know the speed of sound in the air. But I am not getting into that. We're going to let NodeMCU figure out that stuff.

Look at the distance in the serial monitor of the object placed in front of the sensor, the sensor gives an accurate measurement range from 2 - 400cm.

That's all makers! That's how the Ultrasonic sensor works.

Stay Tuned for more Projects!

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017



  • Backyard Contest

    Backyard Contest
  • Classroom Science Contest

    Classroom Science Contest
  • Beauty Tips Contest

    Beauty Tips Contest

25 Discussions


7 months ago

Watch Out! As a good electro-engeneer you use the right logic-levels!
Therfore I used a 2 chanel IIC Level Convertor Module (zie Julian Ilett op 6:30 min)


7 months ago

HC-SR04 needs 5v
HC-SR04P can use 3.3V - 5V


9 months ago

hey bro i want to interface this with thingspeak so how can i do it.
please help me


Question 11 months ago on Step 7


This was very useful for me. Can you pls guide me on how send this data to the thinger io flatform?

Thank you!!


Reply 11 months ago

Connect the HC-SR04 to the board Vin pin, which is 5V and use a voltage divider with Resistors to convert the Echo pin logic to 3.3v


Answer 11 months ago

Connect the HC-SR04 to the board Vin pin, which is 5V and use a voltage divider with Resistors to convert the Echo pin logic to 3.3v


1 year ago

Nice and quite useful article! However, my HC-SR04 did not work with 3.3V output taken from Node MCU. I had to supply 5V from an external source to make it work.

2 replies

Reply 11 months ago

Connect the HC-SR04 to the board Vin pin, which is 5V and use a voltage divider with Resistors to convert the Echo pin logic to 3.3v


Tip 11 months ago

Usually the HC-SR04 needs 5V, that can be one of the reasons why some of you only get 0 as output in the serial monitor (it happened to me).

To solve this, simply connect your HC-SR04 to the Vin pin of the ESP8266, which is 5V (if you power the board with USB).

As the NodeMCU logic level is 3.3V, you can convert the Echo output of the sensor back to 3.3V using a Resistors divider (I personally used 1k/2k resistors).


12 months ago

You can connect your HC-SR04 to the Vin exit of the ESP8266 if you're powering it via USB, as the Vin exit of the ESP8266 gets the power directly from the USB, which is 5V.


1 year ago

#define TRIG_PIN 2 //D4
#define ECHO_PIN 0 //D3

void setup()

void loop()
// Clears the trigPin for 2 microseconds
digitalWrite(TRIG_PIN, LOW);

// Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
digitalWrite(TRIG_PIN, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(ECHO_PIN, HIGH);

// Prints the duration on the Serial Monitor
Serial.print("Duration (us): ");

// Calculating the distance ( distance = (duration/1.000.000)*(1/2) x (0.034 x 1000.000 cm/s) )
float distance = duration*0.017;

// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");




1 year ago

i change the supply font to 5,5V and it works

distance variable must be float, no int.


1 year ago

My code only returns

Duration (us): 5
Distance (cm): 0.085

Tanmay Deuskar

1 year ago

How did you get it working with 3.3V. Mine doesn't seem to work without 5V.


1 year ago

I wonder that the HC-SR04 works with 3V3. Normally 5V is required.

1 reply