Collection: Ultrasonic & Distance Sensors

Distance sensors let robots avoid walls, bins report how full they are, and tanks report their water level. Three types cover most projects.

  • The HC-SR04 ultrasonic sensor sends a 40 kHz ping and times the echo. Distance in centimetres is the echo pulse width divided by 58, with no library needed. Readings past about 3 m get unreliable.
  • Time-of-flight (ToF) sensors such as the VL53L0X and VL53L1X time reflected laser light. They give fine, millimetre-level distance over I2C at shorter range, but bright ambient light can roughly halve their range.
  • IR obstacle sensors only tell you whether something is within range, with no distance number.

How to choose:

  • Pick ultrasonic for cheap, general distance.
  • Pick ToF for precise short range or a narrow beam.
  • Pick IR for simple "is something there?" triggers.

All of them work with Arduino and ESP32. The HC-SR04's Echo pin outputs 5V, so on a 3.3V board such as the ESP32 put a voltage divider on Echo before it reaches the GPIO.

Frequently asked questions

How do I convert HC-SR04 time to distance?

Divide the echo pulse width in microseconds by 58 to get centimetres.

Can I connect an HC-SR04 to an ESP32?

Yes, but add a voltage divider on Echo. It outputs 5V, and ESP32 pins are 3.3V only.

Why is my ToF sensor's range shorter outdoors?

Bright ambient light can roughly halve a VL53L0X's range compared with darkness.

Does an IR obstacle sensor measure distance?

No. It switches when something is within range. Use ultrasonic or ToF for a number.

Guides

Reference pages on soldr.ai

More guides on soldr.ai

Describe your build on Soldr and it picks the parts, writes and compiles the code, and shows how to wire them. Sign in and get 100 credits free.