pulseIn() makes the robot slow: set a timeout and avoid waiting

Give pulseIn() a timeout. Without one it can wait a full second for a pulse that never comes.

What the message means

The function blocks until the pulse ends. With no echo, the whole sketch stops for the timeout, and the robot drives blind.

Fix it in this order

  1. Pass a timeout in microseconds as the third argument, for example 25000 for about 4 m of range.
  2. Treat a return of 0 as no reading and keep the last good value.
  3. Read sensors in turns and not all in one pass.
  4. Use interrupts or a library that measures in the background where you need speed.
  5. Check the wiring if the timeout keeps happening.

Why it happens

pulseIn waits for the pulse to start and finish, and its default timeout is one second. With no echo, such as an ultrasonic sensor facing open space, the whole sketch stops for that second each time, and a robot can travel some distance in that time. Sound travels about 343 m/s, so a round trip of 25 000 µs corresponds to roughly 4 m, which is why that value suits a typical ultrasonic sensor.

A return value of 0 means the timeout expired. Keep the last good reading instead of treating 0 as a distance of zero.

If you see this, do this

You seeDo this
Robot pauses when nothing is in frontNo timeout set.
Always times outWiring or supply.

HC-SR04 reads 0, delay and millis.

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

pulseIn measures how long a pin stays HIGH or LOW. It first waits for the pulse to start, then waits for it to end, and it does nothing else while it waits. The default timeout is one second, so a sensor that never answers stalls the whole sketch for a full second per reading. Motors keep their last speed during that time, so the robot keeps driving without looking.

With an ultrasonic sensor the pulse width equals the sound’s round trip. Sound travels at roughly 343 m/s, which is about 29 microseconds per centimetre one way and about 58 microseconds per centimetre for the round trip. So a timeout of 25000 microseconds allows 25000 / 58, about 430 cm, which is beyond the sensor’s useful range. A timeout of 5000 microseconds would stop at roughly 86 cm.

How to confirm the cause

  • Time one call. Store start = micros() before pulseIn and print micros() – start afterwards. A value near 1,000,000 means the call timed out at its default.
  • Point the sensor at open space and watch the loop rate. If the robot stutters only when nothing is in front of it, pulseIn is waiting for an echo that never arrives.
  • Print the returned value. A 0 is a timeout, not a real distance. A steady value around 150 to 2000 microseconds is a normal echo for objects from a few centimetres to about 35 cm.
  • Disconnect the echo wire briefly. If the timing numbers match the timeout you chose, the code is behaving as designed and the wiring is the issue.
  • Count how many pulseIn calls run per loop. Several sensors each waiting for a timeout add up.

Worked example

A reading function with a timeout, and a rule that keeps the last good value when nothing comes back (TRIG and ECHO are your own pin constants):

long readEchoUs() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH, 25000UL);      // 0 means timeout
}

float lastCm = 400.0;
void loop() {
  long us = readEchoUs();
  if (us > 0) lastCm = us / 58.0;           // microseconds to centimetres
  // use lastCm for the drive decision
}

Common mistakes

  • Giving the timeout in milliseconds. The argument is in microseconds.
  • Choosing a timeout that is too short, for example 1000 microseconds, which covers only about 17 cm.
  • Treating a 0 return as a distance of 0 cm, so the robot stops or reverses for no reason.
  • Triggering two ultrasonic sensors at the same moment, so each hears the other’s echo.
  • Adding delay() calls between readings that make the loop slower again.

If this did not fix it

  • Wiring and supply: a sensor on a weak or noisy 5 V line gives missing echoes. Check the supply under motor load.
  • Other blocking code in the loop, such as long delay() calls or slow serial printing, can look the same as a pulseIn problem.
  • A different cause for no echo: soft or angled objects absorb or deflect the sound, so the echo never returns to the sensor.