Distance Sensor
An ultrasonic sensor that looks like a pair of eyes and measures the distance to objects ahead.
How it works, and range
It emits inaudible ultrasound and times the echo:
- Range 5-200 cm (official spec: 50-2000 mm, accuracy ±2 cm).
- Each "eye" has 4 LED segments you can light up from code (decoration / status display).
- When there's no target (too far, too close, sound-absorbing surface) it returns an invalid reading.
What it detects well
| Target | Result |
|---|---|
| Flat hard surfaces (walls, model sides) | ✅ accurate |
| Surfaces facing the sensor | ✅ accurate |
| Angled surfaces (> ~30°) | ⚠️ echo bounces away, may miss |
| Thin poles, grids, soft fabric | ❌ unreliable |
Word Blocks
(distance sensor [D] distance in cm) ← round reporter
<distance sensor [D] distance < [10] cm> ← condition
wait until <distance sensor [D] distance < [10] cm>Example 1: approach and slow down
Full speed toward the model, slow near it, avoid knocking it over:
when program starts
set movement motors [A+E]
set movement speed [80] %
start moving [forward]
wait until <distance sensor [D] distance < [20] cm>
set movement speed [25] %
start moving [forward]
wait until <distance sensor [D] distance < [6] cm>
stop movingExample 2: keep distance (follow)
forever
if <distance sensor [D] distance < [10] cm> then
start moving [backward]
else
if <distance sensor [D] distance > [15] cm> then
start moving [forward]
else
stop movingPython
python
import runloop, distance_sensor
from hub import port
async def main():
d = distance_sensor.distance(port.D) # millimeters; -1 when no target
if d != -1 and d < 100: # under 10 cm
pass
runloop.run(main())Python returns millimeters
Word Blocks show cm; Python's distance() returns mm and -1 when nothing is detected. Check for -1 first, otherwise "-1 < 100" is always true and the robot thinks it has arrived.
FLL uses
- Precise stops: more slip-proof than counting rotations, ideal for "stop 5 cm before the model".
- Crash guard: a parallel stack doing "distance < 5 cm → emergency stop" during long sprints.
- Localization: measure to a wall whose position you know, and you know where the robot is.
Common pitfalls
- The sound cone spreads: the floor up close or a neighboring model can be "seen". Mount the sensor a bit higher, not at mat level.
- Two robots' distance sensors can interfere (rare but real on back-to-back tables).
- Sampling has latency - leave braking distance at high speed.
Next sensor: Force Sensor.