Distance measurement is one of the most common requirements in robotics, automation, IoT, and embedded electronics. Three fundamentally different technologies dominate the market for hobbyist and industrial use: ultrasonic sensors, infrared (IR) sensors, and laser ranging (ToF) sensors. Each has unique strengths and weaknesses that make it ideal for some applications and unsuitable for others.
In this comprehensive comparison guide, you will understand the physics behind each technology, see side-by-side specification comparisons, and learn exactly which sensor to choose for your next project — from an obstacle-avoiding robot to an industrial liquid level monitor.
How Ultrasonic Sensors Work
Ultrasonic distance sensors use sound waves above the human hearing range (typically 25–40 kHz) to measure distance. The working principle is straightforward:
- The sensor emits a short burst of ultrasonic sound pulses from the transmitter element.
- The sound travels through air at approximately 343 m/s (at 20°C).
- When the sound wave hits an object, it reflects back to the receiver element.
- The sensor measures the time of flight (ToF) and calculates distance: d = (t × v) / 2
The most popular ultrasonic sensor in the maker community is the HC-SR04, which uses a 4-pin interface (VCC, GND, TRIG, ECHO). The HC-SR04 has a range of 2 cm to 400 cm with approximately 3 mm resolution.
JSN-SR04T Waterproof Ultrasonic Rangefinder Module
IP67-rated waterproof ultrasonic rangefinder ideal for liquid level measurement, outdoor obstacle detection, and tank monitoring.
How IR Distance Sensors Work
IR (Infrared) distance sensors come in two distinct types:
1. Triangulation IR Sensors (Sharp GP2Y Series)
These sensors emit a beam of infrared light and use a Position Sensitive Detector (PSD) to measure the angle of the reflected beam. The angle changes depending on the distance to the object — closer objects reflect the beam at a wider angle. This triangulation method is immune to the reflectivity of the target object, making it more reliable than simple amplitude-based IR sensors.
2. Reflective (Proximity) IR Sensors
These sensors measure the intensity of reflected IR light. Closer objects reflect more light = higher output. Disadvantage: highly dependent on object colour and reflectivity. A black object at 5 cm may produce the same reading as a white object at 20 cm.
In the context of this comparison, we focus primarily on triangulation IR sensors (Sharp GP2Y0A21YK, GP2Y0A02YK) which offer genuine distance measurement, not just proximity detection.
How Laser (ToF) Sensors Work
Time-of-Flight (ToF) laser ranging sensors emit pulses of near-infrared laser light and measure the precise time for the pulse to return. Because light travels at 3 × 10⁸ m/s, measuring time accurately to picoseconds gives centimetre or even millimetre precision.
The most popular low-cost ToF sensors use Single Photon Avalanche Diodes (SPADs) — highly sensitive photodetectors that can detect individual photons. The VL53L0X and VL53L1X from STMicroelectronics use this approach and interface via I2C, making them extremely easy to use with Arduino and ESP32.
Industrial LiDAR sensors (like the Benewake TF-Luna or the automotive-grade AD2-S-X3) extend this principle to much longer ranges and add multiple beams for 2D or 3D point cloud generation.
Benewake AD2-S-X3 Automotive-Grade LiDAR
High-performance 3D LiDAR for autonomous driving and advanced robotics. Delivers detailed point cloud data for precise environment mapping.
Full Comparison Table
| Parameter | Ultrasonic (HC-SR04) | IR Triangulation (Sharp) | Laser ToF (VL53L1X) |
|---|---|---|---|
| Range | 2 cm – 4 m | 10 cm – 1.5 m | 4 cm – 4 m |
| Resolution | 3 mm | 5–10 mm | 1 mm |
| Accuracy | ±3 mm | ±5–15 mm | ±3 mm (short), ±5% (long) |
| Update rate | 40 Hz (max) | Continuous analog | Up to 100 Hz |
| Beam width | 15°–30° cone | 5°–10° cone | <1° (pencil beam) |
| Interface | TRIG/ECHO (PWM) | Analog voltage | I2C |
| Works in sunlight | Yes | Poor | Limited |
| Works on glass/clear objects | No | No | Yes |
| Works on black/dark objects | Yes | Reduced range | Reduced range |
| Indoor/outdoor | Both | Indoor only | Primarily indoor |
| Price (approx.) | ₹40–150 | ₹200–600 | ₹300–800 |
| Arduino ease of use | Very easy | Easy (analogRead) | Easy (I2C library) |
Ultrasonic Sensor: Deep Dive
Popular Models
- HC-SR04: The classic. 2–400 cm, 3 mm resolution, 5V. Works with nearly every microcontroller.
- JSN-SR04T: Waterproof version. IP67. Single probe design — transmitter and receiver in one metal housing. Ideal for liquid level and outdoor use.
- HY-SRF05: Extended range version (up to 4.5 m), also supports MODE pin for single-pin operation.
- 25 kHz T25 16mm: Separate transmitter and receiver transducers for custom installations.
Strengths
- Works regardless of object colour, reflectivity (as long as the object is not too soft/angled)
- Works in complete darkness
- Unaffected by ambient light (including sunlight)
- Very low cost
- Simple interface — any GPIO pin can drive it
- Waterproof versions available for harsh environments
Weaknesses
- Wide beam angle (15–30°) means small objects can be missed or multiple objects produce a confusing reading (measures the nearest object in the cone)
- Cannot detect objects that absorb sound (soft foam, fur, dense fabric)
- Cannot detect transparent objects (glass, clear plastic — sound passes through)
- Speed of sound varies with temperature (0.17% per °C) — a 10°C temperature change causes 1.7% range error
- Minimum range of 2 cm (sensor cannot distinguish transmitted from received pulse)
- Sensitive to wind in outdoor environments (wind changes effective sound velocity)
25kHz Ultrasonic Sensor Transmitter T25 16mm
Industrial 25 kHz ultrasonic transmitter for custom ranging, flow measurement, and level sensing installations.
IR Sensor: Deep Dive
Popular Models
- Sharp GP2Y0A21YK0F: 10–80 cm, analog output (2.3 V at 10 cm, 0.4 V at 80 cm)
- Sharp GP2Y0A02YK0F: 20–150 cm extended range version
- Sharp GP2Y0D810Z0F: Digital output proximity sensor (10 cm range)
Strengths
- Simple analog output — connect directly to Arduino’s analogRead pin
- No minimum blind spot
- Faster response than ultrasonic (continuous analog output)
- Compact form factor
- Not affected by soft/sound-absorbing objects
Weaknesses
- Greatly affected by ambient light — direct sunlight can saturate the receiver and give false readings
- Black or dark-coloured objects absorb IR and reduce maximum range by 30–50%
- Non-linear output — requires a lookup table or curve-fitting formula to convert voltage to distance
- Temperature affects output — recalibrate in extreme temperature environments
- Limited range (typically 10–150 cm maximum)
- Cannot measure distance to glass, clear plastics (IR passes through)
Arduino Distance Calculation for Sharp GP2Y0A21YK
int sensorPin = A0;
void loop() {
int rawValue = analogRead(sensorPin);
float voltage = rawValue * (5.0 / 1023.0);
// Curve-fit formula for GP2Y0A21YK (10-80cm)
float distance = 27.86 / (voltage - 0.42) + 0.32;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(100);
}
Laser ToF: Deep Dive
Popular Models
- VL53L0X: Range up to 200 cm (white targets), I2C, 3.3V/5V compatible, very compact. Entry-level ToF.
- VL53L1X: Improved version, range up to 400 cm (long-range mode), faster update rate (100 Hz), better ambient light immunity.
- VL6180X: Ultra-short range (5–100 mm), designed for ambient light sensing combined with distance.
- TF-Luna: Long-range (up to 8 m), serial (UART/I2C), 250 Hz update rate. Popular in robotics and drones.
Strengths
- Highest accuracy (±3 mm for close range) of any low-cost sensor type
- Pencil-thin laser beam — can measure precisely through small apertures and around obstructions
- Works on transparent objects (glass, clear plastic)
- Fast update rate (up to 100 Hz for VL53L1X)
- Compact I2C module — easy to integrate
- Works on any colour or texture
Weaknesses
- More expensive than HC-SR04
- Outdoor performance limited by sunlight (ambient photon flooding)
- Very narrow beam — misses objects not directly in the line of sight
- I2C address conflicts when using multiple sensors on one bus (must use XSHUT pin or I2C multiplexer)
- Accuracy degrades on highly reflective (mirror-like) or highly absorptive (flat black paint) surfaces
Arduino Code Examples
HC-SR04 Ultrasonic Sensor
#define TRIG_PIN 9
#define ECHO_PIN 10
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration * 0.0343) / 2; // cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(100);
}
VL53L1X Laser ToF Sensor
#include <Wire.h>
#include <VL53L1X.h>
VL53L1X sensor;
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // 400kHz I2C
sensor.setTimeout(500);
if (!sensor.init()) {
Serial.println("VL53L1X not found!");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long); // Long, Medium, or Short
sensor.setMeasurementTimingBudget(50000); // 50ms
sensor.startContinuous(50);
}
void loop() {
sensor.read();
Serial.print("Distance: ");
Serial.print(sensor.ranging_data.range_mm);
Serial.println(" mm");
}
Key Limitations to Know Before You Buy
Ultrasonic Blind Zone
HC-SR04 cannot measure objects closer than 2 cm because the electrical “ringing” from the transmitter pulse overlaps with any close reflected echo. For close-range applications, use an IR or ToF sensor.
IR Sunlight Problem
Sharp IR sensors use modulated 38 kHz IR light and a bandpass filter, but very bright sunlight saturates the photodetector. Never mount IR distance sensors pointing toward the sun or a sunny window. Use HC-SR04 or ToF sensors outdoors.
ToF Multiple Sensors
All VL53Lxx sensors have the same default I2C address (0x29). If you need multiple ToF sensors on one Arduino, you must either: (a) use their XSHUT pin to disable all but one and reassign addresses at startup, or (b) use an I2C multiplexer (TCA9548A).
Sound Speed Temperature Compensation
For accurate ultrasonic measurements, compensate for temperature:
// Speed of sound: v = 331.3 + 0.606 × T_celsius (m/s) float temp = 25.0; // Read from a temperature sensor for accuracy float soundSpeed = 331.3 + (0.606 * temp); float distance = (duration * soundSpeed / 10000.0) / 2; // cm
How to Choose the Right Sensor
| Use Case | Best Choice | Reason |
|---|---|---|
| Obstacle avoidance robot (indoor) | HC-SR04 | Low cost, works on all colours |
| Liquid level monitoring | JSN-SR04T (waterproof ultrasonic) | IP67 rated, works on water |
| Precision positioning (<1 m) | VL53L1X ToF | 1 mm resolution, fast update |
| Gesture control | VL53L0X ToF | Fast, compact, pencil beam |
| Outdoor parking sensor | Ultrasonic (waterproof) | Not affected by sunlight |
| Shelf stocking detection | IR (GP2Y0A21) | Fast response, cost-effective |
| Autonomous drone altitude | TF-Luna ToF | Long range, 250 Hz update |
| Detecting glass objects | ToF Laser | Laser reflects from glass surfaces |
Application Examples
- Robotic arms: ToF sensors (VL53L1X) on end-effectors give precise part approach control without contact, preventing damage to delicate components.
- Underground tank level monitoring: Waterproof JSN-SR04T mounted at the top of a water tank measures liquid level by pinging down to the water surface — no submersed components.
- Smart parking systems: Ultrasonic sensors mounted above each parking bay detect vehicle presence and feed an availability display.
- Drone altitude hold: TF-Luna ToF sensor on the drone’s belly provides ground-relative altitude at up to 250 Hz for stable low-altitude hover.
- Industrial conveyor object counting: IR retro-reflective sensors count packages on a conveyor by detecting interruptions in a reflected IR beam.
- Elderly fall detection: Arrays of PIR motion sensors combined with ultrasonic distance sensors can detect a fall event (fast change in distance to floor level) in a room without cameras.
Frequently Asked Questions
Q: Can HC-SR04 detect water surface?
Yes. The HC-SR04 works well on water surfaces (and most liquids) for level measurement. However, the standard HC-SR04 is NOT waterproof — use the JSN-SR04T (Version 3.0) which has an IP67-rated stainless steel probe safe for mounting in damp or wet environments. Mount it facing downward above the liquid surface, not submerged.
Q: Why does my HC-SR04 give erratic readings?
Common causes: (1) Power supply noise — add a 100 µF capacitor across VCC/GND on the sensor. (2) Cable too long — keep TRIG and ECHO wires under 50 cm or use a level shifter for 3.3V Arduinos. (3) Multiple reflections — ultrasonic pulses can echo off walls and return as false readings. Use a cardboard baffle to narrow the beam. (4) Temperature change — speed of sound changes 0.17% per °C; compensate using a temperature sensor.
Q: Is a LiDAR sensor the same as a laser ToF sensor?
LiDAR (Light Detection And Ranging) is a broader term that includes any sensor using laser light for ranging. VL53L1X is technically a single-point LiDAR. When people say “LiDAR” in robotics and autonomous vehicles, they typically mean multi-beam rotary or solid-state LiDARs (like the Benewake AD2-S-X3) that generate 2D or 3D point clouds. Single-point ToF sensors like VL53L1X measure only one point per measurement.
Q: Can I use multiple HC-SR04 sensors simultaneously?
Yes, but trigger them sequentially, not simultaneously. If two HC-SR04 sensors fire at the same time, their ultrasonic pulses can interfere — one sensor may receive the other’s echo and report an incorrect distance. Add at least 60 ms between triggers. For simultaneous multi-sensor applications, use separate trigger/echo pins and stagger the trigger pulses in software.
Q: Does the HC-SR04 work with 3.3V microcontrollers (ESP32, ESP8266)?
The HC-SR04 requires 5V power for the ultrasonic transducers, but the ECHO output signal is also at 5V logic — which can damage 3.3V GPIO pins on ESP32 or Raspberry Pi. Use a voltage divider (1 kΩ + 2 kΩ) or a level-shifter module on the ECHO pin. The TRIG pin can be driven directly from 3.3V (5V HC-SR04 recognises 3.3V as HIGH). Alternatively, use an HC-SR04P which is designed for 3–5V supply voltage and has 3.3V-compatible IO.
Conclusion
There is no single “best” distance sensor — each technology is optimised for different conditions. Ultrasonic sensors are the workhorse: robust, cheap, colour-blind, and weather-resistant. IR sensors are fast and compact but limited by ambient light and object colour. Laser ToF sensors are the most accurate and precise but cost more and are best suited to indoor environments.
For most starter robot projects, the HC-SR04 or JSN-SR04T is the right first sensor. When you need higher precision or need to detect glass objects, upgrade to a VL53L1X. For demanding outdoor or long-range applications, specialist LiDAR modules like the Benewake TF-Luna bridge the gap to industrial performance.
Find Your Ideal Distance Sensor at Zbotic
Zbotic stocks the full range — from waterproof JSN-SR04T ultrasonic sensors to advanced LiDAR modules. Get the right sensor for your project today.
Add comment