Back to Guides
Makers Guide / Sensors

Complete Guide to Distance Measurement Sensors

Distance Sensors Technology

Distance measurement is one of the most fundamental capabilities in robotics, automation, and IoT projects. From simple obstacle detection to precision manufacturing, choosing the right distance sensor can make or break your project. This comprehensive guide covers all major types of distance sensors, their working principles, and when to use each one.

📊 What You'll Learn

  • How ultrasonic, infrared, and laser sensors measure distance
  • Time-of-Flight (ToF) vs triangulation techniques
  • ToF cameras and 3D depth sensing
  • PMD (Photonic Mixer Device) technology
  • Choosing the right sensor for your application

1. Ultrasonic Distance Sensors

Ultrasonic sensors are the workhorses of hobbyist distance measurement. They use sound waves (typically 40kHz) to measure distance by calculating the time it takes for an echo to return.

HC-SR04 Ultrasonic Sensor

HC-SR04

The most popular ultrasonic sensor for Arduino and ESP32 projects. Affordable and reliable for basic robotics.

Range: 2-400cm Accuracy: ±3mm 5V Logic
JSN-SR04T Waterproof Ultrasonic

JSN-SR04T (Waterproof)

Weatherproof variant with a separated probe, ideal for outdoor applications and liquid level measurement.

Range: 25-450cm IP67 Rated Cable: 2.5m

How Ultrasonic Sensors Work

The sensor emits a burst of ultrasonic pulses and listens for the echo. The time between transmission and reception is measured, and distance is calculated using:

Distance = (Time × Speed of Sound) / 2

// Speed of sound ≈ 343 m/s at 20°C
// Division by 2 because sound travels TO the object and BACK

💡 HC-SR04 Arduino Code

const int trigPin = 9;
const int echoPin = 10;

void setup() {
    Serial.begin(115200);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

void loop() {
    // Send 10µs pulse
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    
    // Measure echo duration
    long duration = pulseIn(echoPin, HIGH);
    
    // Calculate distance (cm)
    float distance = duration * 0.0343 / 2;
    
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    
    delay(100);
}

Pros and Cons

✅ Advantages ❌ Disadvantages
Very affordable ($1-5) Affected by temperature changes
Works in any lighting condition Wide beam angle (15-30°)
Good range (up to 4-5m) Struggles with soft/angled surfaces
Simple interface (trigger/echo) Minimum range ~2cm (blind zone)

2. Infrared (IR) Distance Sensors

IR distance sensors use infrared light to detect objects. They come in two main varieties: simple reflective (proximity) sensors and analog distance sensors that use triangulation.

Sharp GP2Y0A21 IR Distance Sensor

Sharp GP2Y0A21YK0F

Analog infrared distance sensor using triangulation. Industry standard for accurate mid-range measurement.

Range: 10-80cm Analog Output Triangulation
Sharp GP2Y0A02 Long Range

Sharp GP2Y0A02YK0F

Extended range variant for applications requiring longer detection distances.

Range: 20-150cm Analog Output 5V Operation

How IR Triangulation Works

Unlike simple IR proximity sensors that just detect presence, triangulation sensors (like Sharp GP2Y series) project an IR beam and use a Position Sensitive Detector (PSD) to measure the angle of the reflected light. The closer the object, the larger the angle—allowing precise distance calculation.

⚠️ Non-Linear Output

Sharp IR sensors have a non-linear voltage output. You cannot simply multiply voltage by a constant. Use a lookup table or the formula from the datasheet for accurate readings.

Sharp IR Sensor Code

const int irPin = A0;

void setup() {
    Serial.begin(115200);
}

void loop() {
    int rawValue = analogRead(irPin);
    float voltage = rawValue * (5.0 / 1023.0);
    
    // Approximate formula for GP2Y0A21 (10-80cm)
    // Distance = 27.86 / (Voltage - 0.42)
    float distance = 27.86 / (voltage - 0.42);
    
    // Constrain to valid range
    distance = constrain(distance, 10, 80);
    
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    
    delay(100);
}

3. Laser Distance Sensors (Time-of-Flight)

Time-of-Flight (ToF) sensors use laser or LED light and measure the time it takes for photons to travel to an object and back. This provides extremely accurate distance measurements in a compact form factor.

VL53L0X ToF Sensor

VL53L0X

World's smallest ToF sensor. I2C interface, perfect for drones, robots, and gesture detection.

Range: 50-1200mm I2C Interface ±3% Accuracy
VL53L1X Long Range ToF

VL53L1X

Extended range version with programmable FOV (field of view). Excellent for lidar applications.

Range: 40-4000mm Programmable FOV 940nm Laser

ToF vs Triangulation

Feature Time-of-Flight (ToF) Triangulation
Measurement Method Measures light travel time Measures reflection angle
Range 30mm - 4m+ (VL53L1X) 10cm - 1.5m typical
Accuracy ±1-3% typically ±5-10% typical
Size Very compact (few mm) Larger (requires baseline)
Cost $5-15 for modules $8-20 for Sharp sensors
Sunlight Immunity Good (with filtering) Poor (affected by ambient IR)

VL53L0X Code Example

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
    Serial.begin(115200);
    
    if (!lox.begin()) {
        Serial.println("Failed to find VL53L0X sensor!");
        while(1);
    }
    Serial.println("VL53L0X Ready!");
}

void loop() {
    VL53L0X_RangingMeasurementData_t measure;
    
    lox.rangingTest(&measure, false);
    
    if (measure.RangeStatus != 4) {  // Valid reading
        Serial.print("Distance: ");
        Serial.print(measure.RangeMilliMeter);
        Serial.println(" mm");
    } else {
        Serial.println("Out of range");
    }
    
    delay(100);
}

