Challenge 1
Water three plants in a row and nothing in between. Set them out with clear gaps, run the robot along, and count: three bursts, three plants, nothing at the gaps and nothing at the end of the row.
EV3 Robotics›Level 2 · Intermediate›Lesson 44
Level 2 · Lesson 44 · EV3-L02-4460 minutes · Ages 9–16 · Model: EV3 Garden Robot
The Garden Robot: a machine that moves along a row of plants, finds each one with its Ultrasonic Sensor, and works its watering mechanism when it is in the right place.
Your first instinct will be “water when something is close”. Try it and the robot waters the plant, then keeps watering as it drives past, then waters the wall at the end of the row, then waters you.
“Close” is not one number. A plant is between about ten and thirty centimetres away. Nearer than that is the pot rim; further is the next row. What the robot needs is a band.
Automatic irrigation runs in parks, farms and golf courses — sprinklers on timers, drip lines along vegetable beds, and increasingly machines that decide for themselves where and how much to water.

Watering the wrong thing is not free. Water costs money, wet paths grow algae and become slippery, and a plant that gets somebody else’s share drowns while its neighbour goes thirsty. So the rules these systems follow are almost never one-sided.
A greenhouse controller does not water when the soil is dry — it waters when the soil is dry and the temperature is in a sensible band and it is not the middle of the day. Every one of those is a window with a floor and a ceiling.
A rule with only a ceiling fires at everything below it, including things that are far too close to be what you meant. Reverse the rule and it fires at the entire rest of the world. A threshold divides everything into two halves, and the thing you are looking for is almost never a whole half of everything.
Real things live in a range. If your rule has only one number in it, ask what is on the other side.
One and, two comparisons, one sensor. The result is a band of distances the robot cares about, and everything outside it is ignored.
<<([4 v] distance in [cm v]) > (10)> and <([4 v] distance in [cm v]) < (30)>> :: operators
Nothing here is a new block. It is Lesson 9’s distance reporter, Lesson 13’s comparison and Lesson 17’s and, put together into the shape sensing actually wants.
| Reading | > 10? | < 30? | In the window? | What it probably is |
|---|---|---|---|---|
| 4 cm | no | yes | no | The pot rim, or the robot’s own arm. |
| 18 cm | yes | yes | YES | A plant. Water it. |
| 55 cm | yes | no | no | The far wall, or nothing at all. |
| 250 cm | yes | no | no | Empty air — the sensor’s maximum. |
Look at the first and last rows. A one-sided rule of “closer than 30” would have watered the pot rim. A one-sided rule of “further than 10” would have watered empty air. Only the pair of them together describes a plant.
Measure, do not guess. Put a plant where it will really be and read the sensor. Then read it with the plant a little nearer and a little further, and pick your floor and ceiling comfortably outside those — a window that only just fits will miss plants that are slightly out of place, and every plant is slightly out of place.
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.
A threshold splits the world in two. A window picks something out of it.
A sensor that reports a number cannot be used to make a decision on its own — 23 is neither true nor false. An operator turns that number into an answer by comparing it with something.
| Block | What it does |
|---|---|
<(x) > (50)> | True when the left value is bigger than the right. |
<(x) < (50)> | True when it is smaller. |
<<> and <>> | True only when both conditions are true. |
<<> or <>> | True when at least one of them is. |
Forget the symbols for a moment. A comparison is a question about position on a number line: is x to the left of the other number, or to the right? Left is smaller, right is bigger — and that is the whole of it.
Drag the orange x and the black marker, and change the comparison. The green stretch is every position of x that would make the answer true — so you can see where the answer flips before you get there. Turn not on and watch the green jump to the other side.
Drag either marker, or use the arrow keys.
is x to the LEFT of it?
The lab above asks one question at a time: is this x true? A robot never has just one x, though — a sensor reading slides up and down all the time, so what really matters is which stretch of the line makes the condition true. This one draws the whole answer at once.
Drag the circle to move the number you are comparing against, and change the comparison. Everything shaded green is a value of x that would make it true.
Drag the circle, or use the arrow keys. It moves in steps of 0.2.
Every number to the left of 0.2 — but not 0.2 itself, so the circle is hollow.
Watch the circle, because it carries the part everyone gets wrong:
Now turn not on with x > 2 selected and watch two things happen together. The shading jumps to the other side, and the circle fills in — because “not greater than 2” means 2 or less, and 2 has to be part of it. That pairing is the whole reason a hollow circle is worth drawing.
Why a robot cares. Two conditions that look almost identical — light < 30 and not (light > 30) — differ by exactly one value, the reading of precisely 30. A robot sitting right on its threshold behaves differently under the two, and that is the sort of bug that only shows up occasionally and looks like a broken sensor.
These three join answers together rather than numbers. The trap is that English is looser than a program: “stop if it is close and the bumper is pressed” sounds like it covers both situations, when it covers neither on its own.
Flip the two conditions and watch the table. There are only four possible situations in total, and and and or differ on exactly two of them.
| close | bumper | and | or |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
and is fussy: it wants both. Three of the four rows are false.
Two sensors are running below: an Ultrasonic reporting a number, and a Touch Sensor reporting true or false. Watch the comparison turn the number into an answer, and watch and and or disagree.
and was true in one row out of four. or was true in three. That is the whole difference, and it is why one of them makes a robot look broken.
The comparison is doing one job: it takes a reading that is neither true nor false and, by holding it against a number you chose, produces something a decision can use. The moment the blue fill crosses the black marker is the moment the answer changes.
The number you compare against is a design decision, not a fact. “Close” for a parking sensor might be 15 cm; for a robot arm it might be 3. Pick it by measuring what the sensor actually reads in the situation you care about, then leave a margin.
and narrows: both must hold, so the robot acts less often but more certainly — stop only if something is close and the bumper is pressed. or widens: either will do, so the robot acts more readily — stop if something is close or the bumper is pressed.
In the four situations above, and was true in one of them and or in three. That is the practical difference: swapping one for the other does not adjust a robot slightly, it changes how often it reacts at all.
Say this back before moving on: “Not too near and not too far.”
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | Reads the sensor continuously and decides, for each reading, whether it is looking at a plant. |
| Large Motor — the travel | Moves the robot along the row. Slowly: a fast robot passes through the window before it has had time to notice it. |
| Medium Motor — the watering mechanism | Works the arm, valve or pump. Runs only while a plant is in the window. |
| Ultrasonic Sensor | Faces sideways, at the plants — not forwards. It is looking across the row, not along it. |
Use dry “watering” for testing. A model that flags a plant by moving an arm and beeping is far easier to debug than one that actually gets things wet — and you can turn a real mechanism on once the logic is right.
Sensors go in ports 1, 2, 3, 4. Motors go in ports A, B, C, D. They are not interchangeable, and nothing will tell you politely if you swap them.
| Part | Port | Why this one |
|---|---|---|
| Travel motor (Large) | A | Moves the robot along the row. |
| Watering (Medium) | D | The other end of the Brick, so its cable is nowhere near the sensor’s line of sight. |
| Ultrasonic Sensor | 4 | Ultrasonic is always port 4 in this course. |
Check your own build 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.
Bluetooth. The robot travels along a row, and a cable trailing across the row is one more thing the sensor can see.
Drive slowly along the row, and water only what is genuinely a plant.
when program starts :: events hat
clear display :: display
write [KEBUN] at line (1) :: display
[A v] start motor at (20) % speed :: motors
repeat (60)
write ([4 v] distance in [cm v]) at line (3) :: display
if <<([4 v] distance in [cm v]) > (10)> and <([4 v] distance in [cm v]) < (30)>> then
write [SIRAM ] at line (5) :: display
[D v] start motor at (60) % speed :: motors
play beep (70) for (0.1) seconds :: sound
else
write [CARI ] at line (5) :: display
[D v] stop motor :: motors
end
wait (0.2) seconds
end
[A v] stop motor :: motors
[D v] stop motor :: motors
write [SELESAI] at line (7) :: displayand. Both halves must agree before anything gets watered.What success looks like: the robot rolls along and the watering motor runs in bursts — one burst per plant, starting as it comes alongside and stopping as it leaves. Between plants, and at the end of the row, nothing.
If it waters continuously, your ceiling is too high and the gaps between plants are inside the window. Read the screen as it travels between two plants and set the ceiling below that number.
Same row of plants every run, same starting point. Count how many plants got watered and how many did not — that is the score.
> (10) half, leaving only closer than 30. Put your hand right in front of the sensor as it passes. It waters your hand.< (30) half instead. Now it waters continuously all the way along, including the empty air at the end.Steps 3 and 4 together are the real lesson. Too narrow and you miss real plants; too wide and you water the wall. The right window is the one that fits your row with a margin, and the only way to find it is to measure and then try.
Every sensor rule you ever write should make you ask: what is on the other side of this number?
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.
Use the viewer's own controls to zoom and turn pages. Fullscreen makes it big enough to build from.
The same build on Google Drive — sometimes a video, sometimes a scan:
Use the viewer's own controls to zoom and turn pages. 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.

Work through the challenges in order — each is harder than the last. The mission comes after all three, and it is meant to make you plan before you build.
Water three plants in a row and nothing in between. Set them out with clear gaps, run the robot along, and count: three bursts, three plants, nothing at the gaps and nothing at the end of the row.
Find your window by measurement, not guessing. Record the sensor reading beside a plant, between two plants, and at the empty end of the row. Choose a floor and a ceiling from those three numbers and justify both.
Break it on purpose, twice. Make a window so narrow it misses real plants, then one so wide it waters the wall. Report the numbers for each and say what the symptom looked like.
Water a garden where the plants are not all the same. Set out a row where the plants are different sizes and different distances from the robot's track — as a real bed would be. A single window will not catch all of them without also catching something it should not. Measure first. Record the sensor reading for every plant and for every gap, and write them in a table before you write any blocks. The table is what tells you whether one window can work at all. Then build the best rule you can. It may need more than one window, or a window plus something else — and if you conclude that no rule can separate your plants from your gaps, that is a legitimate finding as long as the table proves it. Two questions when you present it. Which plant was hardest to catch, and what about it made it hard? And your robot decides purely on distance — describe one other thing it could measure that would separate plants from walls more reliably.