Skip to Content
Circuitera

HC-SR04 ultrasonic sensor

Wire an HC-SR04 to a 5 V Arduino and learn how to measure an echo pulse without exposing a 3.3 V input to its 5 V output.

HC-SR04 ultrasonic sensor photograph
HC SR04 Ultrasonic Module.jpg by Suyash Dwivedi · CC BY-SA 4.0. Resized and encoded as WebP without cropping.

How it works

A short pulse on Trig starts a burst. The sensor reports the time of flight using the high duration of Echo. The signal travels to the object and back, so divide the distance traveled by two. An angled or soft target can return too little sound for a useful reading.

Source: SparkFun

Pinout and wiring

Read the PCB markings: VCC, Trig, Echo and GND. Connect VCC to 5 V and join grounds. Trig goes from the controller to the sensor. Echo goes the other way; use a suitable level shifter or divider before a 3.3 V GPIO.

Source: SparkFun

Timing and timeouts

Give Trig a pulse at least 10 microseconds wide. Leave enough time between triggers for old echoes to decay; the supplier sheet suggests a cycle longer than 60 ms. When reading Echo in code, set a timeout and treat a timeout as missing data instead of zero distance.

Source: SparkFun

Specifications

ParameterValue or conditionSource
Module supply5 V for the standard HC-SR04SparkFun
Quoted measuring range2 cm to 4 m with a suitable targetSparkFun
Ultrasonic burst frequency40 kHzSparkFun

Compatibility and practice

Direct logic connection on a standard 5 V Uno; level-shift Echo for a 3.3 V input.

Sources and datasheets