Primers
Time-of-flight sensors
A time-of-flight sensor measures distance with light. It sends out a short pulse of invisible infrared light, waits for the reflection, and times how long the light took to get there and back. Light covers about 30 cm in a nanosecond, so the timing has to be very fine: a centimetre further away is about 67 picoseconds longer there and back. Small ones, like STMicroelectronics’ VL53 family, do all of that on a chip a few millimetres across and report a distance in millimetres.
Pick your level at the top of the page.
High school
Timing an echo
Shout at a cliff and count until the echo comes back: the longer it takes, the further the cliff. A time-of-flight sensor does the same with light. It flashes, waits for the reflection, and works out the distance from the time:
distance = speed of light × time ÷ 2
Halved, because the light goes there and back.
Why it has to be so quick
Sound travels about 343 metres a second, so an echo from 1 m away takes about 6 thousandths of a second. Light is nearly a million times faster: from 1 m away it’s back in under 7 billionths of a second. The sensor’s clock has to tell apart times that differ by trillionths of a second to measure to the centimetre.
Light you can’t see
The flash is infrared, a colour just past red that eyes can’t see, from a tiny laser that’s weak enough to be safe to look at.
It sees a cone, not a line
The sensor doesn’t measure one point. It sees a cone that widens with distance, like a torch beam, and reports one distance for everything in it. Point one at a chair leg in front of a wall, and the answer might be the leg, the wall or somewhere between, depending on which sends more light back.
What fools it
- Very dark surfaces send back little light, so they’re seen only up close.
- Mirrors and shiny surfaces bounce the light away, unless you point straight at them.
- Glass lets most of the light through.
- Sunlight is full of infrared, which drowns out the sensor’s own flash.
Check yourself
- A reflection comes back after 20 nanoseconds. About how far away is the object? (Light: 30 cm a nanosecond.)
- Why does the formula divide by 2?
- A sensor reads 4 m to a white wall but nothing from a black sofa at 2 m. Why?
<details><summary>Answers</summary>
- 30 cm × 20 = 6 m there and back, so about 3 m.
- The time is for the trip there and back. The object is half that far.
- The black sofa reflects much less of the light, so not enough comes back to time.
</details>
College
Inside the chip
The light comes from a VCSEL, a laser that emits from its surface, at 940 nm, outside what eyes can see and weak enough to be eye-safe (Class 1). The receiver is a grid of SPADs, single-photon avalanche diodes: detectors so sensitive that one photon sets off a measurable pulse.
Direct and indirect
- Direct time-of-flight (dToF) times the pulses, as above. Small sensors like the VL53L1X and VL53L5CX work this way.
- Indirect time-of-flight (iToF) sends light that’s switched on and off at a steady frequency f and measures how far the returning wave has shifted, its phase φ. Distance is c × φ ÷ (4π f). Many depth cameras work this way. The catch: a phase of 360° looks the same as 0°, so beyond c ÷ (2f) the distance wraps around. At 20 MHz that’s 7.5 m.
Field of view
The cone’s width at distance d, for a field of view θ, is 2 d tan(θ ÷ 2). The VL53L1X’s is about 27°, so at a metre it sees a patch about 48 cm wide.
Two ways to see more than one distance:
- Regions of interest. The VL53L1X can be told to use only part of its 16 × 16 detectors, down to a 4 × 4 corner, so it looks in a narrower cone. Moving that region between readings lets one sensor look in several directions in turn.
- Zones. The VL53L5CX splits its view, about 45° square, into a 4 × 4 or 8 × 8 grid and reports a distance for every zone at once, 16 or 64 numbers per reading, like a very low-resolution depth camera. 4 × 4 runs at up to 60 readings a second, 8 × 8 at up to 15.
Range against speed
The sensor fires many pulses for each reading and adds up what comes back. The time it spends is the timing budget. A longer budget collects more light: it sees further and reads steadier, but gives fewer readings a second. The VL53L1X goes from 20 ms (50 readings a second) up to a second. Its distance modes trade the other way: short mode reaches about 1.3 m but copes better with bright light, long mode about 4 m in the dark.
Two on one bus
Both sensors talk over I²C (see the ESP32 primer), and both answer at address 0x29. Two on one bus
need sorting out at power-on: hold one off with its shutdown pin (XSHUT), give the other a new address, then let the
first one start. The new address is forgotten when the power goes, so this happens at every boot.
Behind a window
A sensor behind a cover window sees some of its own light bounce straight back off the window. That’s crosstalk: it pulls readings shorter. It’s measured once with nothing in front and subtracted, and it changes if the window gets dirty or scratched.
Check yourself
- How wide is the VL53L1X’s patch at 2 m?
- An iToF camera runs at 50 MHz. How far can it see before the distance wraps around, and what does an object at 4 m read as?
- You need 50 readings a second. What’s the longest timing budget you can use?
- Two sensors on one bus both answer at
0x29. Write the start-up steps.
<details><summary>Answers</summary>
- 2 × 2 m × tan(13.5°) ≈ 0.96 m.
- c ÷ (2 × 50 MHz) = 3 m. An object at 4 m reads as 1 m.
- 1 s ÷ 50 = 20 ms.
- Hold sensor B’s XSHUT low. Start sensor A and give it a new address, say
0x30. Release B’s XSHUT: it starts at0x29. Do this at every power-on.
</details>
University
Counting photons
A single pulse rarely brings back more than a photon or two, and some pulses bring back none. So a direct time-of-flight sensor fires thousands of pulses and builds a histogram: for each short slice of time after the pulse, how many photons arrived then. This is time-correlated single-photon counting.
The return from the object piles up in one place in the histogram. Ambient light arrives at random times, so it spreads evenly across every slice, as a floor. The distance is the peak’s position. Its centre can be found to a fraction of a slice by interpolating between neighbours, so a sensor with slices a few centimetres long still reports millimetres.
Signal and noise
Photon counts are random, Poisson distributed: a slice that expects N photons gets about N ± √N. The peak has to stand clear of the floor’s noise. With signal S and background B photons in the peak’s slices, the signal-to-noise ratio is about S ÷ √(S + B).
How S falls with distance depends on the target:
- A target that fills the cone (a wall) scatters light back evenly, and the share reaching the receiver falls as 1 ÷ d².
- A target smaller than the spot (a thin pole) intercepts a share of the outgoing light that also falls as 1 ÷ d², so the return falls as 1 ÷ d⁴.
Both scale with the target’s reflectance. Doubling the pulses doubles S and B, so it raises the signal-to-noise ratio by √2.
Many targets, wrong targets
- Two surfaces in one cone (glass and the wall behind it, a chair leg and the wall) make two peaks. A sensor that reports one distance picks one. A sensor that reports several targets per zone, as the VL53L5CX can, gives both.
- Multipath: light that bounces off the floor and then the object arrives late and smears the peak long.
- Pile-up: a SPAD that has just fired is blind for a few nanoseconds. In bright light the early photons in each pulse are counted and later ones missed, which skews the histogram towards shorter distances.
- Interference: two sensors facing each other can count each other’s pulses.
This is why each reading comes with a status: whether the peak was clear enough, the signal too weak, the spread too wide. A reading with a bad status isn’t a distance.
Check yourself
- Outdoors, the background is far larger than the signal. Moving a white wall from 1 m to 2 m away, how many times more pulses do you need for the same signal-to-noise ratio?
- Indoors, in the dark, the background is near zero. Same question.
- A sensor reads a thin pole at 1 m but loses it at 1.5 m, while it sees a wall at 3 m. Why?
- Why can a reading built from too few pulses be wrong, not just missing?
<details><summary>Answers</summary>
- S falls to a quarter. With B dominant the ratio is about S ÷ √B, and both grow with the pulse count n, so it grows as √n. A quarter needs √n four times larger: 16 times the pulses.
- With B near zero the ratio is about √S, so a quarter of the signal halves it. Four times the pulses bring it back.
- The pole is smaller than the spot, so its return falls as 1 ÷ d⁴: at 1.5 m it’s about a fifth of what it was at 1 m. The wall’s falls as 1 ÷ d², and it fills the cone.
- With few pulses, the background in each slice is a few photons or none, and a handful of random photons landing together can look like a peak.
</details>
Try it
No sensor needed: a toy one in Python, about 70 lines. It fires pulses at a wall, counts the photons coming back with ambient light mixed in, and finds the peak. The numbers are made up to behave like a small sensor, not to match any one. You need only a container runtime. Start a shell with Python in it:
container
container run --rm -it docker.io/library/python:3.14-alpine sh
docker
docker run --rm -it python:3.14-alpine sh
podman
podman run --rm -it docker.io/library/python:3.14-alpine sh
Inside it, save the toy sensor and run it:
cat > tof.py <<'EOF'
"""A toy direct time-of-flight sensor: fire many laser pulses, count when photons come back, find the peak.
The numbers are made up to behave like a small sensor, not to match any one. Change them and run it again.
"""
import math
import random
C = 0.29979 # metres light travels in a nanosecond
BIN = 0.25 # ns per histogram bin
BINS = 160 # a 40 ns window: up to about 6 m and back
PULSES = 2000 # pulses per reading
JITTER = 0.2 # ns of timing spread on each photon
SIGNAL = 0.5 # chance a pulse brings back a photon from a white wall 1 m away
def poisson(mean):
# how many random events happen, given how many to expect on average
n, p, limit = 0, 1.0, math.exp(-mean)
while True:
p *= random.random()
if p < limit:
return n
n += 1
def histogram(distance_m, reflectance, ambient):
"""Count photon arrivals over many pulses. ambient: background photons per pulse, over the whole window."""
hist = [0] * BINS
back = 2 * distance_m / C # ns there and back
p_signal = min(1.0, SIGNAL * reflectance / distance_m ** 2) # fainter with distance squared
for _ in range(PULSES):
if random.random() < p_signal:
b = int(random.gauss(back, JITTER) / BIN)
if 0 <= b < BINS:
hist[b] += 1
for _ in range(poisson(ambient)):
hist[random.randrange(BINS)] += 1
return hist
def reading(hist):
"""The distance, or None if the peak doesn't stand clear of the background."""
background = sorted(hist)[BINS // 2] # the median bin: mostly ambient
peak = max(range(1, BINS - 1), key=lambda i: hist[i - 1] + hist[i] + hist[i + 1])
window = range(peak - 1, peak + 2)
counts = sum(hist[i] for i in window)
above = counts - 3 * background
if above < 5 * math.sqrt(3 * background + 1): # not clearly more than the noise
return None
centre = sum((i + 0.5) * (hist[i] - background) for i in window) / above
return centre * BIN * C / 2
def show(hist, width=60):
rows = [sum(hist[i:i + 4]) for i in range(0, 80, 4)] # to 3 m, 4 bins a row: 1 ns, 15 cm
for r, n in enumerate(rows):
print(f"{r * 4 * BIN * C / 2:5.2f} m |{'#' * round(n / max(rows) * width)}")
random.seed(1)
print("One reading of a white wall 1.5 m away, indoors. Each row is a range of distances; the bar is photons counted.")
show(histogram(1.5, 0.9, ambient=0.5))
print()
print("distance white, indoors dark, indoors white, sunlight dark, sunlight")
for d in (0.2, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0):
cells = []
for refl, amb in ((0.9, 0.5), (0.05, 0.5), (0.9, 20), (0.05, 20)):
r = reading(histogram(d, refl, amb))
cells.append("nothing" if r is None else f"{r:.3f} m")
print(f"{d:5.1f} m " + "".join(f"{c:>15}" for c in cells))
EOF
python tof.py
It shows one reading’s histogram, the wall’s peak standing over the even floor of ambient light:
0.00 m |#########
0.15 m |########
0.30 m |#########
0.45 m |########
0.60 m |#######
0.75 m |########
0.90 m |########
1.05 m |#######
1.20 m |########
1.35 m |###########################################################
1.50 m |############################################################
1.65 m |#####
1.80 m |#######
1.95 m |##########
2.10 m |##########
2.25 m |########
2.40 m |########
2.55 m |########
2.70 m |#####
2.85 m |##########
Then a table of readings: a white wall and a dark one, indoors and in sunlight, from 20 cm to 5 m:
distance white, indoors dark, indoors white, sunlight dark, sunlight
0.2 m 0.202 m 0.202 m 0.202 m 0.202 m
0.5 m 0.502 m 0.500 m 0.502 m 0.501 m
1.0 m 0.999 m 1.002 m 0.997 m nothing
2.0 m 2.002 m nothing 1.998 m nothing
3.0 m 3.007 m nothing nothing nothing
4.0 m 3.994 m nothing nothing nothing
5.0 m 5.002 m nothing nothing nothing
The white wall indoors is found all the way to 5 m. The dark one is lost after a metre. Sunlight’s floor of photons hides the white wall past 2 m and the dark one past 50 cm.
Things to change, with vi tof.py, then run it again:
PULSES = 200, a tenth of the timing budget. Readings go missing sooner, and some come back wrong: with so few photons in each slice, a handful of random ones can look like a peak.ambient=20in theshow(...)line, the one-reading histogram in sunlight. The floor rises to nearly the peak’s height.
Type exit to leave.
Learn more
- ST’s pages for the VL53L1X and the VL53L5CX, with their datasheets.
- Arduino libraries: Pololu’s for the VL53L1X, and ST’s for the VL53L5CX.
- Wikipedia on time-of-flight cameras, direct and indirect, and on single-photon avalanche diodes.