Best Distance Sensor for Arduino and ESP32

The best distance sensor for Arduino and ESP32 projects isn’t one product, it’s a choice between four different ways of measuring the gap between your board and whatever it’s pointed at: sound waves, infrared light, laser time-of-flight, or LiDAR. Pick the wrong sensing technology and you’ll spend a weekend fighting inconsistent readings instead of finishing your build.

This guide covers six sensors across those four technologies, from the classic ultrasonic module every beginner starts with to a proper LiDAR unit for outdoor and long-range work. Each one is grounded in real specs and known behavior, not marketing copy, so you can match the sensor to the project instead of guessing.

Building a robot that needs to avoid obstacles? Check out our Best Arduino Robot Car Kits guide, and if you want a broader sensor collection to experiment with first, the Best Arduino Sensor Kit roundup is a good place to start.


Quick Comparison Table

SensorTechnologyRangeInterfaceBest For
ELEGOO HC-SR04 (5-pack)Ultrasonic2cm to 400cmDigital (trigger/echo)Budget/beginner projects
HiLetgo JSN-SR04T (2-pack)Waterproof ultrasonic20cm to 450cmDigital (trigger/echo)Outdoor and liquid-level sensing
HiLetgo VL53L0XToF laserUp to 2mI2CShort-range precision sensing
CQRobot VL53L1XToF laserUp to 4mI2CLonger-range precision sensing
Benewake TF-LunaLiDAR0.2m to 8mUART or I2CLong-range/outdoor ranging
Comimark Sharp GP2Y0A21YK0FIR analog10cm to 80cmAnalogSimple wiring, no bus needed

1. Best Budget Ultrasonic Sensor: ELEGOO HC-SR04 (5-Pack)

ELEGOO HC-SR04 Ultrasonic Distance Sensor (5-Pack)

Best for: Beginners who want the cheapest, most documented way to measure distance on Arduino.

Best distance sensor for Arduino: ELEGOO HC-SR04 ultrasonic sensor 5-pack

The HC-SR04 is the sensor almost every Arduino tutorial starts with, and there’s a reason for that. It sends a 40kHz ultrasonic pulse from one transducer and times how long the echo takes to return on a second transducer, then converts that time into a distance using the speed of sound. The whole thing runs off two digital pins and a few lines of code, no libraries required for basic use.

Coming as a 5-pack, this is the cheapest way to stock up if you’re building more than one project or want spares on hand for a robotics build where sensors occasionally get damaged during assembly. It’s not the most precise sensor here, but for obstacle avoidance, parking sensors, or liquid-level projects where you need “roughly how far,” it’s hard to beat for the price.

Specifications:

  • Range: 2cm to 400cm
  • Resolution: approximately 0.3cm
  • Beam angle: approximately 15 degrees
  • Voltage: 5V
  • Interface: digital trigger and echo pins (pulse width timing)
  • Quantity: 5 sensors per pack

Why it stands out:

  • Cheapest reliable way to add distance sensing to an Arduino project
  • Massive tutorial and code library base, including the dedicated ArduinoYard guide linked below
  • Works directly off two digital GPIO pins with no I2C addressing to configure
  • 5-pack means spares on hand for robotics builds where sensors take the occasional hit
  • Solid accuracy for its price on hard, flat, non-absorbent surfaces

Things to keep in mind:

  • Ultrasonic readings get unreliable on soft, angled, or sound-absorbing surfaces like foam or fabric
  • Not waterproof, so outdoor or liquid-level use needs the JSN-SR04T instead
  • Owners occasionally report needing a small filtering routine in code to smooth out noisy readings

馃憠 Buy ELEGOO HC-SR04 Ultrasonic Sensor on Amazon

Verdict: The right starting point for almost any Arduino distance project. It’s cheap, well documented, and good enough for obstacle avoidance and general-purpose ranging where millimeter precision doesn’t matter.


2. Best Waterproof Ultrasonic Sensor: HiLetgo JSN-SR04T (2-Pack)

HiLetgo JSN-SR04T Waterproof Ultrasonic Sensor Module (2-Pack)

