Arduino UNO Ultrasonic Distance Measurement System
Distance measurement is an essential concept in electronics and automation systems. From robotics to parking sensors, measuring distance accurately plays a vital role in real-world applications.
In this tutorial, you will learn how to measure distance using an HC-SR04 ultrasonic sensor with Arduino UNO. This is one of the most popular beginner projects and is widely used in obstacle detection and smart automation systems.
This guide is designed to be SEO-friendly, beginner-focused, and optimized for real-world learning and Google indexing.
Why Use an Ultrasonic Sensor?
Ultrasonic sensors are widely used because they provide accurate, non-contact distance measurement. They work in various lighting conditions and are more reliable than infrared sensors in many cases.
Learning this project helps you understand sensor interfacing, timing calculations, and real-time data processing.
Required Components
Arduino UNO board
Ultrasonic Sensor (HC-SR04)
Jumper wires
Breadboard
USB cable
About HC-SR04 Ultrasonic Sensor
The HC-SR04 ultrasonic sensor measures distance using sound waves. It sends ultrasonic pulses and calculates the time taken for the echo to return.
It has four pins: VCC, GND, Trig, and Echo. The Trig pin sends the signal, and the Echo pin receives the reflected signal.
Distance is calculated using the speed of sound, making this sensor highly accurate for short-range measurements.
Circuit Connections
VCC → 5V
GND → GND
Trig → Pin 9
Echo → Pin 10
Arduino Code
int trigPin = 9;
int echoPin = 10;
long duration;
float distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
This code calculates the distance based on the time taken for the ultrasonic wave to return.
Working Principle
The ultrasonic sensor sends a high-frequency sound wave using the Trig pin. When the wave hits an object, it reflects back to the sensor.
The Echo pin measures the time taken for the wave to return. Using the speed of sound, the Arduino calculates the distance.
This process happens continuously, allowing real-time distance monitoring.
Distance Calculation Formula
Distance = (Time × Speed of Sound) / 2
Speed of sound = 0.034 cm/µs
The division by 2 accounts for the forward and return journey of the sound wave.
Key Features
Non-contact measurement
High accuracy
Low cost
Easy integration with Arduino
Applications
Obstacle detection in robots
Parking assistance systems
Water level monitoring
Smart dustbins
Distance measurement tools
Frequently Asked Questions
Q1: What is the range of HC-SR04? A: Approximately 2 cm to 400 cm.
Q2: Is ultrasonic better than IR? A: Yes, for accuracy and stability.
Q3: Can it work in darkness? A: Yes, it uses sound waves, not light.
Conclusion
The ultrasonic distance measurement system using Arduino UNO is a fundamental and highly practical project. It introduces key concepts like sensor interfacing and real-time measurement.
This project can be expanded into advanced systems like obstacle-avoiding robots and smart automation solutions.
Mastering this project builds a strong foundation for future Arduino and robotics projects.