Challenge 1
Walk towards a wall and stop with a gap of about 20 cm. Do it three runs in a row. Do not touch it once.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 45
Level 2 · Lesson 45 · EV3-L02-4560 minutes · Ages 8+ · Model: Mechanical King Kong
The Mechanical King Kong: a heavy body with two long arms worked by motors. Its head is an Ultrasonic Sensor. It has no wheels and no legs.
It moves by swinging its arms over. Each swing throws the body forward. The next swing catches it. With the right rhythm, it walks across a table. With the wrong rhythm, it just rocks on the spot.
By the end of the lesson, it will walk forward. Then it will stop before it hits the wall. That is harder than it sounds. A machine that is busy walking is not looking.
A gorilla walks on its knuckles. Its front arms hold its weight. Its body swings through under its shoulders. The whole animal rolls forward, and it hardly has to try.

Knuckle-walking works because of momentum. Once a body is moving, it wants to keep moving. So once the gorilla’s body swings forward, it keeps going by itself. The arms do not have to push all the way. The gorilla only has to start the swing.
Your model does the same. Watch it closely. The body keeps moving after the arms stop pushing. That is called coasting. Most of the distance comes from it.
The head is an Ultrasonic Sensor. It sends out a chirp. Then it listens for the echo and times how long it took. A bat does the same thing! Sound goes one metre in about three thousandths of a second. So the timing has to be very exact. And the sensor does it many times a second.
This heavy machine moves by throwing itself forward. It cannot stop the moment you ask. If it sees the wall too late, momentum carries it into the wall anyway.
The thing that makes it move also makes it hard to stop.
Here is a program that looks fine. But it does not work.
move [forward v] for (10) [rotations v] :: movement [4 v] wait until distance [< v] (25) [cm v] :: sensors stop moving :: movement
While a move for block is running, the program is stuck inside it. It is not checking sensors. It is not reading variables. It is not doing anything else. It waits for the move to finish. Only then does it look at the next block.
repeat until <([4 v] distance in [cm v] :: sensors) < (25)> move [forward v] for (1) [rotations v] :: movement end stop moving :: movement
The walk is not shorter. Ten steps of one rotation is still ten rotations. What changed? Now there are ten times when the program can look.
In Lesson 29, the Apple Picker closed its gripper fifteen degrees at a time. A bigger bite meant it squeezed harder before it noticed the switch. This is the same thing in a new place:
| Step size | Looks at the wall | Stops |
|---|---|---|
| ¼ rotation | very often | quickly, but the walk is jerky and slow |
| 1 rotation | often enough | within one swing. A good middle choice. |
| 5 rotations | not often | into the wall. It may not look again until it is too late |
The bigger each move, the longer the robot is blind. That is true of the gripper. It is true of this walker. It is true of every self-driving car on the road.
Lesson 15 gave you a second answer. start moving [straight: 0] does not wait. It starts the motors and lets the program go on straight away. So the very next block can watch the sensor while the machine is still moving:
start moving [straight: 0] :: movement [4 v] wait until distance [< v] (25) [cm v] :: sensors stop moving :: movement
Both are right. The step version suits a machine that moves in beats. The start-and-watch version suits a machine that rolls. Try both today. Decide which one your King Kong likes best.
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.
| Block | What 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. |
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.
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.
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.
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.
Machines repeat. A wiper sweeps, a conveyor runs, a ride goes round — and none of that should mean copying the same blocks over and over. A loop says “do this again” once.
| Block | What it does |
|---|---|
repeat (10) end | Runs the blocks inside a set number of times, then carries on below. |
forever end | Runs the blocks inside over and over, and never carries on below. |
repeat until <> end | Repeats until a condition becomes true — a loop with a sensor as its exit. |
Anything placed after a forever loop will never run. Not “runs late” — never. Both programs below end with the same block: set the status light green.
repeat (3)
forever
↑ this block never runs
Both programs contain the same green-light block. Let it run as long as you like — the right-hand ring will never turn green.
The repeat loop counts its three passes, stops, and moves on to the block underneath, so its light turns green. The forever loop reaches the bottom of its own blocks and jumps straight back to the top, so the block underneath is never reached — however long you leave it. If a program seems to stop half way through, look for a forever loop above the blocks that are not happening.
A program is a list, and the Brick works down it once. Every block runs, in order, and when the last one is done the program is over. That is fine for a list of instructions — drive, turn, beep, stop — because each is a thing you do once.
A sensor is not a thing you do once. Asking is 1 pressed? gives you an answer about this instant, and an instant later it may be wrong. Checking a sensor once tells you what the world was like at the moment the program started — which is almost never what you wanted to know.
So a program that has to react must ask again, and again, for as long as it is running. That is the whole job of the loop: not to repeat an action, but to keep the question being asked.
Wrap a sensor check and the motor it controls in a forever loop and you have built a closed-loop control system — the pattern behind every line follower, thermostat and cruise control:
It is called closed because the output feeds back round to the input: the motors move the robot, moving the robot changes what the sensor sees, and what the sensor sees changes the motors. Break the circle at any point and the robot stops responding.
when program starts :: events hat
forever
if <[1 v] is pressed? :: sensors> then
[A v] start motor [clockwise v] :: motors
else
[A v] stop motor :: motors
end
endRead it as a sentence and it is almost too simple to need explaining: for ever, if the button is pressed run the motor, otherwise stop it. The motor now follows the button for as long as the program is running.
This is the mistake nearly everybody makes first, and it is a hard one to spot because nothing about it looks wrong:
when program starts :: events hat if <[1 v] is pressed? :: sensors> then [A v] start motor [clockwise v] :: motors else [A v] stop motor :: motors end
The logic is perfect. The ports are right. Nothing is misspelled. And the robot will ignore the button completely — because the Brick reaches that if/else a few milliseconds after you press Run, finds the button not pressed, takes the else branch, stops the motor, runs out of blocks and ends. By the time a finger arrives, there is no program left to notice it.
with forever — a closed loop
without it — the common mistake
↑ running — for the only time
Both programs contain exactly the same if/else. The only difference is the forever block around one of them.
Both programs contain exactly the same if/else. The counter is what gives it away: one keeps checking for as long as it runs, the other is stuck on the single check it made before anybody touched anything. A student who has seen this once stops writing it.
The tell on a real robot is a program that ends the instant you start it — the Brick returns to its menu almost immediately. If a sensor program finishes rather than waits, the loop is what is missing.
A machine looks between its moves. Make the moves small, so it sees in time.
Everything an EV3 does begins with a motor turning. Before worrying about how far or how fast, there are only two things to say to a motor: start and stop. The blocks are the same whichever motor you use — only the port letter changes.
| Block | What it does |
|---|---|
[A v] start motor [clockwise v] :: motors | Starts the motor and immediately carries on to the next block. The motor keeps turning on its own. |
[A v] stop motor :: motors | Stops the motor. |
wait (2) seconds | Holds the program here, which is how you control how long a motor runs. |
Because start motor does not wait, a motor started on its own would run until the program ended. The pattern that gives a motor a length is three blocks:
when program starts :: events hat [A v] start motor [clockwise v] :: motors wait (2) seconds [A v] stop motor :: motors
Read it aloud: start it, leave it two seconds, stop it. Change the wait and you change how far the motor gets.
The highlight below follows the block the Brick is running, and the shaft turns only while the program is between start motor and stop motor. Notice that the count keeps climbing all the way through the wait — the wait does not pause the motor, it pauses the program.
0.00rotations so farnoshaft turning
The count does not pause when the program does. A wait holds up the blocks, and the motor carries on turning underneath it.
This is the whole idea behind start motor: it hands the motor its instruction and moves on, leaving the motor running behind it. Nothing stops the shaft until a block tells it to.
The direction dropdown says clockwise or counterclockwise — but clockwise seen from where? Always from the axle end: look straight at the shaft coming towards you, and clockwise is the way a clock’s hands go.
Here the Medium Motor faces you, so its output axle points straight out of the page. This is the view to picture when you are choosing a direction.
when program starts :: events hat [A v] start motor [clockwise v] :: motors wait (2) seconds [A v] stop motor :: motors
A Medium Motor normally lives in port A or D.
The Large Motor is shown from the side, which is the face its axle comes out of — so once again you are looking straight down the shaft.
when program starts :: events hat [B v] start motor [clockwise v] :: motors wait (2) seconds [B v] stop motor :: motors
A Large Motor normally lives in port B or C. Compare the two programs: they are the same three blocks, and only A has become B.
A fan, a conveyor belt or a spinning ride does not need to stop at an exact position — it just needs to run while something is happening. For those, timing the motor is simpler and perfectly good enough.
Say this back before you move on: “A long block is a long blind spot.”
Look at your model. Where is the sensor pointing? On this machine, that is a real question. The head moves with every step.
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | The body. It is most of the weight. The arms throw it forward. |
| Large Motor ×2 — the arms | They work as a matched pair, like the Stair Climber in Lesson 5. If they get out of step, the machine turns instead of walking. They are Large Motors because each swing throws the whole Brick forward. |
| Ultrasonic Sensor — the head | It has two holes. One sends a chirp. The other listens for the echo. Point it at the wall, and keep the holes clear. |
| The arms themselves (not electronic) | Long, so a small turn of the motor moves the body a long way. You get distance, but you lose force. It is the trade from Lesson 4 again. |
The Ultrasonic Sensor sits on a body that tips back and forth with every step. Tipped a little down, it reads the table. Tipped a little up, it reads the ceiling, or nothing at all.
Sensors go in ports 1, 2, 3, 4. Motors go in ports A, B, C, D. You cannot swap them. If you do, nothing warns you. The robot just will not work.
| Part | Port | Why this one |
|---|---|---|
| Left arm | B | B and C are the pair. These two do one job together. So they go on the ports the Movement blocks use. |
| Right arm | C | The other half of the pair. |
| Head (Ultrasonic Sensor) | 4 | The Ultrasonic Sensor’s usual home. Every check below uses this port. |
The manual puts the two arms on A and D, not B and C. This lesson uses B and C because they are a pair: two motors doing one job. That is what B and C mean all through this course. Move the two cables, or change set movement motors to [B v] and [C v] to say A and D. Both work. Pick one and stick to it.
The manual also says the Ultrasonic Sensor “can be connected or not”. On this model it is an extra. It is not in the instructions, so you fix it on yourself. This lesson needs it, so put it on before you start.
Check your own wiring now:
Two routes, and either is fine. USB is the reliable one and the one to fall back on when a room’s Bluetooth is busy; Bluetooth leaves the robot free to move, which some models need.
Do these in order. Naming the Brick after you go looking for it in the list is how groups end up driving each other’s robots.
EV3 until somebody changes it.EV3.The two failures, every class, every time. The Brick has gone to sleep while you were building — press the centre button to wake it. Or you have paired with the group at the next table, which is why the name matters.
The long version, including Port View and how to read the port tiles, is in the Brick & Bluetooth guide.
Use Bluetooth. This time you must. This machine moves around by throwing its weight about. A USB cable will stop it walking. Or it will pull the cable off the desk.
Stuck? The long version, with a photograph of every screen, is in the Brick & Bluetooth guide.
Change one thing at a time. Mark the wall’s place with tape. Each run, measure where the machine really ends up.
Steps 1 and 3 look the same, but they have different causes. The skill is telling them apart. A long step means it noticed late. A high speed means it could not stop after it noticed. One is a program problem. The other is physics.
Noticing in time and stopping in time are two different problems.