Best for: Outdoor projects and liquid-level sensing where the standard HC-SR04 can’t survive the environment.

HiLetgo JSN-SR04T waterproof ultrasonic sensor module

The JSN-SR04T splits the sensor into two pieces: a control board that stays dry, connected by a cable to a waterproof transducer probe that can be mounted outside an enclosure or over a water tank. That separation is the entire point of this sensor. Where the HC-SR04 fails the moment it gets rained on, the JSN-SR04T’s sealed probe handles outdoor exposure and condensation without issue.

It uses the same trigger and echo timing approach as the HC-SR04, so code written for one largely works for the other with minor timing adjustments. The tradeoff is a longer minimum blind zone, this sensor doesn’t read reliably much closer than 20 to 25cm, so it’s not the right pick for close-range obstacle detection.

Specifications:

  • Range: 20cm to 450cm
  • Voltage: 5V
  • Interface: digital trigger and echo pins (pulse width timing)
  • Probe: waterproof, separate from control board via cable
  • Beam angle: wider than HC-SR04, better suited to open-area sensing
  • Quantity: 2 sensors per pack

Why it stands out:

  • Waterproof probe survives rain, humidity, and direct exposure to liquids
  • Separated cable design lets you mount the sensor probe outside an enclosure while keeping electronics protected
  • Wider beam angle works well for tank level monitoring and open outdoor spaces
  • Compatible with most existing HC-SR04 Arduino code with minor adjustments
  • Two sensors per pack for projects needing redundancy or multiple monitoring points

Things to keep in mind:

  • Minimum reliable range is around 20 to 25cm, not suitable for close obstacle detection
  • Slightly more involved wiring than the HC-SR04 due to the separate probe cable
  • Reviewers note mounting angle matters more here since the probe needs a clear, unobstructed path to whatever it’s measuring

馃憠 Buy HiLetgo JSN-SR04T Waterproof Ultrasonic Sensor on Amazon

Verdict: The obvious upgrade over the HC-SR04 for anything outdoors or near water. Water tank level monitoring and weatherproof obstacle sensing are exactly what this sensor was built for.


3. Best I2C Time-of-Flight Sensor: HiLetgo VL53L0X

HiLetgo VL53L0X ToF Laser Distance Sensor Breakout

Best for: Short-range precision sensing where ultrasonic accuracy isn’t good enough.

HiLetgo VL53L0X time-of-flight laser distance sensor breakout for Arduino

The VL53L0X, designed by STMicroelectronics, measures distance completely differently from an ultrasonic sensor. It fires an eye-safe class 1 laser pulse at 940nm and directly times how long the light takes to return, rather than inferring distance from a sound echo. That gives it tighter, more consistent accuracy at short range, and it isn’t fooled by soft or angled surfaces the way ultrasonic sensors can be.

It talks over I2C, which means it shares the same two wires as your other I2C sensors and displays, and it includes an XSHUT pin that lets you address multiple VL53L0X units on a single bus, something covered in the educational section below. Ranging is fast, often under 30 milliseconds per reading, which matters for anything that needs to react quickly to a changing distance.

Specifications:

  • Range: up to 2m (most reliable within roughly 1.2m)
  • Laser: 940nm, class 1 (eye-safe)
  • Interface: I2C, default address 0x29
  • Ranging time: as fast as 30ms
  • Voltage: 3.3V (breakout typically 5V tolerant with onboard regulation)
  • Multi-sensor addressing: via XSHUT pin

Why it stands out:

  • Direct time-of-flight measurement is more precise and consistent than ultrasonic at short range
  • Fast ranging speed suits projects that need quick reaction to changing distance
  • I2C interface shares the same bus as other sensors and displays, fewer pins used overall
  • XSHUT pin makes multi-sensor setups on one Arduino easy to wire and address
  • Eye-safe laser class means no special precautions needed for hobbyist use

