This part has a page of its own, with a note on using it and a circuit to try: the part page.

HC-SR04 ultrasonic sensor

The four-pin ultrasonic ranger, HC-SR04 style: two transducer cans, a 5 V supply, and two logic pins. Aliases: ultrasonic sensor, ping sensor.

The whole protocol is a stopwatch: hold TRIG high for 10 microseconds or more, the module sends eight cycles of 40 kHz out of the transmitter, and then drives ECHO high for exactly as long as the sound takes to come back. There is no bus, no register and no address, which is why this is the sensor every class starts with.

Pins

Pin What it does
VCC Supply, 4.5 V or more.
TRIG Input; hold high 10 us or more to fire a ping.
ECHO Push-pull output, high for the round-trip time.
GND Ground.

Properties

temperature: the air temperature in degrees Celsius, 20 by default. Sound travels at 331.3 + 0.606 T meters a second, so this is what sets the microseconds per centimeter: 6.04 per centimeter of round trip at freezing, the datasheet's round 5.8 at room temperature, 5.63 at 40 °C.

The target distance is not a property. It is set on the slider.

While it runs

Drag the slider on the part to move the thing in front of it, in millimeters.

What the model gets right

The datasheet's own timing, including the awkward number every sketch divides by: ECHO rises 460 microseconds after the trigger ends and stays high for 58 microseconds per centimeter of range: the round trip at the speed of sound. With nothing in range it stays high for the full 38 ms timeout, and a sketch that does not expect that hangs for 38 ms on a reading. The 60 ms re-trigger cycle is real too: trigger it faster and the module ignores the request, because the last burst is still rattling around the room.

What it does not model

No beam angle or off-axis reflection. The slider is a single number, "how far away the thing in front of it is", not a scene to bounce sound around.

No sound is simulated at all. The 40 kHz burst is never emitted; the part is a stopwatch that waits 460 microseconds and then holds ECHO high for the round trip at the speed of sound. That speed does come from the air temperature: the temperature property is degrees Celsius, 20 by default, and sound moves at 331.3 + 0.606 T meters a second, so a 1 m wall echoes for 6.04 ms at freezing and 5.63 ms at 40 °C. A sketch that divides by 58, as every sketch does, reads that wall as 104 cm in the cold and 97 cm in the heat, which is exactly what a real module does and why one never quite agrees with a tape measure. What is still missing is humidity and any drift: the temperature is a dial you set, not something that wanders.

The reading has no error in it. Whatever the slider says comes back exactly, with no ±3 mm accuracy figure, no jitter between readings, no dead zone and no drop-outs from a soft or angled surface. Anything closer than 20 mm or further than 4000 mm reads as nothing in range rather than as a wrong number, which is what a real HC-SR04 does at the near end, and kinder than one at the far end.

ECHO is a push-pull 5 V output with no rise time, and there is no supply current.

Common mistakes

Polling in a tight loop faster than 60 ms. The first reading comes back and every one after it is silently ignored, which is exactly the bug real HC-SR04 code hits too.

See it in action

Parking sensor reads range straight into a buzzer's pitch. Open it at /templates.