December 5, 2026

HC-SR04 Ultrasonic Sensor: How It Measures Distance, and Why It Reads Long in the Cold

An ultrasonic sensor with two round transducers on a small robot

Put an HC-SR04 exactly one meter from a wall in an unheated garage at 0°C, and the standard formula reports 104 centimeters. Warm the same garage to 35°C and it reports 98. The sensor didn’t change and the wall didn’t move. The speed of sound did, and the formula printed on the module’s datasheet quietly assumes a warm room. The figures below come from the HC-SR04 datasheet and from speed-of-sound calculations based on the ideal-gas formula, worked out in Python.

Video: DroneBot Workshop on YouTube

How an echo becomes a distance

Chirp, wait, listen

The HC-SR04 datasheet hosted by SparkFun describes the sequence. A microcontroller sends a 10-microsecond pulse to the trigger pin. The module fires a short burst of 40 kHz ultrasound, well above human hearing, from one of its two round transducers. The other transducer listens for the echo. The echo pin goes high when the burst leaves and drops when the echo returns, so the length of that high pulse is the round-trip time.

The datasheet’s arithmetic

The datasheet offers two shortcuts and one formula:

  • Pulse width in microseconds divided by 58 gives centimeters.
  • Pulse width in microseconds divided by 148 gives inches.
  • Distance equals pulse time multiplied by the speed of sound, 340 m/s, divided by two.

The division by two is because the sound travels out and back. The two shortcuts quietly assume a speed of sound. Working backwards, dividing by 58 implies 344.8 m/s, and dividing by 148 implies 343.2 m/s.

The rest of the spec sheet

Item Datasheet value
Supply 5 V DC
Current 15 mA
Range 2 cm to 4 m
Measuring angle 15 degrees
Trigger 10 µs TTL pulse
Measurement cycle Over 60 ms recommended
Closed garage door during winter

Sound speed is a thermometer

The physics

Sound in air travels faster when the air is warmer, because the molecules carrying the wave move faster. HyperPhysics’ page on sound speed in gases derives it from the ideal-gas relationship and gives dry air at 0°C as 331.39 m/s, 332.00 m/s at 1°C, and 343 m/s at 20°C. The Simon Fraser University Handbook for Acoustic Ecology gives the rule of thumb: about 0.61 m/s faster for every degree Celsius.

The datasheet’s 340 m/s corresponds to roughly 15°C. The divide-by-58 shortcut corresponds to about 22°C.

What that does to a reading

A Python calculation put a target exactly 1.000 m away, worked out the true echo time at each temperature with the ideal-gas formula, then converted it back to distance with the datasheet’s divide-by-58 rule:

Air temperature Speed of sound Echo time Distance reported Error
−10°C 325.2 m/s 6,150 µs 1.060 m +6.0 cm
0°C 331.3 m/s 6,036 µs 1.041 m +4.1 cm
20°C 343.2 m/s 5,827 µs 1.005 m +0.5 cm
35°C 351.9 m/s 5,683 µs 0.980 m −2.0 cm

The error grows with distance. At 3 m on a freezing day, the same rule would be off by about 12 cm.

The fix

Measure the air temperature, compute the speed of sound, and use it in the formula instead of a constant. HyperPhysics gives the near-room-temperature approximation the SFU handbook also uses: start at about 331.4 m/s and add about 0.6 m/s per degree Celsius. A cheap temperature sensor next to the HC-SR04 removes most of the error in the table. Humidity also nudges the speed upward slightly, a much smaller effect than temperature.

Timing limits and the 60 ms rule

How long an echo takes

At 20°C, an echo from 4 m, the datasheet maximum, returns after about 23.3 milliseconds. An echo from the 2 cm minimum returns in about 117 microseconds, which is close to the length of the outgoing burst itself; at shorter distances the module is still ringing from its own transmission and can’t hear the return.

Why wait 60 ms

The datasheet recommends a measurement cycle of more than 60 ms. A new chirp sent too soon can collide with late echoes from the previous one, bouncing off a far wall, and the module may time the wrong echo. The 60 ms gap leaves room for sound to die out well beyond the rated range. In practice that caps a single sensor at around 16 readings per second.