Things to keep in mind:

  • Range drops noticeably on dark or low-reflectivity surfaces, since less laser light bounces back
  • Doesn’t perform reliably through glass or on highly reflective mirrored surfaces
  • Cheaper clone breakout boards occasionally ship with inconsistent I2C address defaults, worth confirming in code before wiring multiples

馃憠 Buy HiLetgo VL53L0X ToF Sensor on Amazon

Verdict: The right pick when ultrasonic accuracy falls short and you need consistent short-range readings. It’s a small step up in wiring complexity for a real jump in precision.


4. Best Long-Range Time-of-Flight Sensor: CQRobot VL53L1X

CQRobot VL53L1X Time-of-Flight Long Distance Ranging Sensor

Best for: Projects that need ToF-level precision but need to see further than the VL53L0X can reach.

CQRobot VL53L1X long-range time-of-flight distance sensor breakout

The VL53L1X is ST’s successor to the VL53L0X, and the headline improvement is range: up to 4m compared to the VL53L0X’s roughly 2m ceiling. It keeps the same direct time-of-flight measurement principle and I2C interface, but adds configurable distance modes (short, medium, long) and an adjustable field of view between 15 and 27 degrees, letting you tune the sensor’s cone of detection to the project.

CQRobot’s breakout adds onboard voltage regulation, so it plays nicely with both 3.3V boards like the ESP32 and 5V Arduino boards without extra level-shifting hardware. That flexibility, combined with the longer range, makes it the better choice for robotics projects that need to see obstacles further out and react earlier.

Specifications:

  • Range: up to 4m
  • Interface: I2C
  • Distance modes: short, medium, long (software configurable)
  • Field of view: adjustable, 15 to 27 degrees
  • Voltage: 3.3V to 5V (onboard regulation on CQRobot breakout)

Why it stands out:

  • Doubles the effective range of the VL53L0X while keeping the same I2C simplicity
  • Configurable distance modes let you trade range for ranging speed depending on the application
  • Adjustable field of view is useful for narrowing detection to a specific target zone
  • Onboard voltage regulation makes it compatible with both 3.3V and 5V boards out of the box
  • CQRobot provides a detailed manual, useful for a sensor with more configuration options than the VL53L0X

Things to keep in mind:

  • Bulkier breakout board than the VL53L0X, worth checking clearance in tight enclosures
  • More configuration options mean a slightly steeper learning curve for first-time ToF users
  • Still shares the same low-reflectivity surface limitation as other laser ToF sensors

馃憠 Buy CQRobot VL53L1X ToF Sensor on Amazon

Verdict: The sensor to reach for when the VL53L0X’s 2m range isn’t enough but you still want laser-grade precision over ultrasonic. A strong middle ground between short-range ToF and full LiDAR.


5. Best LiDAR Sensor: Benewake TF-Luna

Benewake TF-Luna LiDAR Distance Sensor Module

Best for: Long-range and outdoor distance sensing where ultrasonic and short-range ToF sensors run out of reach.

Benewake TF-Luna LiDAR distance sensor module for Arduino and ESP32

The TF-Luna is a proper single-point LiDAR module, a step up in both range and cost from the VL53L1X. It measures 0.2m to 8m indoors using the same time-of-flight principle as the VL53 sensors, but with a more powerful IR laser and dedicated onboard processing, which gets it to genuinely long-range readings that ultrasonic sensors and short-range ToF modules can’t touch.

It supports both UART (the default) and I2C, selectable via a solder pad on the board, so it fits into whatever communication scheme the rest of your project already uses. The narrow roughly 2-degree field of view makes it good at picking out a specific target rather than averaging everything in a wide cone, which matters for applications like drone altitude hold or precise robotic navigation.

Specifications:

  • Range: 0.2m to 8m indoors (reduced in direct sunlight, typically 2 to 3m outdoors)
  • Interface: UART (default) or I2C, selectable
  • Field of view: approximately 2 degrees
  • Frame rate: configurable up to 250Hz
  • Accuracy: approximately 卤6cm
  • Voltage: 5V

