Home · Academy · Robotics & Coding · Sensors and Actuators · The Distance Sensor

The Distance Sensor

Learn to measure distance with an ultrasonic sensor and detect obstacles.

LESSON COMPASS

What will you use this page for?

Core idea

A distance sensor measures how far away an object is; it is one of the most important senses a robot has, letting it notice an obstacle and stop before crashing into it.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Never setting a threshold If you read the number from the sensor but do not attach it to any condition, the robot cannot make a decision. There must always be an "if it is closer than this" comparison. Measuring only once Distance changes all the time. If you measure only once at the start, the robot cannot see the…

Next connection

The Motion Sensor: We will meet the sensor that detects whether there is movement in a space and build rules such as "turn on the light when someone passes."

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Temperature Sensor
ContentStandard lesson · 1,565 words
Last updated

One-sentence summary

A distance sensor measures how far away an object is; it is one of the most important senses a robot has, letting it notice an obstacle and stop before crashing into it.

Why does it matter?

A robot has no eyes. It does not know whether a wall, a chair or an open path is in front of it. This is exactly where a distance sensor helps: it tells the robot "there is something ahead, and it is this close."

Without this information, a robot bumps into everything. With a distance sensor, the robot can stop, change direction or slow down. In robotics this is called obstacle avoidance, and it is a basic skill of almost every moving robot.

You already see this sensor in everyday life:

Behind a robot arm, a toy car or a line-following robot that stops "without thinking," there is very often a distance sensor.

How does an ultrasonic distance sensor work?

The most common distance sensor is the ultrasonic one. The name sounds complicated, but the idea is simple: it measures distance using sound waves.

Think like a bat

When bats fly in the dark, they make a sound. The sound bounces off an object and comes back. By measuring how long the sound takes to return, the bat works out how far away the object is. This is called echolocation.

An ultrasonic sensor does exactly the same thing:

  1. The sensor sends out a sound wave with a pitch too high for our ears to hear (that is why it is called "ultrasonic").
  2. The sound wave hits the object in front and bounces back.
  3. The sensor measures how long the sound took to go out and come back.
  4. Because sound travels through air at a steady speed, the sensor calculates the distance from that time.

From time to distance

Sound travels through air at about 340 metres per second. The sensor says, "I sent the sound out, and it came back after exactly this much time." A long time means the object is far away; a short time means it is close.

A library or a ready-made block usually does this calculation for us. Most of the time we simply read a number in centimetres. For example, when the sensor gives us distance = 25, it means "there is something 25 centimetres ahead."

Two everyday examples

Reading the sensor and detecting obstacles

Getting a number from the distance sensor is not enough on its own. The real job is to look at that number and make a decision. For this we use the condition structure we remember from the Algorithms lesson.

The basic idea is this: choose a threshold (a limit value). If the measured distance drops below this threshold, treat it as "there is an obstacle."

Pseudocode: stop when something gets close

Start
Repeat forever:
  distance = read the sensor
  If distance is less than 15 centimetres
    stop the motors
    turn on the red LED
  Otherwise
    move forward
    turn on the green LED
End

Here 15 centimetres is our threshold. The robot measures the distance at every moment. If something comes closer than 15 centimetres, it stops and gives a warning; if the path is clear, it keeps moving.

micro:bit-style example

The short example below behaves differently depending on the measured distance. In place of read_distance() you use the block or function that matches your sensor; the logic stays the same.

while True:
    distance = read_distance()   # in centimetres
    if distance < 15:
        stop_motors()            # obstacle is very close
        show("STOP")
    else:
        move_forward(speed=30)   # path is clear, move gently

A smarter example with two thresholds

Instead of stopping the robot in one step, it is smoother to slow it down first and then stop. We can do this with two thresholds.

distance = read the sensor
If distance is less than 10 centimetres
  stop
Otherwise if distance is less than 25 centimetres
  slow down
Otherwise
  move at normal speed

This is very close to the logic that lets real robots stop gently instead of crashing into an obstacle.

Mini practice

You can do this activity on paper, thinking it through, even without a sensor.

Goal: Let a tabletop robot move forward without falling off the table.

  1. Imagine you have fitted a distance sensor to the front of the robot.
  2. Choose a threshold value (for example, 12 centimetres).
  3. Fill in the blanks below with your own logic:
distance = read the sensor
If distance is less than ____ centimetres
  ____
Otherwise
  ____

Questions:

There is no single correct answer. The goal is to see how the threshold changes the robot's behaviour.

Common mistakes

Never setting a threshold

If you read the number from the sensor but do not attach it to any condition, the robot cannot make a decision. There must always be an "if it is closer than this" comparison.

Measuring only once

Distance changes all the time. If you measure only once at the start, the robot cannot see the changing environment. The measurement must happen again and again inside a loop.

Mounting the sensor facing the wrong way

An ultrasonic sensor measures wherever it is pointing. If it faces up or to the side, it cannot see the obstacle ahead. Make sure the sensor points in the direction you want to travel.

Forgetting very soft or very small objects

A sound wave may not bounce well off soft fabric or very thin objects. Some obstacles can be "invisible" to the sensor, so always test the robot while watching it.

Safety note

Lesson summary

Review questions

  1. What does an ultrasonic distance sensor use to measure distance?
  2. The sensor says "there is something 8 centimetres ahead." Our threshold is 15 centimetres and the rule is "if it is closer than the threshold, stop." What does the robot do?
  3. Why do we need to take the measurement again and again inside a loop?
  4. Why should we not power a motor directly from the board's pin?
  5. Why is it safer to start the robot at low speed on the first try?

Answers

  1. It uses sound waves. It measures how long its high-pitched sound takes to hit an object and bounce back, and calculates the distance from that time.
  2. The robot stops (and usually gives a warning), because 8 centimetres is closer than the 15-centimetre threshold.
  3. Because the environment and the position of obstacles change constantly. If we measure only once, the robot cannot see these changes and makes a wrong decision.
  4. Motors draw a lot of current; connecting one straight to a pin can damage the board. Instead, use a motor driver and a separate, proper power source.
  5. At low speed the robot has time to receive the sensor's "stop" command, reducing the risk of a crash. At high speed it may hit the obstacle before it can stop.

Source and verification note

For “The Distance Sensor”, verification focuses on whether the relationship between How does an ultrasonic distance sensor work? and From time to distance remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.

Next lesson

The Motion Sensor: We will meet the sensor that detects whether there is movement in a space and build rules such as "turn on the light when someone passes."

Start QuizBack to Sensors and Actuators
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.