Build the model before you read any further. Everything after this is about making it do something, and none of it will make much sense with nothing on the table in front of you.
One page at a time — use Next and Previous, or the ← → keys. Fullscreen makes it big enough to build from.
The same build on Google Drive — sometimes a video, sometimes a scan:
Fullscreen it while you build — you can pause and scrub with the player's own controls.
Check the finished build against the picture before you switch anything on. A motor mounted the wrong way round is far easier to spot now than it is to debug later, when it looks like a program fault.
Walk forward, one step at a time, until the wall is close.
when program starts :: events hat set movement motors to [B v] and [C v] :: movement clear display :: display write [WALKING ] at line (1) :: display repeat until <([4 v] distance in [cm v] :: sensors) < (25)> move [forward v] for (1) [rotations v] at (40) % speed :: movement end stop moving :: movement clear display :: display write [OBSTACLE] at line (1) :: display play sound [Animals / Dog bark v] until done :: sound
set movement motors first — the arms are a pair, and this says which pair.stop moving after the loop — the last step has already finished. Put it in anyway. The coasting is the part you cannot see in the program.What success looks like: the machine stomps forward in clear steps. At about 25 cm from the book, it stops. It shows OBSTACLE and barks. It should not touch the book. But it may coast closer than you expect.
Does it stop straight away? The head is reading something close. Most often it is the table, or its own arm. Watch tile 4 while you rock the model by hand. Does it never stop? The wall is scattering the echo. Try a flat book, facing the sensor straight on.
Work through them in order — each is harder than the last. The extra challenge comes after all three, and it is meant to make you plan before you build.
Walk towards a wall and stop with a gap of about 20 cm. Do it three runs in a row. Do not touch it once.
After it stops, walk back from the wall. Use the same number of strides it took to get there. A stride is one step. You will have to count them.
Make it stop for anything that comes in front of it, not just the wall. Put your hand in front of it while it walks. It must stop within one stride.
Cross a table that has a wall somewhere on it. Do NOT tell the robot where. It must stop before the wall, wherever the wall is. Then it must work its way along the wall to one end. Before you write a single block, decide how it will tell "wall ahead" from "nothing ahead".