Why it stands out:

  • Longest range in this lineup by a wide margin, up to 8m indoors
  • Narrow field of view gives precise single-point readings instead of a wide averaged cone
  • Configurable frame rate up to 250Hz suits fast-reacting applications like drones
  • Dual interface support (UART or I2C) fits into more project architectures without adapters
  • Genuine LiDAR-grade performance at a price still reasonable for hobbyist projects

Things to keep in mind:

  • Outdoor range drops significantly in direct sunlight due to ambient IR interference, a known limitation of this sensor class
  • More expensive than every other sensor in this roundup
  • Stable, vibration-free mounting matters more here since the narrow beam is more sensitive to angle changes than a wide-cone ultrasonic sensor

馃憠 Buy Benewake TF-Luna LiDAR Module on Amazon

Verdict: The sensor to buy when range is the priority and budget allows for it. Nothing else in this guide gets close to its indoor range or narrow-beam precision.


6. Best Analog IR Distance Sensor: Comimark Sharp GP2Y0A21YK0F

Comimark Sharp GP2Y0A21YK0F Analog IR Distance Sensor

Best for: Simple wiring with no I2C bus or pulse-timing code required.

Comimark Sharp GP2Y0A21YK0F analog infrared sensor module

The Sharp GP2Y0A21YK0F works on a completely different principle again: infrared triangulation. It emits an IR beam and reads the angle at which it bounces back on an internal sensor array, then outputs that as an analog voltage. There’s no trigger pulse to time and no I2C bus to configure, just three wires (power, ground, and an analog output pin), which makes it the simplest sensor in this guide to wire up.

The catch is that the voltage-to-distance relationship is nonlinear, so getting an accurate distance reading in centimeters requires applying a conversion formula or lookup table in code rather than a simple linear scale. The datasheet also recommends adding a capacitor across the power pins, since the sensor’s internal IR emitter pulses and can introduce noise onto the supply line without one.

Specifications:

  • Range: 10cm to 80cm
  • Output: analog voltage (nonlinear, inversely related to distance)
  • Voltage: 4.5V to 5.5V
  • Connector: 3-pin JST
  • Interface: single analog input pin, no digital timing or bus required

Why it stands out:

  • Simplest wiring of any sensor in this guide: three wires and one analog pin
  • No I2C addressing or pulse-timing code needed, good for beginners just learning analogRead()
  • Reliable performance on a wider range of surface colors and textures than ultrasonic sensors
  • Long track record in the hobbyist community with extensive example code available
  • JST connector makes for a tidy, reliable physical connection compared to loose header pins

Things to keep in mind:

  • Nonlinear output requires a conversion formula or lookup table for accurate distance readings
  • Shortest maximum range in this lineup at 80cm
  • Datasheet recommends a smoothing capacitor across the power pins, which some cheaper breakout versions omit

馃憠 Buy Comimark Sharp GP2Y0A21YK0F IR Distance Sensor on Amazon

Verdict: The easiest sensor here to wire and understand for a first analog sensing project, provided the 80cm range and one-time calibration work fit your build.


Which Distance Sensor Should You Buy?

The right distance sensor for Arduino or ESP32 comes down to three questions: how far do you need to see, how much precision do you actually need, and how much wiring complexity are you willing to take on.

For most beginner projects, obstacle-avoidance robots, and general “how far away is that” builds, the ELEGOO HC-SR04 covers it at the lowest cost and with the most tutorial support available anywhere. If the project lives outdoors or near water, swap it for the HiLetgo JSN-SR04T and accept the longer minimum range in exchange for weatherproofing.

When ultrasonic accuracy isn’t tight enough, the VL53L0X and VL53L1X are the natural next step. Pick the VL53L0X for short-range work under 2m where fast ranging matters, and the VL53L1X when you need that same precision extended out to 4m. Both share the same I2C simplicity, so switching between them later doesn’t mean rewriting your whole project.

For long-range or outdoor work where even the VL53L1X can’t see far enough, the TF-Luna is the sensor built for that job, with the tradeoff of a higher price and reduced range in direct sunlight. And if you’d rather skip digital timing and I2C addressing entirely, the Sharp GP2Y0A21YK0F gets you working distance sensing off a single analog pin, at the cost of a shorter range and a bit of calibration work in code.


