Robot Logic in Scratch
Senior 3 · ICT
Robot Logic in Scratch
Lesson 4 of 5
60 min
Robot vacuum cleaners, warehouse carts and planetary rovers share one basic skill: they keep moving through a space full of obstacles without crashing. An obstacle-avoiding robot is the classic first project in robotics because it uses the whole sense–think–act loop with a single sensor. In this lesson you design the algorithm, simulate it in Scratch with a forward-looking sensor, test it, and see how the same logic runs on real hardware.
Definition
Algorithm: a precise, step-by-step set of instructions for solving a problem.
Flowchart: a diagram of an algorithm, using ovals for start and end, rectangles for actions, diamonds for decisions and arrows for the order.
Detection range: the distance at which the sensor first notices an obstacle.
Reaction distance: how far the robot travels between a sensor reading and the end of its response (stopping or turning).
Flowchart drawn by Ganzaa.
Why a random turn? If the robot always turned exactly 90° right, it could bounce between the same two walls forever, or circle one corner. A random angle sends it in new directions, so it explores the whole space.
The distance to block from the last lesson sees in every direction. A real ultrasonic sensor looks only forward. To imitate it, draw a thin cyan sensor beam sticking out in front of the robot in its costume, 40 pixels long. Draw the arena walls and obstacles on the backdrop in brown. Then color [cyan] is touching [brown]? is true only when something is within 40 pixels ahead: a detection range of 40.
Diagram drawn by Ganzaa. Keep the beam narrow and the same colour everywhere; nothing else on the stage may be cyan.
Block diagram drawn by Ganzaa in Scratch 3 colours.
| Algorithm step | Blocks |
|---|---|
| Start at the entrance | go to x: (−180) y: (−120); point in direction (90) |
| Read the sensor | <color [cyan] is touching [brown]?>, inside the if |
| Obstacle: back off and turn randomly | move (−5) steps; turn right (pick random (90) to (150)) degrees |
| Count the turns | change [turns] by (1) |
| Clear: move forward | move (3) steps |
| Repeat | forever |
Worked example 1: will it stop in time?
The robot's front is 100 pixels from a wall and it moves 3 pixels per frame. Its beam reaches 40 pixels ahead, so it detects the wall when its front is 40 pixels away, after travelling pixels:
It then backs off 5 pixels and turns, never touching the wall. Now suppose a pupil sets the speed to 50 pixels per frame “to make it faster”. In one frame the beam tip can pass from in front of a thin wall to beyond it, and the robot itself can end up inside the wall. A safe rule for the simulation: the step must be much smaller than the beam length.
Worked example 2: choosing a threshold for a real robot
A real robot drives at 0.3 m/s. Its ultrasonic sensor gives 10 readings per second, and its motors need 0.2 s to stop.
The threshold must be more than 0.09 m. Sensors are not perfect, so engineers add a safety margin: a threshold of 0.15 m (15 cm) is a sensible choice. If the robot is made twice as fast, the reaction distance doubles to 0.18 m and the threshold must grow too.
| Job | Simulation | Real robot (for example EV3 or a micro:bit buggy) |
|---|---|---|
| Sense | <color [cyan] is touching [brown]?> | Ultrasonic sensor: (distance) < (15) |
| Back off | move (−5) steps | Both motors backwards for a short time |
| Turn | turn right (pick random (90) to (150)) degrees | One motor forwards and the other backwards for a random time |
| Move forward | move (3) steps | Both motors forwards |
A wheeled robot turns by driving its two wheels differently: if the left wheel goes forward and the right one backward, it spins to the right on the spot.
Warning
Why the simulated robot gets stuck, and fixes
Blue Butterfly: a 3D animation of how an ultrasonic distance sensor works, the sensor most obstacle-avoiding robots use. Watch how the echo time gives the distance.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.