Measure Distance Using Arduino and HC-SR04 Ultrasonic Sensor


Accurate distance measurement is essential in various applications, from robotics to home automation. The HC-SR04 Ultrasonic Sensor is a reliable and cost-effective solution for measuring distances using sound waves. In this comprehensive guide, we will explore how to Measure Distance Using Arduino and HC-SR04 Ultrasonic Sensor, how it works, and how to interface it with an Arduino UNO to measure distances accurately. By the end of this tutorial, you’ll have the knowledge and skills to integrate the HC-SR04 Ultrasonic Sensor into your projects, enabling them to measure distances with precision. Let’s dive into the world of distance measurement with Arduino and unlock the potential of the HC-SR04 sensor!

Hardware Required

You will require the following Hardware Components for Measure Distance Using Arduino and HC-SR04 Ultrasonic Sensor. Buy From amazon.

Components#Buy From Amazon
Arduino UNO1Buy Link
HC-SR04 Ultrasonic Sensor1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires4Buy Link
Breadboard1Buy Link

What is the Module?

The HC-SR04 sensor employs ultrasonic sound waves to measure distances. It consists of two main components: an ultrasonic transmitter and receiver. The transmitter emits a high-frequency sound wave, which then travels through the air until it encounters an obstacle. When the sound wave hits an object, it bounces back to the sensor’s receiver. The sensor measures the time it takes for the sound wave to travel to the object and back, and from this, it calculates the distance using the speed of sound.


  1. Measurement Range: The HC-SR04 sensor can typically measure distances from 2 cm to 400 cm, making it suitable for a wide range of applications.
  2. High Accuracy: It offers high measurement accuracy, typically within a few millimeters.
  3. Digital Output: The sensor provides distance measurements in digital form, making it easy to interface with microcontrollers like the Arduino UNO.
  4. Compact Size: The sensor’s small form factor allows for easy integration into various projects.


  1. Non-contact Measurement: The sensor can measure distances without physically touching the object, making it suitable for applications where contact is not possible or desired.
  2. Wide Applicability: It finds use in robotics, obstacle avoidance systems, automatic door openers, and many other projects.
  3. Low Power Consumption: The HC-SR04 is energy-efficient, making it suitable for battery-powered applications.



Pinout Configuration

Pin NameDescription
VccThe Vcc pin powers the sensor, typically with +5V
TriggerTrigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending US wave.
EchoEcho pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor.
GroundThis pin is connected to the Ground of the system.

Circuit Diagram

The following circuit shows you the connection of the Measure Distance Using Arduino and HC-SR04 Ultrasonic Sensor Please make the connection carefully

Measure Distance Using Arduino and HC-SR04 Ultrasonic Sensor-Circuit

Circuit Connections

ArduinoUltrasonic Sensor

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow


//For more Projects:

const int EchoPin = 5;
const int TriggerPin = 6;

void setup() {
  pinMode(TriggerPin, OUTPUT);
  pinMode(EchoPin, INPUT);

void loop() {
  int cm = ping(TriggerPin, EchoPin);
  Serial.print("Distance: ");

int ping(int TriggerPin, int EchoPin) {
  long duration, distanceCm;
  digitalWrite(TriggerPin, LOW); //to generate a clean pulse we set LOW 4us
  digitalWrite(TriggerPin, HIGH); //generate Trigger of 10us
  digitalWrite(TriggerPin, LOW);
  duration = pulseIn(EchoPin, HIGH); //we measure the time between pulses, in microseconds
  distanceCm = duration * 10 / 292/ 2; // we convert to distance, in cm
  return distanceCm;


  1. Obstacle Avoidance: Use the sensor in robotics to detect and avoid obstacles in real time.
  2. Distance Measurement: Create projects that measure distances for applications like parking assistance, liquid level detection, or object detection.
  3. Security Systems: Integrate the sensor into security systems to detect the presence of objects or people in specific areas.
  4. Smart Home: Use it in home automation projects, such as automatic lighting control when a person enters a room.


You now have a comprehensive understanding of the HC-SR04 Ultrasonic Sensor and its applications in distance measurement projects. By interfacing the sensor with an Arduino UNO, you can create projects that accurately measure distances, opening up possibilities in robotics, automation, and more. The sensor’s reliability and ease of use make it a valuable tool for achieving precise distance measurements. With this newfound knowledge, you’re prepared to integrate the HC-SR04 Ultrasonic Sensor into your projects and enhance their ability to measure distances with accuracy. So, embrace the world of distance measurement, and let the HC-SR04 sensor bring precision to your creative endeavors!

Leave a Comment