Robot Logic in Scratch
Senior 3 · ICT
Robot Logic in Scratch
Lesson 3 of 5
60 min
Your simulated robot from the last lesson drives in a straight line until you stop it. It drives straight into walls because it cannot feel them. A robot is only as clever as its sensors. In this lesson you give it one sense at a time, learn what the matching real sensor measures, and write the Scratch logic that reacts to it.
Definition
Digital reading: a sensor value with only two states, such as pressed or not pressed (true or false).
Analogue reading: a sensor value that can take many values in a range, such as a distance of 0 to 255 cm or a loudness of 0 to 100.
Threshold: the value at which a program changes its decision, such as “closer than 60”.
Calibration: measuring a sensor's readings in known conditions (for example on black and on white) to choose good thresholds.
| Sensor | Measures | Real device | Reading | Simulation in Scratch |
|---|---|---|---|---|
| Touch | Contact with an object | Push switch on a bumper | Digital | <touching [Wall]?> |
| Colour (light) | Colour or brightness of a surface | Colour sensor or light-dependent resistor, facing the floor | Analogue or a colour | <color [green] is touching [black]?>, where the green spot is part of the robot's costume |
| Distance | How far away an object is | Ultrasonic sensor (echo of sound) or infrared sensor | Analogue | (distance to [Box]) |
| Temperature | How hot or cold | Thermistor or temperature chip | Analogue | A variable called temperature shown as a slider |
| Sound | Loudness | Microphone | Analogue | (loudness): uses the computer's real microphone |
Diagram drawn by Ganzaa. Draw the robot's costume like this: Scratch can test whether one colour of the costume is touching a colour on the stage.
Paint a Wall sprite. The robot drives until it touches the wall, backs off and turns. A real bumper is a switch that closes when pressed: a digital reading.
Paint a thick black line on the backdrop. A single colour sensor can follow the edge of the line: when the sensor is over black, steer away from the line (left); when it is over the floor, steer back (right). The robot wiggles along the edge, with the line on its right, where the sensor is.
An ultrasonic sensor sends out a short pulse of sound, too high for people to hear, and times its echo. Sound travels about 343 metres per second in air at 20 °C, and the pulse goes there and back, so:
In Scratch, distance to [Box] measures from the robot's centre to the box's centre in any direction, so it “sees” behind too. That is good enough here; the next lesson builds a forward-looking sensor.

Worked example 1: the echo
An ultrasonic sensor hears the echo 2.9 milliseconds (0.0029 s) after sending the pulse. How far away is the wall?
The other way round: for a box 1.2 m away, the echo takes s, that is about 7.0 ms. Forgetting to divide by 2 is the classic mistake: it would put the wall at 1.0 m, twice as far as it really is.
Scratch has no thermometer, so a slider stands in for one: make a variable temperature, show its monitor, right-click it → slider, and set the range to 0–50 (right-click → change slider range). Dragging the slider imitates the room warming up. Give the robot (or a fan sprite) two costumes, fan on and fan off:
The loudness block in Sensing reads the computer's microphone (Scratch asks permission the first time). It reports a value from 0 to 100. A clap makes the robot turn back:
Block diagrams drawn by Ganzaa in Scratch 3 colours.
Worked example 2: calibrating a light sensor
A real robot's light sensor reads 8 on the black line and 72 on the white floor. A good threshold is halfway:
The rule becomes: if the reading is less than 40, the sensor is over the line. Later the classroom gets sunnier: black now reads 20 and white 90, so the best threshold is . With the old threshold of 40, the edge of the line, which reads about 50 in sunlight, would be mistaken for the floor. That is why robots are calibrated in the room where they will run.
Warning
Common simulation mistakes
First_Coding: a line-following robot in Scratch, using a colour in the robot's costume as its sensor.
Use your Robot_Sim project. Work through the five sensors; save a copy after each.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.