Skip to content
RoboLearn
Not completed

Ultrasonic distance sensors

What you’ll learn

  • How ultrasonic sensors measure distance
  • How to wire and code the HC-SR04
  • Its limits, and how to work around them

Echoes

The HC-SR04 sends a burst of ultrasound (40 kHz, too high for people to hear) and listens for the echo, like a bat. Sound travels at about 343 m/s (0.0343 cm per microsecond), so the time for the echo to come back gives the distance:

distance (cm) = echo time (µs) × 0.0343 ÷ 2

(÷ 2 because the sound goes there and back.) A handy shortcut: cm ≈ µs ÷ 58.

Wiring

HC-SR04 pin Arduino
VCC 5V
GND GND
TRIG any digital pin (e.g. 3)
ECHO any digital pin (e.g. 2)

To measure: set TRIG HIGH for 10 µs, then time how long ECHO stays HIGH with pulseIn().

Limits

  • Range roughly 2 cm to 4 m.
  • Soft things (curtains, clothes) absorb sound; angled surfaces bounce it away, so readings can jump.
  • Two sensors firing at once can hear each other’s echoes: trigger them one after the other.
  • Readings are noisy: take a few and use the median.

Finished this lesson?