Distance Measurement using HC-SR04 Ultrasonic Sensor and Arduino

Distance Measurement using HC-SR04 Ultrasonic Sensor and Arduino

Arduino Projects

Overview

In this tutorial, we will learn to make Distance Measurement using the HC-SR04 Ultrasonic Sensor and Arduino. We will also learn the Working Principle of HC-SR04 Ultrasonic Sensor and Hence Provide a Program Code to measure the distance. Hope you’ll enjoy it.

About this Project

Honestly, I always love these types of tracking devices. Because I came to know about this tiny device from spy movies. I thought they were so cool and hoped to get one on my hands. I searched on many online stores to buy a similar type of products but, I came to know that they don’t sell those sorts of the device outside. Now, my curious nature started thinking If I could manage to build myself. Few days before I had bought an Arduino Starter Kit. Suddenly I opened the box and search for the sensor. Actually, The ultrasonic sensor that was included in my Arduino Starter Kit was the main reason I got into this. 

I started looking up for projects online, found numerous tutorial but, none of them work for me. I tried to make my “dream-device” through my coding skills. And Finally, the end was pretty much worth. I know, it is missing a lot of features but, also it was pretty much a Kickstarter for me to follow new projects afterward. Hopefully, those who are just getting into this stuff, will be a Kickstarter project for everyone, and I hope you’ll enjoy it! 

Components and Supplies

The Components required for this project are mentioned in the following points: 

* Arduino UNO 

* USB Cable 

* Ultrasonic HC-SR04 

* LCD1602 Screen 

* 10kΩ Potentiometer 

* Few jumper wires 

Working Principle of HC-SR04 Ultrasonic Sensor

This tutorial uses the popular Parallax PING ultrasonic distance sensor HC-SR04 to measure the distance to an object. The range of this module is ranging from 2cm to around 4m. Actually, the Ultrasonic sensors provide the measurement of the time that it takes for sound to bounce off an object and return back to the sensor. The “ping” sound pulse is generated when the pingPin level goes HIGH for 10 micro-seconds. The module will generate a pulse that gets terminated when the sound returns back. The width of the pulse is proportional to the distance of the sound traveled and we use the pulseIn() function to measure that duration. 

Distance Measurement using HC-SR04 Ultrasonic Sensor and Arduino
Working Principle of HC-SR04 Ultrasonic Sensor

We all know the speed of sound is 340 meters per second, which is 29 microseconds per centimeter. The formula to calculate the distance of the trip is( RoundTrip) = microseconds / 29. Hence, the formula for the one-way distance in centimeters is: microseconds / 29 / 2 

Circuit Diagram of this project

  Know More About Arduino from here: Getting Started with Arduino UNO | Getting Started For Beginners

  • LCD VSS pin to ground
  • LCD VDD pin to 5V
  • LCD Vo Pin to Center Pin of Potentiometer
  • LCD RS pin to digital pin 12
  • LCD R/W pin to ground
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • Potentiometer Ends to +5V and ground
  • HC_SR04 Ultrasonic VCC pin to +5v
  • HC_SR04 Ultrasonic GND pin to GND
  • HC_SR04 Ultrasonic Trig pin to Digital pin 7
  • HC_SR04 Ultrasonic Echo pin to  Digital pin 6
Circuit Distance Measurement using HC-SR04 Ultrasonic Sensor and Arduino

Source Codes/Program

First of all, you have to define TrigPin and EchoPin on the program codes. In this project, I have set TrigPin to the Digital Pin 7 and EchoPin to Digital Pin 6 on the Arduino Board. Similarly, the long variable named “Duration” to get travel time from the sensor. And integer variable is used for “Distance”. In the setup, we need to define The Echopin as an INPUT and TrigPin as an OUTPUT.  

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 7;
const int echoPin = 6;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}

Video Tutorial

Conclusion

So with this, we came to the conclusion of this Distance Measurement using the HC-SR04 Ultrasonic Sensor and Arduino Project. But, we are going to come up with a lot more interesting articles and various Projects on Arduino and IoT (Internet of Things). So if there’s some project that really you want to know more about please allow us to know within the comment section below.

With this thank you and goodbye I hope you have enjoyed reading this article. Please be kind enough to share it on social media.  You can comment on any of your doubts and queries and we will reply to them as soon as possible. Actually, you subscribe to our website newsletter to learn more happily.

Alsan Parajuli

I am a WordPress enthusiast, a hardworking and highly positive person. I always believes in practicality rather than theoretical knowledge. With my curiosity and fast learning skills, I managed to learn everything on my own. I love coding, editing, writing and rummaging around Internet. I am passionate about IoT Projects, Digital marketing, website designing, and reviewing. Moreover, I had been contributing to WordPress Biratnagar as an active member since 2018.

https://theiotprojects.com/

1 thought on “Distance Measurement using HC-SR04 Ultrasonic Sensor and Arduino

Leave a Reply