The HC-SR04 Ultrasonic Sensor is one of the most popular and affordable tools to measure distance without physical contact. Whether you’re building a robot that avoids obstacles or a smart parking assistant, this sensor is your go-to choice. In this blog, we’ll explore how to use the HC-SR04 ultrasonic sensor with Arduino, learn how it works, how to wire it, and how to write Arduino code to accurately measure distances in centimeters and inches.

How Does HC-SR04 UltraSonic Sensor Work?
Ultrasonic sensor is use to calculate the distance. It use ultrasonic waves to measure the distance. The ultrasonic wave can hit the object and and come back, ultrasonic sensor can use time to calculate the distance. HC-04 consists of two ultrasonic transducers. One can transmit ultrasonic waves and the other acts as receiver.

- The Trig pin sends a short 10µs pulse.
- This triggers the sensor to emit an ultrasonic wave at 40kHz.
- When the sound wave hits an object, it reflects back.
- The Echo pin goes HIGH and the time it stays HIGH corresponds to the distance.
Formula to Calculate Distance:
Distance (cm) = Duration (µs) / 58
Specifications of HC-SR04 UltraSonic Sensor:
Feature | Value |
---|---|
Operating Voltage | 5V DC |
Measuring Range | 2 cm to 400 cm |
Measuring Angle | ~15° |
Accuracy | ±3 mm |
Interface | 4-pin (VCC, GND, Trig, Echo) |
Components Required for connecting Ultrasonic Sensor with Arduino:
- Arduino UNO (or any compatible board)
- HC-SR04 Ultrasonic Sensor
- Breadboard
- Jumper Wires
- Optional: 16×2 I2C LCD for display
Pinout of HC-SR04 Ultrasonic Sensor:

- Vcc –> 5V
- Gnd –> Gnd
- Trigger –> 2
- Echo –> 3
Circuit Diagram for Connecting Ultrasonic Sensor with Arduino:
HC-SR04 Pin | Arduino Pin |
---|---|
VCC | 5V |
GND | GND |
Trig | Pin 3 |
Echo | Pin 2 |

Code for Using Ultrasonic Sensor HC-SR04 with Arduino:
#define trigPin 2
#define echoPin 3
long dur;
int dist = 0;
void setup() {
pinMode(trigPin,OUTPUT);
pinMode(echoPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
dur = pulseIn(echoPin, HIGH);
dist = dur * 0.034 / 2;
Serial.print("Distance : ");
Serial.print(dist);
Serial.println("cm");
}
Video Tutorial for Using Ultrasonic Sensor HC-SR04 with Arduino:
Applications Ultrasonic Sensor:
- Smart Car Parking Systems
- Obstacle Avoidance Robots
- Automatic Doors
- Distance Measurement Tools
- Level Sensing (water, grains, etc.)
Troubleshooting of Ultrasonic Sensor with Arduino:
Problem | Solution |
---|---|
Always reads 0 cm | Check wiring, especially Trig/Echo |
Random values | Add delay between readings |
Too slow or unstable | Use capacitor for VCC or shield wires |
Echo time too long | Object might be too far or soft |
Conclusion:
The HC-SR04 ultrasonic sensor is an incredibly useful module for any Arduino enthusiast. It helps you bring interactive and intelligent functionality to your projects with accurate distance measurement using nothing but sound.
From robotics to smart automation systems, it’s a must-have sensor in your toolkit!
Need Help in Setup of Ultrasonic Sensor with Arduino?
If you need any Help or Assistance for Setup of Ultrasonic Sensor with Arduino, with Modifications or Customization then you can contact us through WhatsApp. We can deliver you this Project in the Following Ways.
Learn More about the services we offer.
Good day! I just want to offer you a big thumbs up for your excellent information you have got here on this post. Ill be coming back to your blog for more soon.