EV3 RoboticsComponent tutorialsThe Ultrasonic Sensor

Sensing · Level 1 · 6 min

The Ultrasonic Sensor

Measure how far away something is.

ComponentSensing6 min

The Ultrasonic Sensor

The Ultrasonic Sensor measures distance. It sends out a burst of sound too high for people to hear, listens for the echo, and works out how far away the surface is from how long the echo took — exactly how a bat finds a moth, and how a submarine uses sonar.

EV3 Ultrasonic Sensor from the front, showing its two round openingsfront
EV3 Ultrasonic Sensor from the sideside
The two round openings on the front are the point of this sensor: one sends the burst of sound out, the other listens for the echo coming back.

Blocks reference

BlockWhat it does
([4 v] distance in [cm v] :: sensors)
Reports how far away the nearest thing in front of the sensor is, as a number in centimetres.
wait until <([4 v] distance in [cm v] :: sensors) < (15)>
Holds the program until something comes closer than 15 cm.

A number, not a yes or no

This is the important step up from the Touch Sensor. Touch gives you true or false; the Ultrasonic gives you a number, and the deciding is left to you. Pick a threshold below and watch where the robot ends up.

when program starts
start moving straight: 0
4 wait until distance < 15 cm
stop moving
60cm · reading15cm · threshold
Nothing is close. The sensor reports about 60 cm and the program waits.
stopped

The black line on the bar is the threshold; the blue fill is the reading. The robot stops the instant the fill crosses the line.

Three different robots, and only one number is different between them. That is what having a number rather than a yes-or-no buys you: the behaviour is tuned by editing one slot, not by rebuilding the program. It also means the sensor can never tell you it is “close” — close is a decision you make about a reading.

Why it matters

Car parking sensors, automatic doors at a shopping centre, and the sensor that stops a lift door closing on somebody all work this way. Reacting before contact is what makes a machine feel safe.

If your set has an Infrared Sensor instead

The Home/Retail EV3 set (31313) ships an Infrared Sensor and a Beacon in place of the Ultrasonic and Gyro sensors. The Infrared Sensor also measures distance, so the programs in this module work with it — but it reports a rough 0–100 proximity rather than real centimetres, and it is affected by sunlight and by dark surfaces in ways the Ultrasonic is not.

EV3 Infrared Sensor from the frontIR Sensor
EV3 Infrared Beacon, a small remote controlBeacon
The Infrared Sensor and its Beacon, from the Home set. If your kit has these, expect proximity numbers rather than centimetres — and retune any threshold accordingly.

More sensing tutorials