HC-SR04 Ultrasonic Sensor: Measuring Distances with Arduino
The HC-SR04 sensor is one of the most popular components in the Arduino ecosystem. Using ultrasonic waves, it allows for distance measurement with surprising precision for its price.
⚡ Quick Answer
The HC-SR04 sensor measures distances using ultrasonic waves (the echo principle). It emits an ultrasonic burst and calculates the return time of the bounce off an obstacle. By using the speed of sound (340m/s), a distance measurement accurate to within a few millimeters is obtained.
Fluke 323 Digital Clamp Meter (True RMS, 400A AC)
🛠️ The essential tool for this project.
The Setup

Wiring Table
| Component Pin | Arduino Pin | Color |
|---|---|---|
| VCC | 5V | Red |
| GND | GND | Black |
| TRIG | Pin 9 | Blue |
| ECHO | Pin 10 | Green |
Arduino Code
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Live Demonstration

Deep Dive into Ultrasonic Technology
When I started in robotics, the HC-SR04 was a true revelation. But how does this little device that looks like “robot eyes” actually work? The principle is inspired by nature, much like bats or dolphins: echolocation.
One of the cylinders (the “Trig” or transmitter) emits a burst of ultrasonic waves at 40 kHz, a frequency inaudible to the human ear. These waves travel through the air at the speed of sound (approximately 340 meters per second). When they hit an obstacle, they bounce back and return to the sensor. The other cylinder (the “Echo” or receiver) picks up this return. By measuring the time elapsed between emission and reception, the Arduino can calculate the object’s distance with millimeter precision. It’s pure physics applied to hobby electronics!
Maker Tips and Troubleshooting
On the workbench, things don’t always go according to theory. Here are some insights from years of prototyping:
- The “Ghost” Effect: If your object is too soft (like a plush toy or fabric), it will absorb the waves instead of reflecting them. The HC-SR04 will then display an incorrect distance. Prefer hard, flat surfaces for your tests.
- Acoustic Interference: In very noisy or confined environments, echoes can overlap. Don’t hesitate to add a slight extra
delay()in your code between two measurements to allow time for the waves to dissipate. - Unstable Power Supply: If your values jump mysteriously, check your 5V. A filtering capacitor on the Arduino power supply often resolves this issue.
Project Ideas to Go Further
Now that you’ve mastered this component, the possibilities are endless! Why not build a reverse parking sensor for your RC car? Or perhaps a smart trash can that opens automatically when you bring your hand near it? My favorite project remains the “Sumo Robot” which detects its opponents to push them out of the arena. Your turn now, let your creativity speak!
To deepen your understanding of power management, don’t forget to check out our article on Ohm’s Law.
