Skip to content

HC-SR04 — distance [mm] (hcsr_read_mm)

What it does: Returns the distance from HC-SR04 in millimeters. In the internal implementation, up to 3 attempts will take place and the value is calculated from the length of the ECHO pulse.

hcsr_read_mm

Inputs

  • Field hc_id: selection of previously initialized sensor instance.

Output

  • Number: distance in mm (may return default value 2000 mm if measurement fails).

When to use it

  • When you need a distance with a finer resolution in millimeters.
  • After initialization via hcsr_init.

Tips and pitfalls

  • The output is an integer conversion from the pulse time (pulse_time * 100 // 582).
  • You may get a default value when interfering or out of range, so it's a good idea to add filtering.

Usage example

example_hcsr

Open example in ESP IDE

Generated Python example

value = ultraSoundSensor1.distance_mm()