Challenge 1
Pick up three objects of different sizes, one after another. Do not change a single block between them. Show the degrees counted for each one on the screen. Then put the three in size order. Use only the numbers.
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 29
Level 2 · Lesson 29 · EV3-L02-2960 minutes · Ages 8+ · Model: Apple Picker
The Apple Picker: a long arm with a gripper on the end. A gripper is a hand that grabs. A switch is hidden inside it. The switch closes when there is something in there.
A Medium Motor closes the gripper, a little at a time. When it holds an apple well, the apple itself presses the switch. That is how the machine knows.
By the end, your gripper will close on its own. It will stop as soon as it has the apple. Not after a time you guessed. Not after a number of turns you counted. It stops because it has the apple.
Most fruit is still picked by hand. People stand on ladders and pick one apple at a time. A picker does not just squeeze for two seconds and hope. They squeeze until they feel the apple. Then they twist.

Every apple is a different size. Each one hangs at a different angle. What if a machine always closed its gripper the same amount? It would squash the big ones and drop the small ones. The apple has to be the thing that says stop. Nobody can know in advance how big the apple will be.
Farmers care a lot about this. A bruised apple gets turned into juice. It sells for much less than a good apple. So a gripper that squeezes a bit too hard turns a good crop into a cheap one.
A picker with no sense of touch has only two choices. Too soft, and the fruit falls. Too hard, and the fruit is spoiled. Neither one gets the apple to the box.
If you cannot know the answer before you start, let the job tell you when it is done.
You know two loops well. forever never stops. repeat (10) stops after a number you chose when you wrote the program. The Dancing Robot, in Lesson 23, used a third: repeat until. There it waited for the timer. Today it stops when something in the world tells it to.
repeat until <[1 v] is pressed? :: sensors> [A v] run for (15) [degrees v] at (25) % speed :: motors end
Read it in the order the Brick runs it. First it checks. Then it runs the inside once. Then it checks again. The loop ends between steps, never in the middle of one.
In Level 1 you used [1 v] wait until [pressed v]. That also ends on the switch. But put it on the Apple Picker, and it will sit there all lesson.
| Block | While it waits for the switch, the motor is… |
|---|---|
[1 v] wait until [pressed v] :: sensors | doing nothing. The gripper never closes. So the apple is never held, and the switch is never pressed. It waits for ever. |
repeat until <[1 v] is pressed? :: sensors> [A v] run for (15) [degrees v] at (25) % speed :: motors end | closing, one step at a time. The loop itself makes the switch get pressed. It ends because of what it did. |
Waiting is not trying. Use wait until when something else will press the switch. That could be a person, a falling part, or another stack. Use repeat until when this program has to make it happen.
A step is how far the loop moves before it checks again. It decides how far the gripper goes too far. Fifteen degrees is a squeeze you can only just see. Three hundred and sixty degrees would slam shut before the loop could look at the switch again.
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.
The Touch Sensor is the simplest input the EV3 has: a button that is either pressed or not. That sounds trivial, but it is how a robot knows it has hit a wall, reached the end of a track, or been told to start by a person.
| Block | What it does |
|---|---|
wait until <[1 v] is pressed? :: sensors> | Holds the program here until somebody presses the sensor. |
<[1 v] is pressed? :: sensors> | Reports true or false. Drop it into a condition to make a decision rather than a wait. |
[1 v] when [bumped v] :: events hat | Starts a whole stack of its own. The dropdown chooses the moment: pressed, released or bumped. |
A button is not only “pressed”. One press is three things: the moment it goes down, the time it stays down, and the moment it comes back up. Watch what a single press does to three programs at once.
versus two hat blocks
The middle counter is the one that surprises people. Nothing is wrong with it — a loop really does check that fast, and every check really is a separate answer.
Nothing there is broken. A loop really does get round hundreds of times a second, and each time it asks is pressed? the honest answer is still yes — so if that loop plays a sound or counts something, it does it hundreds of times from one finger. The two hat blocks each fire once, and they fire at different moments: pressed the instant the button goes down, bumped only when it comes back up.
The three options, and what each is for:
when program starts :: events hat set movement motors to [B v] and [C v] :: movement start moving [straight: 0] :: movement wait until <[1 v] is pressed? :: sensors> stop moving :: movement
The robot drives until something presses the sensor. Note that the movement is started unmeasured on purpose — the sensor decides when to stop, not a distance.
Touch sensors are everywhere in machines you cannot see into: a lift knows the doors are shut, a printer knows the lid is closed, a washing machine will not spin until it is latched. They are safety devices as much as inputs.
repeat until does something while it waits. And that something is what ends the wait.
The Touch Sensor is the simplest input the EV3 has: a button that is either pressed or not. That sounds trivial, but it is how a robot knows it has hit a wall, reached the end of a track, or been told to start by a person.
| Block | What it does |
|---|---|
wait until <[1 v] is pressed? :: sensors> | Holds the program here until somebody presses the sensor. |
<[1 v] is pressed? :: sensors> | Reports true or false. Drop it into a condition to make a decision rather than a wait. |
[1 v] when [bumped v] :: events hat | Starts a whole stack of its own. The dropdown chooses the moment: pressed, released or bumped. |
A button is not only “pressed”. One press is three things: the moment it goes down, the time it stays down, and the moment it comes back up. Watch what a single press does to three programs at once.
versus two hat blocks
The middle counter is the one that surprises people. Nothing is wrong with it — a loop really does check that fast, and every check really is a separate answer.
Nothing there is broken. A loop really does get round hundreds of times a second, and each time it asks is pressed? the honest answer is still yes — so if that loop plays a sound or counts something, it does it hundreds of times from one finger. The two hat blocks each fire once, and they fire at different moments: pressed the instant the button goes down, bumped only when it comes back up.
The three options, and what each is for:
when program starts :: events hat set movement motors to [B v] and [C v] :: movement start moving [straight: 0] :: movement wait until <[1 v] is pressed? :: sensors> stop moving :: movement
The robot drives until something presses the sensor. Note that the movement is started unmeasured on purpose — the sensor decides when to stop, not a distance.
Touch sensors are everywhere in machines you cannot see into: a lift knows the doors are shut, a printer knows the lid is closed, a washing machine will not spin until it is latched. They are safety devices as much as inputs.
Say this back before moving on: “Check, do a little, check again — and stop when the apple says so.”
Look at your model before you read on. Which electronic parts can you find? There are three. One is hidden on purpose.
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | The body of the picker. It runs the program. It shows what the gripper thinks it is holding. |
| Medium Motor | Moves the gripper. It is Medium, not Large, because this is a small, careful squeeze. It is not a heavy lift. |
| Touch Sensor — inside the gripper | The part that makes this lesson work. The apple presses it, not a person. That is unusual, and it is the whole idea. |
| The arm parts (not electronic) | They turn the motor’s turning into the gripper closing. If they are stiff, the loop will take many more steps. Check them by hand first. |
Steps 2 and 3 together are the real test. A switch that presses when the gripper is empty is a big problem. So is a switch that never presses. Both will look like a program mistake when you get to step 9.
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 |
|---|---|---|
| Gripper (Medium Motor) | A | A motor on its own, doing its own job, goes in A in this course. |
| Gripper switch (Touch Sensor) | 1 | A Touch Sensor always goes here in this course. It is also the port in the loop’s check. |
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.
Cable or Bluetooth are both fine. The picker stays on the desk. But keep the cable clear of the arm as it swings. This model has a long arm, and it hates getting caught on wires.
Stuck? The long version, with a photo of every screen, is in the Brick & Bluetooth guide.
Change one thing at a time. Say your guess out loud before each run. A wrong guess teaches you something. Just watching teaches you nothing.
repeat until for [1 v] wait until [pressed v] on its own, and run it. Nothing happens, for ever. Now you have seen the difference for yourself.Step 6 is worth thinking about. You added nothing to the machine. The motor was counting all along. But now the picker can tell you how big the apple was. A machine often knows more than it says.
A smaller step means a gentler grip and a slower pick. Choose on purpose.
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.
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.

