This part has a page of its own, with a note on using it and a circuit to try: the part page.
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 echo time per millimeter
of range (the sound goes there and back): 6.04 microseconds at freezing, 5.82 at 20 °C
(the datasheet's round 58 per centimeter) and 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.
The speed of sound does follow temperature, so a sketch that divides by 58 reads a
1 m wall as 104 cm at freezing and 97 cm at 40 °C, as a real module does. Humidity
is not modeled, and the temperature is a value you set, not something that drifts.
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. A trigger that comes less than 60 ms
after the last one is ignored, so ECHO never rises and pulseIn() times out and
returns 0, the same bug real HC-SR04 code hits. Put delay(60) between readings.
See it in action
Parking sensor reads range straight into a buzzer's pitch. Open it at /templates.
The Smart blind stick beeps and buzzes faster the closer an obstacle is, from quiet at a meter to continuous under 20 cm.
The Ultrasonic water tank level monitor reads the water from the lid of a tank and shows the level as a percent on an I2C LCD.
Related
- The HC-SR04 part page, with its pinout