Component · Sensor

HC-SR04 Ultrasonic Sensor

The HC-SR04 is the ultrasonic sensor most robots use to measure distance. How echo-ranging works, its real range and blind spots, and how to wire it.

What it is

The HC-SR04 is an ultrasonic distance sensor—the small board with two silver cylinders that look like robot eyes. It measures distance the way a bat or a submarine does: it emits a burst of sound too high to hear, listens for the echo, and times how long the round trip takes. For a few dollars it gives a robot a sense of the space in front of it, which is why it is the default sensor for the first obstacle-avoiding robot almost everyone builds.

The two cylinders are a transmitter and a receiver. One chirps, the other listens. Everything else on the board is timing circuitry that turns that echo into a signal your microcontroller can measure.

Diagram of an HC-SR04 wired to a microcontroller: colour-coded VCC, Trigger, Echo, and GND wires run to the sensor's four pins; the sensor emits ultrasonic waves that reflect off an object, and distance equals the echo time times the speed of sound, divided by two.
Trigger a pulse, time how long the echo takes to return, and convert it to distance—the four wires are power, ground, trigger, and echo. Download SVG

How it works

The sensor speaks a simple two-wire language:

  1. You pulse the Trigger pin HIGH for 10 microseconds.
  2. The sensor fires eight ultrasonic pulses at 40 kHz and raises the Echo pin.
  3. When the echo returns, it drops Echo LOW again.
  4. You measure how long Echo stayed HIGH.

That pulse width is the round-trip time. Sound travels about 343 m/s in air, so distance = (echo_time × 343) / 2—the division by two because the sound went out and came back. Most Arduino code wraps this in pulseIn() and a bit of arithmetic.

Because it depends on the speed of sound, the reading drifts slightly with temperature (sound is faster in warm air). For a robot avoiding walls that error is negligible; for precise ranging it matters, and you would compensate for temperature.

When to use it

Reach for an HC-SR04 whenever a robot needs to know roughly how far away the nearest thing is—obstacle avoidance, wall-following, a “don’t drive off the table” guard, or triggering a behaviour when something approaches. Its wide beam is actually helpful here: it is hard to miss an obstacle.

Look elsewhere when you need a precise distance, a narrow beam, or fast updates. A time-of-flight sensor uses light instead of sound and gives a tight, fast, millimetre-accurate reading—better for mapping or a robot that must judge a specific gap.

Wiring and gotchas

  • Echo is a 5 V pin. On a 5 V board like the Arduino Uno that’s fine. On a 3.3 V board like the ESP32, put a voltage divider on Echo or you risk damaging the pin.
  • Mind the blind spot and the cone. Nothing closer than ~2 cm registers, and the ~15° beam means the sensor reports the nearest thing in a cone—a table leg to the side can trigger it.
  • Soft and angled targets lie. Foam, cloth, or a wall at a steep angle scatter the echo away from the receiver, so the sensor reads “far” or nothing. It works best on flat, hard surfaces facing it.
  • Don’t poll too fast. Leave ~60 ms between readings so the previous echo has died away; ping sooner and stray echoes give false readings. Filtering the stream helps—see taming sensor noise.

Explore the graph

Used in these builds

Projects, learning paths, and simulators that include the HC-SR04 Ultrasonic Sensor.

Compare

Alternatives

Questions

HC-SR04 Ultrasonic Sensor FAQ

What is the HC-SR04 ultrasonic sensor?

The HC-SR04 is a low-cost distance sensor that measures how far away an object is by bouncing a burst of ultrasound off it—the same principle bats and sonar use. It is the sensor most beginner robots use to detect and avoid obstacles.

How does the HC-SR04 work?

You send a short pulse to its Trigger pin. The sensor emits eight 40 kHz ultrasonic bursts and raises its Echo pin until the reflection returns. You measure how long Echo stays high, and since sound travels about 343 m/s, distance equals that time times the speed of sound, divided by two for the round trip.

What is the maximum range of the HC-SR04?

The HC-SR04 measures roughly 2 cm to 4 m. It is most accurate in the 5 cm to 2 m band; near the 4 m limit readings get unreliable, and objects closer than about 2 cm fall inside its blind spot and cannot be measured.

Is the HC-SR04 accurate and reliable?

It is accurate enough for obstacle avoidance—around 3 mm resolution on a flat, hard target facing it squarely. It struggles with soft or angled surfaces (which scatter the echo), is affected by temperature, and has a wide ~15° beam, so it senses a cone, not a point.

How do you connect an HC-SR04 to an Arduino?

Wire VCC to 5 V, GND to ground, Trigger to any digital output pin, and Echo to a digital input pin. One caution: Echo outputs 5 V, so if you use a 3.3 V board like an ESP32, drop it with a voltage divider before reading it.

HC-SR04 vs US-100—which is better?

The US-100 is a close cousin with a temperature-compensated mode, a serial interface option, and 3.3–5 V operation, which makes it more accurate and easier on 3.3 V boards. The HC-SR04 is cheaper and has more tutorials. For learning, the HC-SR04 is fine; for precision, the US-100 edges ahead.

How do I check if my ultrasonic sensor is working?

Print the measured distance to the serial monitor and move your hand toward and away from the sensor—the reading should track smoothly. If it is stuck at zero or a maximum value, check the Trigger and Echo wiring, confirm it has a solid 5 V supply, and make sure your timeout is long enough for the full 4 m range.

Further reading

References