Source: https://mokxi.com/learn/arduino-ultrasonic-sensor
Updated: 2026-09-27

Learn

# Measure distance with an HC-SR04 ultrasonic sensor

Written by the Mokxi team, updated September 27, 2026

Try it now, no account Sign up free or see every lesson

- 192 parts on the bench
- 25 boards running now
- 1.00x real time, on every board

Click to open it in the editor

The parking example: an HC-SR04 on pins 7 and 6, a buzzer on 9 and a lamp on 8. Open it in the editor and drag the sensor’s slider to move the wall.

The HC-SR04 is the sensor every class starts with, because there is nothing hidden in it: no bus, no address, no library you have to trust. You send it a pulse, it sends back a pulse, and the length of the second one is the distance. The circuit above wires one to an Arduino Uno and turns the reading into a parking sensor, with a buzzer that beeps faster as the wall gets closer and a red lamp inside the stop line.

Open it in the editor and drag the slider on the sensor to move the wall. The serial monitor prints the distance in centimeters and the gap between beeps.

## What you need

- An Arduino Uno
- An HC-SR04 ultrasonic sensor
- A buzzer
- A red LED and a 220 ohm resistor
- A breadboard and jumper wires

## Wiring

Part and pin

Goes to

Note

HC-SR04 VCC

5 V

The module wants 4.5 V or more

HC-SR04 GND

GND

HC-SR04 TRIG

Pin 7

The board fires a ping on this pin

HC-SR04 ECHO

Pin 6

High for as long as the sound takes to come back

Buzzer

Pin 9

The other leg to GND

Red LED, through 220 ohm

Pin 8

Lights inside the stop line

## How the sensor measures

Hold TRIG high for at least ten microseconds and the module sends a short burst of 40 kHz sound, then drives ECHO high for exactly as long as the sound takes to reach the object and come back. Sound travels about 343 meters a second at room temperature, which is 0.0343 centimeters a microsecond, and it makes the trip twice. So the distance in centimeters is the echo time in microseconds divided by about 58, the number every HC-SR04 sketch divides by.

Mokxi’s part keeps the datasheet’s timing: ECHO rises 460 microseconds after the trigger, stays high 58 microseconds per centimeter, and with nothing in range it stays high for the full 38 millisecond timeout. The speed of sound comes from an air temperature you can set on the part, so a sketch that divides by 58 reads a one meter wall as 104 centimeters at freezing, which is what a real module does too.

## The code, explained

The example reads the sensor through a small helper that does the trigger, times the echo and returns millimeters, or zero when nothing came back. Written out by hand with pulseIn(), which is how most tutorials do it, the measurement is five lines.

The parking behavior is one function. Inside 15 centimeters it holds one long note and lights the LED. Between there and two meters, the gap between beeps grows in a straight line from 90 milliseconds to 900. Past two meters it stays quiet. Each loop ends with a 60 millisecond wait, and that number matters: the datasheet asks for 60 milliseconds between pings, because the echo of the last one is still bouncing round the room.

The measurement, the way most tutorials write it

## Try it in the editor

Open the circuit in the editor and select the sensor. Its temperature property is the air temperature, 20 degrees by default. Set it to 0 and the same wall reads a few centimeters further away, because sound is slower in cold air; set it to 40 and it reads closer. The sketch has not changed, and neither would a real one.

Next, change the delay(60) at the end of every branch of loop() to delay(20). Many of the pings now go unanswered, because the module is still waiting for the last echo to die away and ignores a trigger that comes too soon, so the serial monitor starts mixing nothing in range in with real distances. Put it back to 60 and every reading is a real one again.

For a different project on the same wiring, remove the buzzer code and print the distance to the serial plotter instead, and you have a simple rangefinder.

## Common mistakes

Pinging faster than every 60 milliseconds. The first reading comes back and the rest are ignored, because the module refuses to trigger while it is still listening. The simulated part does the same, so the bug shows up here before it shows up on your desk.

Swapping TRIG and ECHO. Nothing breaks, but the board waits for a pulse that never comes, and every reading is zero or the full timeout.

Forgetting the timeout. With nothing in range the echo lasts 38 milliseconds, and pulseIn() without a timeout waits a full second before giving up, which makes the rest of the sketch feel stuck.

Trusting the last millimeter. A real HC-SR04 is good to a few millimeters at best and gets confused by soft or angled surfaces. The simulated reading is exact, which is useful for learning the code but kinder than the bench.

Wiring ECHO straight to a 3.3 volt board. The module drives ECHO at 5 volts. On an Uno that is fine; on an ESP32 or a Pico, a real circuit needs a resistor divider on that line.

## Questions

What is the range of the HC-SR04?

About 2 centimeters to 4 meters. The simulated part reports anything closer than 20 millimeters or further than 4000 as nothing in range.

Why divide by 58?

Sound takes about 29 microseconds to travel a centimeter at room temperature, and the echo covers the distance twice, out and back. 29 times 2 is 58.

Can I use the HC-SR04 with an ESP32?

Yes, with the same code. On real hardware the ESP32 pins are not 5 volt tolerant, so put a divider on ECHO, for example 1 k and 2 k, to bring it down to about 3.3 volts.

Does the simulator model the sound itself?

No. There is no beam angle and no reflection off a scene. The part is a stopwatch that holds ECHO high for the round trip at the speed of sound, with the distance set on a slider. That is enough to write and debug the code exactly as it runs on hardware.

Related

## Keep going

Arduino Blink Without Delay: Two LEDs, Two Rates Arduino Servo Motor with a Potentiometer PIR Motion Sensor: How It Works, With an Arduino The HC-SR04, pin by pin How the HC-SR04 is modeled The project page, with the full sketch

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building Open the editor