Ultrasonic vs. IR vs. Time-of-Flight vs. LiDAR: How Distance Sensing Actually Works

All six sensors in this guide answer the same question, “how far away is that object,” but they get there through genuinely different physics, and understanding the difference explains why each one behaves the way it does.

Ultrasonic sensors like the HC-SR04 and JSN-SR04T send a burst of sound above human hearing range and measure how long the echo takes to return. Since sound travels at a known, fairly constant speed through air, that timing converts directly into distance. The catch is that soft or angled surfaces absorb or deflect sound waves instead of reflecting them straight back, which is why these sensors can give inconsistent readings against foam, fabric, or anything not roughly perpendicular to the sensor.

Time-of-flight (ToF) laser sensors like the VL53L0X and VL53L1X fire a laser pulse and measure the time it takes for the light itself to bounce back, essentially the same principle as ultrasonic but using light instead of sound. Because light travels so much faster than sound, these sensors need precise onboard timing electronics, which is why they’re more expensive than a basic ultrasonic module but deliver tighter, more consistent accuracy. They struggle on the opposite end from ultrasonic sensors: dark, low-reflectivity surfaces absorb the laser light instead of bouncing it back, reducing effective range.

LiDAR sensors like the TF-Luna use the same time-of-flight principle as the VL53 sensors but add a more powerful laser and dedicated signal processing to extend range dramatically, up to 8m indoors in the TF-Luna’s case. The tradeoff is cost and, for outdoor use, sensitivity to ambient infrared from direct sunlight, which competes with the sensor’s own laser signal and reduces effective range.

IR triangulation sensors like the Sharp GP2Y0A21YK0F work differently from all three: they emit an IR beam and measure the angle it returns at using an internal lens and sensor array, similar to how your eyes judge distance using the angle between them. That geometric approach is why the output is a nonlinear voltage rather than a clean digital distance value, the angle-to-distance relationship simply isn’t linear across the sensor’s range.


Running Multiple VL53L0X or VL53L1X Sensors on the Same I2C Bus

A common problem when building a robot with more than one distance sensor: every VL53L0X and VL53L1X ships with the same default I2C address, so wiring two of them to the same bus without extra steps means they’ll collide and only one will respond reliably.

The fix uses the XSHUT pin included on both sensors. Wire each sensor’s XSHUT pin to a separate digital GPIO pin on your Arduino or ESP32, then in your setup code, hold all sensors in shutdown except one, assign that sensor a new I2C address in software, bring it out of shutdown, and repeat for each additional sensor. Once every sensor has a unique address, all of them can stay active on the same two I2C wires (SDA and SCL) simultaneously.

This is exactly the setup used in multi-sensor obstacle-avoidance robots that need to see left, right, and forward at once, three VL53L0X units, three XSHUT pins, three unique addresses assigned at startup, and a single I2C bus handling all of them afterward.


Final Recommendation

For most Arduino and ESP32 projects, the ELEGOO HC-SR04 remains the best all-around distance sensor for Arduino and ESP32 builds: cheap, well documented, and good enough for the vast majority of obstacle-avoidance and general ranging projects. If your build needs more precision or a longer effective range, step up to the VL53L1X for the best balance of accuracy and range without LiDAR pricing.

ArduinoYard has detailed build guides that pair directly with two of the sensors in this roundup: the Ultrasonic Sensor HC-SR04 with Arduino tutorial walks through wiring and code from scratch, the Waterproof Ultrasonic Sensor JSN-SR04T with Arduino guide covers the outdoor-rated version in detail, and the Autonomous Edge-Detection Robot with Arduino project shows distance sensing applied to a real obstacle-avoidance build.


Looking to round out the rest of your robotics build? Check out our guides on the Best Arduino Robot Car Kits, Best DC Motor Driver for Arduino and ESP32, Best Relay Module for Arduino and ESP32, and Best Temperature and Humidity Sensor for Arduino and ESP32.

Leave a Comment