The HC-SR04 ultrasonic sensor is a popular, affordable component used to measure distance in Arduino projects. It uses ultrasonic sound waves to detect objects without physical contact. This sensor is useful for robotics, obstacle avoidance, parking assistance prototypes, water-level monitoring, and many other DIY electronics projects.
In this guide, you will learn how the HC-SR04 works, how to connect it to an Arduino Uno, and how to write a simple program to measure distance in centimetres.
![]() |
| How to Use HC-SR04. |
What Is the HC-SR04 Ultrasonic Sensor?
The HC-SR04 is an electronic distance-measuring module with two round ultrasonic transducers.
Transmitter: Sends out an ultrasonic sound pulse.
Receiver: Detects the returning echo.
Control circuitry: Helps generate the pulse and produce an echo signal for the Arduino to measure.
The sensor typically operates at around 40 kHz, which is above the range of human hearing.
Many HC-SR04 modules specify a nominal measuring range of approximately 2 cm to 400 cm. Actual performance depends on the target, environment, sensor alignment, and module quality.
How Does the HC-SR04 Work?
The sensor measures the time taken for an ultrasonic pulse to travel to an object and return.
The basic process is:
The Arduino sends a short trigger pulse to the sensor’s TRIG pin.
The sensor transmits an ultrasonic burst.
The sound waves reflect from an object.
The sensor’s ECHO pin produces a pulse whose duration represents the round-trip travel time.
The Arduino calculates the distance using the speed of sound.
Because the sound travels to the object and back, the measured time must be divided by two when calculating one-way distance.
The approximate formula at room temperature is:
Distance (cm) = Echo duration (µs) × 0.0343 ÷ 2
For example, if the measured echo duration is 1,000 microseconds:
Distance = 1,000 × 0.0343 ÷ 2
Distance ≈ 17.15 cm
This is an approximate result because the speed of sound changes with air temperature.
HC-SR04 Pin Configuration
The standard HC-SR04 module has four pins:
| Pin | Function |
|---|---|
| VCC | Power supply, typically 5V |
| TRIG | Trigger input |
| ECHO | Echo output |
| GND | Ground |
Check the labels on your actual module before connecting it. Pin layouts and markings can vary between products.
How to Connect the HC-SR04 to Arduino Uno
For this example, use an Arduino Uno and the following connections:
| HC-SR04 Pin | Arduino Uno |
|---|---|
| VCC | 5V |
| TRIG | Digital pin 9 |
| ECHO | Digital pin 10 |
| GND | GND |
Make sure the wiring is secure before powering the circuit.
Important: The standard HC-SR04 ECHO output is typically a 5V logic signal. If you use a 3.3V-only microcontroller, such as many ESP32 boards, use an appropriate voltage divider or logic-level interface on ECHO unless the specific board or module supports 5V input.
Arduino Code for Measuring Distance
Connect the sensor according to the wiring table, then upload this example to your Arduino Uno.
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Ensure the trigger starts LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10-microsecond trigger pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo pulse, with a timeout
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) {
Serial.println("No echo detected");
} else {
float distance = duration * 0.0343 / 2.0;
Serial.print("Distance: ");
Serial.print(distance, 1);
Serial.println(" cm");
}
delay(500);
}This program sends a trigger pulse, measures the echo duration, calculates the distance, and prints the result to the Serial Monitor.
The pulseIn() timeout prevents the program from waiting indefinitely when no echo is received.
How to View the Distance Reading
After uploading the code:
Connect the Arduino to your computer using a USB cable.
Open the Arduino IDE.
Select the correct board and COM port.
Open the Serial Monitor.
Set the baud rate to 9600.
Place an object in front of the sensor and observe the readings.
Example output:
Distance: 23.5 cm
Distance: 24.1 cm
Distance: 22.8 cm
Distance: 23.0 cmThese are illustrative readings. Your actual results will depend on the distance, target surface, and surrounding conditions.
Common Applications of the HC-SR04
1. Obstacle Avoidance Robot
A robot can use the sensor to detect objects in front of it and trigger a stop, turn, or avoidance routine.
2. Water-Level Monitoring
The sensor can measure the distance from a fixed mounting point to a water surface. A suitable enclosure and installation are important to protect the electronics from moisture.
3. Parking Assistance Prototype
A project can estimate the distance between a vehicle model and an obstacle, then use LEDs or a buzzer to indicate proximity.
4. Robotics and Automation
The HC-SR04 is useful for learning distance measurement, object detection, and basic automation control.
5. General Distance Measurement
It can measure the distance to suitable objects in science experiments, DIY projects, and educational demonstrations.
Tips for Better Measurement Accuracy
For more reliable readings:
Keep the sensor firmly mounted.
Aim it directly toward the target.
Use a large, reasonably flat target when testing.
Avoid soft materials that absorb sound.
Be aware that angled surfaces may reflect sound away from the receiver.
Allow enough time between measurements to avoid confusing echoes.
Consider temperature changes when higher accuracy is required.
The HC-SR04 is not a precision industrial measuring instrument. For demanding environments or high-reliability applications, choose a sensor designed for those requirements.
Troubleshooting Common Problems
Problem: The Serial Monitor always shows zero or no echo.
Check the TRIG and ECHO wiring, power supply, ground connection, and target position. Make sure the correct COM port and baud rate are selected.
Problem: The readings fluctuate.
Try using a larger, flat target, mounting the sensor securely, and taking several readings to calculate an average.
Problem: The sensor does not detect soft or angled objects reliably.
Some materials absorb ultrasonic energy, while angled surfaces may reflect sound away from the receiver. Try a different target orientation.
Problem: The sensor does not work with a 3.3V microcontroller.
Check the module’s voltage requirements and ensure that the ECHO signal is safe for the controller’s input pins.
Conclusion
The HC-SR04 ultrasonic sensor is an accessible way to learn distance measurement with Arduino. With four simple connections and a short program, you can build projects involving obstacle detection, robotics, water-level monitoring, and basic automation.
Once you understand how the trigger pulse, echo duration, and distance formula work together, you can adapt the same principles to more advanced electronics projects.
Explore more electronics parts and project ideas with MY CREATIVE ENGINEERING — Your Electronics Parts & Projects Partner.
Follow us for more practical electronics tutorials, component guides, and automation projects.

No comments:
Post a Comment