HOW TO MEASURE DISTANCE USING ULTRASONIC HCSR04 & ARDUINO

In this blog, we are discussed how to measure distance using ultrasonic HCSR04 sensor and Arduino. Let’s take some information about the Ultrasonic HCSR04 sensor. To measure some distance we used it, but how it can work & measure the distance? That we will see in this blog.

So, Ultrasonic sensors are used in a variety of applications. Our ultrasonic sensors made with piezoelectric crystals, use high-frequency sound waves to resonate with the desired frequency and convert electric energy into acoustic energy, and vice versa.

In this what happened that Sound waves are transmitted to and reflected from the target(obstacle) back to the transducer. Targets can have any reflective form, even round. Certain variables, such as target surface angle, changes in temperature and humidity, and reflective surface roughness, can affect the operation of the sensors.

MEASURE DISTANCE USING ULTRASONIC HCSR04

The picture is of Ultrasonic HCSR04 sensor which has 4 pins as below:

  1. Vcc (power supply) pin
  2. Trig (trigger) pin
  3. Echo pin
  4. GND pin

The requirement for the project:

Hardware components:

  • Arduino UNO
  • Ultrasonic HCSR04 sensor
  • Breadboard
  • Jumper wires

Software used:

Hardware connections as follows:

  • Step1: connect the Vcc pin of Ultrasonic HCSR04 to the 5V power supply of Arduino.
  • Step2: connect Trig pin of Ultrasonic HCSR04 to the D4 digital pin of Arduino.
  • Step3: connect the Echo pin of Ultrasonic HCSR04 to the D5 digital pin of Arduino.
  • Step4: connect the GND pin of Ultrasonic HCSR04 to the GND pin of Arduino.

How does it work?

  • In Ultrasonic sensor, if the Trigger pin gets high then it will produce the sound wave.
  • That sound wave travel some distance and if there is some object , it will hit.
  • After hitting that object it will bounce back and received in the Echo pin.
  • Here, the trigger pin is input which generate sound wave of 8 cycle for 10 microsecond when it is high . Echo pin act as output which is proportionly high till the sound wave is receive.
For example: If we took one object at 10cm, to calculate it we used formula
Time= distance/speed

As we know, speed of sound is 340 m/sec so it will convert to 0.034 cm/usec because we took 10 usec trigger.

If we want to calculate distance the formula became as

Distance= time*0.034/2   

(we divide it by 2 because when a sound wave produced it will hit to object and get a bounce back, we will get total time between this duration ).

MEASURE DISTANCE USING ULTRASONIC HCSR04
MEASURE DISTANCE USING ULTRASONIC HCSR04

Now its time for coding of this project:

const int trigPin = 4;    // pin no. 5 is D4 ESP8266
const int echoPin = 5;    // pin no. 7 is D5 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);
}

Steps for getting Program Output

After coding we will follow the below steps for getting the output of the program:

  • STEP 1: Connect the Arduino to the USB port of the computer.
  • STEP 2: Open two Arduino IDE window and Select Arduino UNO as Board and select the appropriate COM27 port.
  • Board: Tools > Board > Arduino UNO.
  • STEP 3: Port: Tools > Port in Arduino(COM27 port in your case port no. can be changed) .
MEASURE DISTANCE USING ULTRASONIC HCSR04
  • STEP 4: Compile and upload the above program to your Arduino UNO board via Arduino IDE. Readings from the sensor will be obtained in the serial monitor.
  • The Output Screenshot of Serial Monitor:
MEASURE DISTANCE USING ULTRASONIC HCSR04

The output will be obtained varied as far object moved towards the sensor or away from the sensor. If the sound wave hits the object at 10cm, so we will obtain output as a 10cm distance.

Read my another blog Next and share with your friends —

Arduino Flame Sensor Interfacing to Build Fire Alarm System

Leave a Comment

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