The smallest program that picks an apple. Open, wait for a person to hold out the fruit, then close until it is held.
when program starts :: events hat clear display :: display write [READY ] at line (1) :: display [A v] run [counterclockwise v] for (1) [rotations v] :: motors [A v] reset degrees counted :: motors repeat until <[1 v] is pressed? :: sensors> [A v] run for (15) [degrees v] at (25) % speed :: motors end [A v] stop motor :: motors clear display :: display write [APPLE HELD] at line (1) :: display play sound [Communication / Hello v] until done :: sound
stop motor after the loop is extra safe. The last run for has already finished. But it makes the ending clear.What success looks like: the jaw opens and stops. You hold an apple-sized brick in it. The jaw closes in small steps you can see. As soon as it grips, it stops dead. The Brick says APPLE HELD.
If the gripper closes all the way and keeps trying, the apple is not reaching the switch. Go back to the hand check in step 5. Does it stop before it touches anything? Then the build itself is pressing the switch.
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.
Pick up three objects of different sizes, one after another. Do not change a single block between them. Show the degrees counted for each one on the screen. Then put the three in size order. Use only the numbers.
Make the picker let go as well as grip. Press the centre Brick button once, and the gripper closes on the object. Press it again, and the gripper opens all the way. Then it goes back to the ready position.
Grip a thing so soft that it cannot press the switch hard. Try a folded tissue or a foam block. Find the bite size and speed that hold it without squashing it. Prove it by picking it up five times in a row.
Build a sorting line. Put objects of three different sizes in the gripper, one at a time. The picker must grip each one. Then it must decide which of three size groups the object is in. It may only use the encoder count to decide. That is the number of degrees the motor turned. It shows the group on the screen. It plays a different sound for each group. Then it lets go. It must get all three right, with nobody telling it anything.