4. ToF Cameras (Depth Imaging)

ToF cameras take the single-point ToF concept and extend it to capture entire depth images. Each pixel measures distance independently, creating a 3D depth map of the scene.

📷 Popular ToF Camera Modules

  • Intel RealSense D435: Stereo depth + RGB camera, 1280x720 depth, USB3
  • Azure Kinect: Microsoft's advanced ToF sensor, 1MP depth, AI-ready
  • OAK-D: Combines stereo depth with AI processing on-device
  • PMD CamBoard pico: Compact ToF camera for embedded applications

How ToF Cameras Work

There are two main approaches:

  1. Direct ToF (dToF): Measures actual pulse travel time. Used in LiDAR and high-precision systems.
  2. Indirect ToF (iToF): Measures phase shift of modulated light. More common in consumer devices due to lower cost.

iToF cameras emit modulated light (typically sinusoidal) and measure the phase difference between transmitted and received signals. The phase shift directly correlates to distance:

Distance = (c × φ) / (4π × f_mod)

Where:
c = Speed of light (3×10⁸ m/s)
φ = Phase shift (radians)
f_mod = Modulation frequency

Applications of ToF Cameras

  • Gesture recognition (Microsoft Kinect, Leap Motion)
  • Autonomous vehicles and drones
  • 3D scanning and room mapping
  • Augmented reality (AR) devices
  • Industrial automation and bin-picking

5. PMD (Photonic Mixer Device) Technology

PMD sensors are a specialized form of ToF imaging that uses photonic mixing at the pixel level. Each pixel contains a demodulator that directly correlates the incoming light with the reference modulation signal.

🔬 PMD Advantages

  • Extremely high depth accuracy (sub-millimeter possible)
  • Real-time depth at high frame rates (60+ fps)
  • Compact sensor design
  • Less affected by motion blur than stereo vision
  • Works in complete darkness (active illumination)

PMD technology powers many commercial ToF cameras including the pmd[vision] series and is used in smartphone face recognition systems (like iPhone's TrueDepth).

PMD vs Structured Light vs Stereo

Technology Method Best For
PMD / ToF Phase measurement of modulated light Real-time depth, mobile devices
Structured Light Projects patterns, analyzes distortion 3D scanning, static scenes
Stereo Vision Two cameras, triangulation Outdoor/bright light, texture-rich scenes

6. Industrial & Precision Sensors

Industrial Laser Distance Sensors

Industrial-grade laser and ultrasonic sensors for precision measurement

For industrial applications requiring micrometer-level precision, specialized sensors are used:

Laser Displacement Sensors (Triangulation)

These high-precision sensors project a laser spot and use a CMOS/CCD array to detect the reflection position. Unlike ToF, they measure the angle of reflection for sub-micron accuracy.

  • Keyence LK-G Series: 0.01µm resolution, 1000mm range
  • Micro-Epsilon optoNCDT: Nanometer resolution for precision measurement

Confocal Chromatic Sensors

Use chromatic aberration of lenses to measure distance. Different wavelengths focus at different distances—the reflected wavelength indicates the exact distance.

Capacitive/Eddy Current Sensors

Non-optical solutions for extreme precision (nanometers) at very short ranges. Used in semiconductor manufacturing and precision positioning.

7. Choosing the Right Sensor

🎯 Quick Selection Guide

Application Recommended Sensor Why
Robot obstacle avoidance HC-SR04 or VL53L0X Affordable, good range
Liquid level sensing JSN-SR04T (waterproof) IP67 rated, non-contact
Gesture detection VL53L0X / VL53L1X Fast, precise, low power
Drone altitude VL53L1X or TFmini Long range, lightweight
3D scanning Intel RealSense / ToF Camera Full depth image
Industrial measurement Laser triangulation Micron-level accuracy
Outdoor (bright light) Ultrasonic or VL53L1X Immune to ambient light

Key Factors to Consider

  1. Range: What minimum and maximum distances do you need?
  2. Accuracy: Is ±1cm acceptable or do you need sub-mm precision?
  3. Speed: How fast do you need readings? (Hz)
  4. Environment: Indoor/outdoor? Temperature extremes? Dust/moisture?
  5. Power: Battery-powered devices need low-power options
  6. Interface: I2C, SPI, UART, or analog?
  7. Cost: Budget for your project scale

8. Summary

Sensor Type Range Accuracy Cost Best Use
Ultrasonic (HC-SR04) 2-400cm ±3mm $1-5 Robotics, parking sensors
IR Triangulation (Sharp) 10-150cm ±5% $8-15 Mid-range detection
Laser ToF (VL53L0X) 5-120cm ±3% $5-10 Precision, gesture, drones
Long Range ToF (VL53L1X) 4-400cm ±3% $10-15 Lidar, robotics
ToF Camera 10cm-10m ±1-2% $100-500 3D imaging, AR/VR
Industrial Laser 0.1-1000mm ±0.01µm $500+ Precision manufacturing

🚀 Key Takeaways

  • Ultrasonic sensors are affordable and work in any lighting, but have wide beam angles
  • IR triangulation offers good accuracy but is affected by ambient light
  • ToF sensors (VL53L0X/L1X) provide the best balance of accuracy, size, and cost
  • ToF cameras capture full depth images for 3D applications
  • PMD technology enables high-speed, accurate depth sensing in compact form factors
  • Choose your sensor based on range, accuracy, environment, and budget requirements