Several sensors at once

Robots often carry several HC-SR04s pointing in different directions. Firing them at the same moment invites crosstalk, where one sensor hears another’s chirp and reports a distance to nothing. Triggering them one after another, each with its own quiet period, avoids it.

A white robot standing in front of a black background

What the sensor can and can’t see

The beam is a cone

The datasheet lists a 15-degree measuring angle. Treated as the full width of the beam, that cone is about 26 cm wide at 1 m and 53 cm wide at 2 m. The sensor reports the nearest thing anywhere in that cone, which is why a reading aimed at a far wall can return the distance to a chair leg off to one side. (Real beam patterns are less tidy than a cone, and the edge of detection depends on the target.)

Surfaces matter

The datasheet says the target area should be at least 0.5 square meters and as smooth as possible for best results. Hard, flat surfaces facing the sensor reflect the ultrasound straight back. Soft materials such as curtains, foam and clothing absorb it. Surfaces at a steep angle reflect it away, so the echo never returns, and the module reports either nothing or a much longer bounce path.

When readings jump

Occasional wild readings are normal. Taking several measurements and using the median, rather than the average, removes single bad echoes without being dragged off by them.

Wiring to 3.3 V boards

The echo pin is 5 V

The HC-SR04 runs on 5 V, and its echo output swings to 5 V. A Raspberry Pi’s GPIO pins are 3.3 V devices. Connecting the echo pin straight to a Pi risks damaging it. A voltage divider fixes it: a 1 kΩ resistor in series and a 2 kΩ resistor to ground bring 5 V down to about 3.3 V. The trigger input accepts a 3.3 V pulse on most modules, since it only needs to read as high.

3.3 V versions

Variants sold as HC-SR04P are listed by sellers as working from 3 V to 5.5 V, which avoids the divider on 3.3 V boards. Checking the listing and the board marking matters, because the original and the variant look nearly identical.

Ground first

The datasheet adds an unusual wiring note: connect the ground terminal before power, because connecting power first can affect normal operation. On a breadboard that means plugging in the ground wire before the 5 V wire.

Where to buy

The modules are cheap and usually sold in multipacks:

As an Amazon Associate I earn from qualifying purchases. A commission may be earned if you buy through this link, at no extra cost to you.

A close up of a raspberry board on a table

Getting good readings

A checklist

  • Wait at least 60 ms between triggers.
  • Correct for air temperature instead of trusting the divide-by-58 shortcut.
  • Take three to five readings and keep the median.
  • Aim at large, hard, flat surfaces square to the sensor.
  • Divide the echo pin down to 3.3 V on Raspberry Pi and other 3.3 V boards, or use a 3.3 V variant.

The correction in code

On an Arduino, the temperature correction is two extra lines. The sketch below assumes the air temperature in Celsius is already read from a separate sensor, and uses Arduino’s pulseIn with a 30 ms timeout so a missing echo returns zero instead of hanging:

digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 30000);        // 0 if no echo
float speed = 331.4 + 0.6 * tempC;                    // m/s
float cm = us * 1e-6 * speed / 2.0 * 100.0;           // one-way distance

A reading of zero means no echo arrived in time, which should be treated as “nothing in range” rather than a distance of zero.

What it’s good for

The HC-SR04 does well at the jobs it was built for: obstacle detection on small robots, parking aids, tank and bin level sensing, and anything where a few centimeters of error is fine. In each of those, the sensor is really measuring time, and the conversion to distance is only as good as the speed of sound plugged into it. A cold garage turns a precise stopwatch into a slightly optimistic tape measure, and a five-cent temperature correction turns it back.

Sources

  • HC-SR04 Ultrasonic Ranging Module datasheet, hosted by SparkFun: https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
  • Sound Speed in Gases, HyperPhysics (Georgia State University): https://hyperphysics.gsu.edu/hbase/Sound/souspe3.html
  • Speed of Sound, Handbook for Acoustic Ecology, Simon Fraser University: https://www.sfu.ca/sonic-studio-webdav/cmns/Handbook5/handbook/Speed__Of_Sound.html

820 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *