HC-SR04 reads 0 or jumps around: trigger timing, echo and surface

A reading of 0 usually means no echo came back before the timeout. Check the wiring, supply and the surface in front of the sensor.

What the message means

The sensor sends a burst of sound and times the echo. Soft cloth, angled surfaces and very close objects return a weak or no echo.

Fix it in this order

  1. Send a trigger pulse of at least 10 microseconds, then read the echo pin.
  2. Check that trigger and echo go to the pins named in the code.
  3. Give the sensor a steady 5 V supply and a shared ground.
  4. Test against a flat hard wall at about 30 cm.
  5. Ignore readings under 2 cm and above about 400 cm.
  6. Take several readings and use the median to remove spikes.
  7. See the page on echo voltage when you use a 3.3 V board.

Why it happens

The sensor sends a short burst of ultrasound and the echo pin stays high for as long as the sound takes to go out and come back. Your code measures that time and turns it into a distance. If no echo returns before the timeout, the measured time is zero and the distance reads zero.

Soft surfaces absorb sound and angled surfaces send it away from the sensor, so little comes back. Very close objects can also give unreliable readings. A weak supply and wrong trigger and echo wiring give the same symptoms, so check them before you blame the surface.

If you see this, do this

You seeDo this
Always 0Wiring or supply.
Random jumpsSurface, noise or one bad reading.

echo voltage on ESP32, common ground.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

The HC-SR04 sends a burst of ultrasound and holds its Echo pin high until the sound returns. The code measures that time and converts it. Sound travels at about 343 m/s in air at 20 degrees C, which is 0.0343 cm per microsecond. The sound makes a round trip, so distance = time x 0.0343 / 2. An echo of 5831 microseconds therefore gives 5831 x 0.0343 / 2 = 100 cm.

A reading of 0 comes from the code’s timeout. pulseIn() returns 0 if the pulse does not start or end in time, so zero does not mean zero distance. Speed of sound changes with temperature, about 331.3 + 0.606 x T metres per second, so it is 343.4 m/s at 20 degrees and about 349.5 m/s at 30, a difference of nearly 2 percent. Echoes also fail from soft cloth, angled panels, thin poles and narrow objects.

How to confirm the cause

  • Point the sensor at a flat wall about 30 cm away. The echo should be near 60 / 0.0343 = 1749 microseconds. If you get that, the sensor works and your surface or range was the problem.
  • Print the raw pulseIn() value as well as the distance. A zero shows a timeout; a stable number shows a working echo.
  • Check that the trigger pulse is at least 10 microseconds and that readings are at least 60 ms apart.
  • Measure the 5 V supply at the sensor while it runs. A drooping supply shortens or removes the echo.
  • Move the sensor away from nearby walls and mounting plates, which can reflect the burst straight back.

Worked example

Taking the median of three readings to remove spikes. The pin numbers are examples; the timeout of 30 ms covers a little over 5 m.

long readUs() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH, 30000);
}
long a = readUs(); delay(60);
long b = readUs(); delay(60);
long c = readUs();
long m = max(min(a, b), min(max(a, b), c));   // median of three
float cm = m * 0.0343 / 2;

Common mistakes

  • Treating 0 as a real distance.
  • Triggering again before the previous echo has died away.
  • Running two sensors at the same time, so one hears the other.
  • Mounting the sensor too close to a case or bracket.
  • Ignoring temperature for measurements that need to be accurate to a centimetre.

If this did not fix it

  • Check the Trig and Echo wires for swapping.
  • Check the supply voltage. A 3.3 V supply often gives short range or no echo.
  • Check the sensor itself by trying a second unit, since cheap modules vary.