Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Sensors and Perception

Distance Sensors: Ultrasonic, IR and Lidar

How ultrasonic, infrared, time-of-flight and lidar sensors measure distance, their limits, and how to turn a lidar scan into obstacle points in Python.

Knowing how far away things are is what lets a robot avoid walls, follow a corridor and build a map. Four technologies cover most needs: ultrasonic, infrared (IR), time-of-flight (ToF) and lidar. They differ in range, cost, speed and in the ways they fail. This lesson explains how each one works, shows the arithmetic behind them, and turns a lidar scan into points in the robot's frame.

Ultrasonic sensors

An ultrasonic sensor sends a short burst of sound (about 40 kHz, above human hearing) and measures how long the echo takes to return. Sound travels at roughly 343 m/s in air at 20 degrees C, and the pulse goes to the object and back, so

distance = speed_of_sound ร— time / 2

Typical hobby sensors measure 2 cm to 4 m with a beam about 15-30 degrees wide. They are cheap and work on any surface, including glass. Weak points:

  • Wide beam: you know something is in the cone but not where.
  • Soft or angled surfaces absorb or deflect the sound, so the echo never returns.
  • Temperature: the speed of sound changes about 0.6 m/s per degree, so errors reach a percent or more outdoors.
  • Crosstalk: one sensor hears another's pulse; fire them in turns.
  • Slow update rate (tens of Hz), because the robot must wait for the echo.
Python Echo time to distance, with temperature correction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Infrared and time-of-flight sensors

IR proximity sensors shine an infrared LED and measure the angle or strength of the reflection. They are fast and narrow, but the output is non-linear (it often follows roughly 1 / distance), and it depends on the surface color and ambient light. Typical range is 10 to 80 cm.

Time-of-flight (ToF) sensors emit short laser pulses and measure the light's travel time directly. Because light moves at 3ร—10โธ m/s, a 1 m distance takes only about 6.7 ns round trip, so the electronics are clever, but small chips now do millimeter-resolution ranging at 30-60 Hz with a narrow beam. Strong sunlight and very dark or shiny surfaces still cause errors.

Python Linearizing an IR sensor with a lookup table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The curve flattens at long range, so small voltage noise becomes a large distance error far away. IR sensors are best used up close.

Lidar

Lidar (light detection and ranging) is a ToF laser on a rotating mirror or a spinning head. It returns a scan: a list of distances at known angles, for example 360 readings per revolution at 5-20 Hz. A 2D lidar sweeps one plane (the standard for indoor mobile robots); 3D lidars stack many beams and produce a point cloud, used by self-driving cars. Lidar gives accurate, dense, long-range data (up to tens or hundreds of meters). It is more costly and can be fooled by glass, mirrors and black surfaces, and rain or dust.

flowchart LR
    A["Laser pulse"] --> B["Hits object"]
    B --> C["Reflection returns"]
    C --> D["Timer: t"]
    D --> E["distance = c * t / 2"]
    E --> F["Scan: (angle, distance) pairs"]

A scan is a polar list; to use it with maps you convert it into x, y points.

From a scan to points

Each scan reading is (angle_i, range_i) in the sensor frame. The point is

x = range cos(angle), y = range sin(angle)

and then the sensor's pose on the robot (and the robot's pose in the world) is applied with a homogeneous transform. Invalid readings (no return, shown as infinity or maximum range) must be dropped first.

Python Simulate a lidar scan and plot the obstacle points

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Using range data: reactive obstacle avoidance

Even without a map, range sensors support useful behavior. A common idea: split the scan into left, front and right sectors, take the minimum range in each, and steer toward the freest side while slowing down when the front is near.

Python Choose a steering command from a scan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This simple scheme is the starting point for the controllers in Mobile Robot Control.

Comparison

Sensor Typical range Beam Strengths Weaknesses
Ultrasonic 0.02-4 m wide cheap, works on glass slow, imprecise direction
IR proximity 0.1-0.8 m narrow fast, small non-linear, surface and light dependent
ToF 0.03-4 m narrow accurate, small sunlight, shiny surfaces
2D lidar 0.1-30 m thin plane dense, accurate cost, glass, only one plane
3D lidar up to 100+ m volume 3D structure cost, data volume

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. An ultrasonic echo returns after 10 ms. The speed of sound is 343 m/s. How far is the object?

    distance = 343 * 0.010 / 2 = 1.715 m; the sound travels there and back.
  2. 2. Why are ultrasonic sensors poor at telling exactly where an obstacle is?

    The wide cone means direction is uncertain.
  3. 3. What must you do with lidar readings of 'no return' before converting to points?

    Invalid beams (infinite or maximum range) would create false obstacles if treated as real.
  4. 4. Which conversion turns a lidar reading (angle, range) into sensor-frame coordinates?

    Polar to Cartesian: x = r cos(a), y = r sin(a).

Summary

  • Ultrasonic sensors time a sound echo; ToF and lidar time a light pulse. Distance is speed times time divided by two.
  • Each technology has typical failure modes: wide beams, angled or soft surfaces, sunlight, glass.
  • A lidar scan is a list of (angle, range) pairs; convert to points and transform into the world frame.
  • Even a simple sector-based rule makes a robot avoid obstacles.

Next: Inertial Sensors (IMU)