HC-SR04 ultrasonic sensor: measure distance — This sensor sends a short ultrasonic burst and measures how long the echo takes to return. The arithmetic is simple once the wiring is right.

Sensors

HC-SR04 ultrasonic sensor: measure distance

HC-SR04 ultrasonic sensor: measure distance — This sensor sends a short ultrasonic burst and measures how long the echo takes to return. The arithmetic is simple once the wiring is right.

This sensor sends a short ultrasonic burst and measures how long the echo takes to return. The arithmetic is simple once the wiring is right.

22 November 20267 min read
HC-SR04 ultrasonic sensor with trigger and echo wires connected to an Arduino on a breadboard
The four sensor pins: power, trigger, echo and ground. The echo line returns a pulse whose width encodes distance.

Bu projeyi gerçekten kurmak istersen parçaları hobidevre.com'dan temin edebilirsin →

Build it for real

To build this project for real, get the parts from hobidevre.com → hobidevre.com →

More build guides: hobidevre.com/blog

The four pins

The module exposes VCC, TRIG, ECHO and GND. Connect VCC to 5 V and GND to the common ground. Drive TRIG from a digital output; read ECHO on a digital input. Both signal pins use 5 V logic on the commonly sold module, which matters if your board runs at 3.3 V.

For a 3.3 V board, put a resistor divider on the echo line — two equal 10 kΩ resistors halve 5 V to 2.5 V, safely inside the input limit. Connect the divider ground to the same ground as the sensor.

Trigger and echo timing

Hold TRIG low, then send a 10 microsecond HIGH pulse. The module emits eight ultrasonic bursts at about 40 kHz and then raises ECHO. The width of that ECHO pulse is the round-trip time of the sound.

Because the sound travels to the object and back, divide by two. At 20 °C sound travels about 0.0343 cm per microsecond, so:

distance in cm = echo time in µs × 0.0343 / 2

A 294 µs echo gives 294 × 0.0343 / 2 ≈ 5.0 cm. A 1,000 µs echo gives about 17.2 cm. The sensor is normally specified from roughly 2 cm to 400 cm.

A minimal reading

const int trig = 9;
const int echo = 10;

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  long sure = pulseIn(echo, HIGH, 30000);
  if (sure == 0) {
    Serial.println("no echo");
  } else {
    float cm = sure * 0.0343 / 2.0;
    Serial.println(cm);
  }
  delay(100);
}

The third argument to pulseIn is a timeout in microseconds. It prevents the sketch from freezing when nothing is in range — always include it.

What affects accuracy

  • Surface: soft fabric absorbs ultrasound; a slanted flat surface can reflect the beam away from the sensor.
  • Temperature: the speed of sound changes with air temperature, so a fixed 0.0343 factor drifts in very hot or cold rooms.
  • Dead zone: objects closer than about 2 cm may be reported unreliably.
  • Repeat rate: allow at least 50-60 ms between measurements so the previous echo has finished.

Use the sensor for rough proximity, not for precision measurement. Average several readings if the value jitters.

Wiring checks

  • Confirm the echo pin is an input and the trigger pin an output.
  • Share one ground between the board and the sensor.
  • On a 3.3 V board, do not connect the 5 V echo line directly — use the divider.
  • If readings are always zero, check that the object faces the sensor and is inside the usable range.

Get the parts

If you want to build this project in real life, you can get the parts from hobidevre.com.

Frequently asked questions

Can I use 3.3 V for VCC?

Most HC-SR04 modules are specified for 5 V. Power it from 5 V and level-shift the echo line when your board uses 3.3 V logic.

Why do I get zero forever?

Check the trigger pulse, the shared ground and whether the target reflects sound back toward the sensor. A timeout of zero means no echo arrived.

How accurate is it?

Typically within a few millimetres under good conditions, with larger errors from soft or angled surfaces and temperature changes.

Related reading

Practise in the browser

Open the same layout in the simulator and follow the steps. Sign in to save your work.