Distance Measurement Using HC-SR04 Via NodeMCU

121,469

68

33

Introduction: Distance Measurement Using HC-SR04 Via NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

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

SPECIFICATION of HC-SR04

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

Step 3: Working of HC-SR04

HOW IT WORKS?

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.

FORMULA

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

4 People Made This Project!

Recommendations

  • Retro Tech Challenge

    Retro Tech Challenge
  • Organization Contest

    Organization Contest
  • Rocks, Gems, and Stones Speed Challenge

    Rocks, Gems, and Stones Speed Challenge

33 Comments

0
Davidroarman
Davidroarman

Question 11 months ago on Step 6

Hello i loved it please tell the baud rate or if its different for everyone please tell me how to find one
Thanks and regards,
David

0
YannickT9
YannickT9

1 year ago

Salut j'aime bien vos tutos et merci de nous partager vos recherches cela me permet d'avancer dans le domaine. J'ai un problème avec mes nodemcu (esp8266mod et esp8266 lolin V3). Le problème est celui de l'utilisation de ces derniersde façon autonome comme une carte Arduino . J'ai tant fait des recherches mais je n'y arrive pas à l'utiliser ainsi. Svp pourriez vous m'aider grâce à un tuto. Je vous remercie . Cordialement Yannick

0
shehand2
shehand2

Reply 11 months ago

quincycz thank you brother. You save my day

0
quincycz
quincycz

Tip 1 year ago

After I uploaded program, it's looks like ESP is not running, not blinking, no traffic over serial line. I had to change pins like this:

const int trigPin = 5; //D1
const int echoPin = 4; //D2

Also my HC-SR04is not working on 3.3V, I had to use VV pin instead (there is 5V coming from usb).

0
Badoumba
Badoumba

1 year ago

Thanks for this article ! As some others guys, serial monitor shows me "0", I had to connect D1 to Trigg, D2 to Echo and VCC to VU on the Nodemcu (Lolin V3). But it's better to use HC-SR04P as you can use 3.3v

0
Kshama2705
Kshama2705

Question 1 year ago

the serial monitor doesn't show anything,( even 0cm doesn't show up) it comes up empty. how do I fix this? does this mean the sensor is faulty or the esp 8266 is faulty?please help

0
SumuduV
SumuduV

2 years ago

Great.... It worked for me.... I'm using SRF-05 Sensor. It gives me accurate reading as it is.... Thanks for the tutorial.

20190915_211545.jpg
0
StevenV24
StevenV24

2 years 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)



Module.JPGschema.JPG
0
CheesyPeteza
CheesyPeteza

2 years ago

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

0
nikhil1997
nikhil1997

2 years ago

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

0
Shiroma
Shiroma

Question 3 years ago on Step 7

Hi!

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

Thank you!!

0
AlexandreD67
AlexandreD67

Reply 3 years 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

0
arunk358
arunk358

Question 3 years ago on Step 7

the output only shows distance =0 . can anyone help?

0
AlexandreD67
AlexandreD67

Answer 3 years 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

0
VidyasagarM2
VidyasagarM2

3 years 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.

0
AlexandreD67
AlexandreD67

Reply 3 years 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

0
YashP92
YashP92

Reply 3 years ago

how to connect external source for 5V?