Arduino and the HC-SR04 ultrasonic sensor: measure distance (wiring, code and live simulator)
The HC-SR04 measures distance with sound. The Arduino sends a 10 microsecond pulse to its TRIG pin, the sensor sends out a burst of ultrasound, and its ECHO pin stays HIGH until the echo comes back. The Arduino times that pulse with pulseIn(): every 58 microseconds is one centimetre. It needs four wires and no library, and measures from 2 cm to about 4 m. Move the object in the simulator below and watch the reading follow.
Run it in your browser (free simulator)
Press ▶ Simulate in the box above. Nothing to install and no account needed. Open it in the workbench to change it (free account).
Press Simulate
The Uno starts measuring ten times a second and prints each result on the serial console: the object starts 80 cm in front of the sensor, and the console shows cm=79 (the division rounds down).
Move the object
Drag the distance slider on the sensor. The reading on the console follows it, from 2 cm up to 400 cm.
Look at the signal
Open the circuit in the workbench and add an oscilloscope on ECHO: the pulse gets wider as the object moves away, 58 µs for every centimetre.
Make it do something
In the workbench add an LED or a buzzer and switch it in the code when the distance is below 20 cm: a parking sensor in a few lines.
What you need
- An Arduino Uno (or Nano, Mega) and its USB cable
- An HC-SR04 ultrasonic sensor module
- 4 female-to-male jumper wires
- Arduino IDE (no library to install)
Careful: The HC-SR04 runs on 5 V and its ECHO pin outputs 5 V. That is fine for an Uno. On a 3.3 V board (ESP32, Raspberry Pi Pico) put a voltage divider on ECHO (for example 1 kΩ and 2 kΩ), or use the HC-SR04P version that runs on 3.3 V.
Wiring: Arduino Uno and HC-SR04
The sensor has four pins, labelled on its front. TRIG and ECHO can be any digital pins; the code uses D2 and D3.
| HC-SR04 pin | Arduino pin | What it does |
|---|---|---|
| VCC | 5V | Power, 5 V |
| TRIG | D2 | A 10 µs HIGH pulse starts one measurement |
| ECHO | D3 | Stays HIGH for 58 µs per centimetre of distance |
| GND | GND | Ground |
Point the sensor at a flat, hard surface for the first test. Soft things (cloth, foam) and surfaces at a steep angle reflect little sound back, so the reading jumps or drops to 0.
How the code works
loop() first writes TRIG HIGH for 10 microseconds and then LOW again: that is the start signal. The sensor answers by sending eight 40 kHz sound pulses and raising ECHO. When the echo returns, ECHO goes LOW.
pulseIn(ECHO, HIGH, 100000) waits for ECHO to go HIGH and returns how many microseconds it stayed HIGH, or 0 when nothing comes back within 100 ms. Sound travels about 343 metres a second, 0.0343 cm per microsecond, and it goes to the object and back, so the distance is microseconds × 0.0343 / 2, which is microseconds / 58. The sketch prints that and waits 100 ms before the next measurement, so echoes of one measurement do not reach the next.
For several sensors, or to measure without blocking the program, the NewPing library does the same with timers. The wiring stays the same.
Put it on the real Arduino
Wire the four pins
VCC to 5V, GND to GND, TRIG to D2 and ECHO to D3, as in the table.
Upload the sketch
Paste the code below into a new sketch, choose your board and port, and press Upload.
Open the Serial Monitor
Set it to 9600 baud. You see lines like cm=34. Move your hand in front of the sensor and the number follows.
Check with a ruler
Hold a book at 10, 50 and 100 cm. The reading is usually within a centimetre; for better accuracy correct the speed of sound for the temperature of the room.
The code (Arduino C++)
This is the Arduino sketch for the real Uno, with no library. The simulator runs the same code.
Show the code (19 lines)
// HC-SR04: TRIG on D2, ECHO on D3. Times the echo pulse and prints "cm=N" (cm=0 when no echo comes back).
const int TRIG = 2, ECHO = 3;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 100000);
Serial.print("cm=");
Serial.println(us / 58);
delay(100);
}
Troubleshooting
- The reading is always 0
- No echo arrived within the timeout. Check that TRIG and ECHO are not swapped, that VCC has 5 V, and that something is in front of the sensor within 4 m.
- The reading jumps around
- The surface is soft, small or at an angle, or a second sensor is running at the same time. Use a flat target, and take the median of three or five readings.
- The reading is wrong by a few centimetres
- The speed of sound changes with temperature (about 0.6 m/s per °C). The 58 in the code is right near 20 °C. The distance is also measured from the front of the sensor, not from the pins.
- Very close objects read wrong
- Below about 2 cm the sensor is still ringing from its own pulse. Keep objects at least 3 cm away.
Frequently asked questions
What is the range of the HC-SR04?
From about 2 cm to 400 cm, with a beam of about 15 degrees. Hard, flat objects facing the sensor are seen furthest.
Why divide by 58?
Sound travels about 0.0343 cm per microsecond and the pulse makes a round trip, so one centimetre of distance takes about 2 / 0.0343 ≈ 58 microseconds of echo. For inches, divide by 148.
Can I use the HC-SR04 with an ESP32?
Yes, with a voltage divider on ECHO, because ECHO outputs 5 V and ESP32 pins take only 3.3 V. Power the sensor from 5 V (VIN). The code is the same.
Do I need a library?
No. pulseIn() is part of Arduino. NewPing is useful when you have several sensors or want measurements that do not block the rest of the program.
Can I use one pin for TRIG and ECHO?
With NewPing, yes: it switches one pin between output and input. The simple sketch here uses two pins, which is easier to follow.
Can I try this without a sensor?
Yes. The simulator on this page runs the same sketch on a virtual Uno with a virtual HC-SR04: drag the slider to move the object and watch the console. It is free in your browser.
Embed this circuit on your own page
Copy this code into your blog, tutorial or course page. Visitors can run the circuit right there.
<iframe src="https://voltbench.click/embed/gallery/arduino-ultrasonic-sensor-hc-sr04" width="100%" height="560" style="border:1px solid #2a3140;border-radius:8px" loading="lazy" allowfullscreen title="Arduino and the HC-SR04 ultrasonic sensor: measure distance"></iframe>
Build your own version
Open this circuit in the VoltBench workbench, change the wiring or the code, and run it. When it works, flash a real board. Free to use.