Part

Ultrasonic ranger

Pulse TRIG and time ECHO. The slider moves whatever it is looking at.

or see every part
  • 4 pins
  • 1 property
Drawn live by the editor's own code, at the size you see it.
Reference

Every one of its 4 pins

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

What it does

The HC-SR04 measures distance with sound and reports it as time. Hold TRIG high for ten microseconds and the module fires a short burst of 40 kHz, then holds ECHO high for as long as the sound takes to reach the object and come back: about 58 microseconds for every centimeter of range. There is no bus, no register and no address, which is why it is the sensor every class starts with. Mokxi’s part keeps the datasheet’s timing, including the 460 microsecond wait before ECHO rises, the full 38 millisecond pulse when nothing is in range, and the rule that a new ping inside 60 milliseconds of the last is ignored. The speed of sound follows an air temperature you can set, so a sketch that divides by 58 drifts with the weather the way a real module does. The distance itself is a slider on the part, from 2 centimeters to 4 meters.

How to use it

How to use the HC-SR04 with an Arduino

Wire VCC to 5 V, GND to GND, and TRIG and ECHO to two digital pins. To measure, hold TRIG high for 10 microseconds, time how long ECHO stays high with pulseIn(), and divide by 58 for centimeters. Give pulseIn() a timeout of about 38 milliseconds so an empty room does not stall the sketch, and leave at least 60 milliseconds between pings.

Part pin
Board pin
VCC
5 V
TRIG
Pin 7
ECHO
Pin 6
GND
GND

The same code runs on an ESP32 or a Pico. On real hardware those boards are not 5 V tolerant, so put a divider on ECHO.

Arduino Uno: parking sensorlive0.000 s 0.00x
Click to open it in the editor
This is the simulator itself, running here. Click anything to open it in the editor.
One reading, in centimeters
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long cm = pulseIn(ECHO, HIGH, 38000) / 58;
Reference

The one property you can set

Property
Default
What it means
temperature
20
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.
How it is modeled

What is true about the Ultrasonic ranger, here

What is modeled

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.

Not modeled

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.

From HC-SR04 ultrasonic sensor, in full.

Projects

See the Ultrasonic ranger in a project

Shown here on: Arduino Uno R3

Learn

Where it turns up in a lesson

In a learn article

Wire up the Ultrasonic ranger

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.