Challenge 1
Fool each sensor on its own. Put a bag where a person would stand. The gate must not open. Wave the ticket from across the room. It must not open then either. If one of these tricks works, you have used "or" where you meant "and".
Quick Learn — the model, the instructions, the program and the challenges. The theory is in Full Lesson.
EV3 Robotics›Level 2 · Intermediate›Lesson 31
Level 2 · Lesson 31 · EV3-L02-3160 minutes · Ages 8+ · Model: Ticket Checker
The Ticket Checker is a gate. A Large Motor lifts the barrier. An Ultrasonic Sensor looks for a person at the gate. A Colour Sensor reads their ticket.
Each sensor on its own can be tricked. A bag by the gate looks like a person. A red jumper looks like a good ticket. A mistake only matters when the barrier opens.
By the end, your gate will open only when both sensors agree. You will also choose which mistake it may make.
Look closely at a ticket gate at a station. It has more than one sensor. One sees that a person is there. One reads the ticket or card. Often a beam further in sees that the person is through. Then the gate can close behind them.

The gate does not open just because the card is read. It opens when the card is good and a person is standing there. Hold up a card from two metres away, and nothing happens. That is on purpose. If the card alone opened it, anyone waving a wallet could keep it open.
Every sensor is wrong sometimes. A reader gets a bent card wrong. A suitcase breaks a beam. A sensor spots a coat going past. Making two sensors agree does not make either one better. But it is less likely they are both wrong at the same time.
Using sensors together like this is called sensor fusion. It is everywhere. A plane has three speed sensors. It believes the two that agree. Some cars can brake by themselves. They wait until the camera and the radar both see the same thing in the road.
This is the important part. The two mistakes are not equally bad. A gate that opens by mistake lets someone in without paying. A gate that stays shut by mistake just annoys someone. They go and ask the staff.
Decide which mistake you would rather make. Then build the machine that makes that one.
In Lesson 30 you joined checks to decide what should happen. Today you join them to decide how sure you are. That is a different question.
| Sensor | Fooled by | What the gate does wrongly |
|---|---|---|
| Ultrasonic alone | A bag, a trolley, someone walking past the gate. | Opens for nobody. |
| Colour alone | A red coat, a poster, the sensor pointing at the floor. | Opens with no ticket presented. |
| Both, agreeing | Something shaped like a person and showing the right colour, at the same time. | This is rare. It is usually a real person. |
set [present v] to <([4 v] distance in cm) < (20)> :: variables set [valid v] to <([3 v] color) = [green v]> :: variables if <(present) and (valid)> then broadcast [open gate v] :: events end
| Mistake | What it means here | Made less likely by |
|---|---|---|
| False positive | Opens when it should not. | Using and. More must agree first. |
| False negative | Refuses a real passenger. | Using or. Less must agree. |
You cannot make both mistakes rarer at once. That is true for every machine, not just EV3. Make the gate stricter, and you get fewer false positives. But you get more false negatives. Make it looser, and the opposite happens. So do not ask “how do I stop all mistakes?” Ask “which mistake can I live with?”
Two sensors can agree for a split second just by luck. Make them agree for half a second, and almost all of those lucky moments go. The cost is a short wait.
wait until <(present) and (valid)> :: control reset timer :: control wait (0.4) seconds :: control if <(present) and (valid)> then broadcast [open gate v] :: events end
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.
The Colour Sensor looks down at a surface and can answer three quite different questions: what colour is this?, how bright is this? and how light is the room? Choosing the wrong one is the usual reason a line-following robot refuses to work.
| Block | What it does |
|---|---|
([3 v] color :: sensors) | Reports which colour it sees, from a short list — red, blue, green, black, white and a few more. |
([3 v] reflected light intensity :: sensors) | Reports how bright the surface is, as a number from 0 (black) to 100 (white). |
<[3 v] is color [red v]? :: sensors> | Reports true or false for one particular colour. |
([3 v] ambient light intensity :: sensors) | Reports how much light is falling on the sensor, 0 to 100, with its own lamp switched off. |
The sensor does not describe a colour — it picks one from a list of eight, and that list is the whole of what it can ever say:
| Reports | Means |
|---|---|
| 0 | no colour — too far away, or too dark to call |
| 1 · 2 · 3 | black, blue, green |
| 4 · 5 · 6 | yellow, red, white |
| 7 | brown |
Anything you put under it is forced into one of those eight. There is no orange and no purple: an orange brick comes back as red or as yellow, and often as red one moment and yellow the next as the robot creeps along. Light blue and grey are the other classic pair to avoid — grey is neither black nor white, so it flips between them.
This is why colour mode is a good fit for a task you control and a bad fit for one you do not. Sorting the LEGO bricks that come in the set works, because they are made in exactly these colours. Reading a printed sheet, a coloured tile from another set, or anything pastel is asking the sensor to answer a question it does not have a word for.
Two practical points follow from how it decides. It shines its own lamp and looks at how much red, green and blue comes back, so it must be close — about half a centimetre, and no more than a centimetre. Lift it and the answer decays to 0. And because it takes those three readings before it can answer, colour mode is the slowest thing this sensor does; a robot driving quickly can pass right over a small patch without ever reporting it.
When a colour must be recognised reliably, test it. Drive the robot slowly over the real surface with color shown on the screen and watch what it actually says — including what it says at the edges between two colours, which is where the wrong answers live.
Both questions are asked of the same surface at the same moment. Watch the two answers travel across a strip of colours and then over the edge of a black line.
Watch the two read-outs over the last third of the strip. One of them changes once. The other changes the whole way across.
Over the patches, both read-outs are useful. Over the edge of the line they part company: the colour name has only two answers to give and jumps between them, while the number slides smoothly from 88 down to 8. Every value in that slide tells you how far onto the line the sensor is — which is information the name simply does not carry.
A line follower built on colour names only knows “black” or “not black”, so it can only lurch. Built on reflected light it can tell how far onto the line it has drifted, which is what makes smooth following possible.
The first two modes both switch the sensor’s own lamp on and measure what bounces back off the surface. Ambient light intensity does the opposite: the lamp goes off, and the sensor simply reports how much light is arriving from wherever — 0 in the dark, up to 100 in bright light.
| Mode | Own lamp | Measures | Points |
|---|---|---|---|
| colour | on | which of eight colours the surface is | at the surface, very close |
| reflected light | on | how much of its own light comes back | at the surface, very close |
| ambient light | off | how bright the surroundings are | wherever you want to measure |
That makes it the only one of the three that is not really about the floor. A number between 0 and 100 means very little on its own, so watch the same sensor sit through five different rooms — nothing underneath it changes at any point.
the sensor’s own lamp is off — it is measuring the room
Nothing under the sensor changed at any point in this run. Ambient light is the one mode that is not asking about the surface at all.
Those are the shape of the scale rather than exact figures, but the shape is the useful part: a lit room is nowhere near 100, and the top of the range is reserved for a light pointed straight at the sensor. Cover it with your hand and the number drops to near zero — which is the easiest way to check the sensor is doing what you think.
Point it at the ceiling and it tells you whether the room lights are on; point it forwards and a torch will spike the reading, which is a way of signalling to a robot without touching it.
Do not reach for it as a substitute for reflected light. Room light falling on a black line and on white paper is almost the same, so ambient mode can barely tell them apart — the reason reflected light works is precisely that the sensor brings its own light and measures how much of it survives.
It is also the mode most at the mercy of the room. A reading taken by a window in the morning will not match the same spot in the afternoon, so anything built on ambient light needs measuring on the day, in the place, with the lights as they will be.
Even the two lamp-on modes are affected by room lighting — a reading taken by a sunny window differs from one taken in a corner. The sensor must also sit close to the surface and at a constant height, because lifting it changes the reading even though the surface has not changed.
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.
Two sensors do not make each one right. They make it unlikely that both are wrong at the same time.
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: “Which mistake would I rather make?”
Find the two sensors. For each one, think of something you could trick it with. You will use both in step 10.
| Part | What it is doing here |
|---|---|
| EV3 Intelligent Brick | The body of the gate. Its screen shows what each sensor thinks. So you can see when they disagree. You do not have to guess. |
| Large Motor — the barrier | Lifts and lowers the arm. It is a Large Motor because a barrier must move firmly. A slow, weak gate gets pushed. |
| Ultrasonic Sensor — presence | Watches the way in. It cannot tell a person from a bag. That is why you need a second sensor. |
| Colour Sensor — the ticket | Reads a coloured card held up to it. It cannot tell a ticket from anything else of the same colour. |
| The barrier linkage (not electronic) | Must close every time. If the barrier sticks half open, people can get through. No sensor can fix that. |
Put the two sensors where they cannot see the same thing. Is the Colour Sensor so close that the Ultrasonic also sees the ticket? Then they are not working apart any more. Two sensors that go wrong together are hardly better than one.
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 |
|---|---|---|
| Barrier (Large) | A | The only motor. |
| Ticket (Colour) | 3 | Colour stays on 3 across the course. |
| Presence (Ultrasonic) | 4 | Ultrasonic stays on 4 across the 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.
USB is fine. The gate does not move. But keep the cable out of the way in. If the Ultrasonic can see the cable, it thinks a person is always there.
Stuck? There is a photo of every screen in the Brick & Bluetooth guide.
One change at a time. Guess, then run, then look. Today you try to break your own machine on purpose. That is how real gates are tested.
or where you wanted an and.and to or. Now both tricks work. Count how many ways you can get through in thirty seconds. That is how many and was stopping.(present and valid) or override. You have made a way in with no ticket, on purpose. Say why that is OK.Try to break your own machine before someone else does. A mistake you find costs less than one you miss.
Two sensors agreeing keeps you safe when one is wrong. It does not help when one is jumpy.
Your Ultrasonic number jumps by a centimetre or two, even when nothing moves. Stand right on the 20 cm line. Then present flicks from true to false and back, many times a second. The other sensor cannot fix a number that keeps changing its mind.
One fix is to use two lines, not one. Act at one number. Only act again once the number has gone past a second one. You do this next, on the Gymnastic. It counts a gymnast swinging past an Ultrasonic Sensor.
Today made two readings agree. Next, you stop one jumpy reading from counting twice.
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.
One stack reads the sensors and shows what each one thinks. The other stack opens the gate only when both agree.
when program starts :: events hat
[A v] set motor to [hold position v] at stop :: motors
clear display :: display
forever
set [present v] to <([4 v] distance in cm) < (20)> :: variables
set [valid v] to <([3 v] color) = [green v]> :: variables
write (present) at line (2) :: display
write (valid) at line (4) :: display
end
when program starts :: events hat
forever
wait until <(present) and (valid)> :: control
wait (0.4) seconds :: control
if <(present) and (valid)> then
write [PASS] at line (6) :: display
set status light to [green v] :: display
[A v] run [clockwise v] for (90) [degrees v] at (50) % speed :: motors
wait (2) seconds :: control
[A v] run [counterclockwise v] for (90) [degrees v] at (50) % speed :: motors
set status light to [off v] :: display
write [] at line (6) :: display
wait until <not (present)> :: control
end
endwait until not present at the end stops the gate opening again and again for one person. This is called edge detection. You will use it again in the next lesson.hold position at stop on the barrier keeps the gate down. It stays shut if someone leans on it.What success looks like: standing there with no ticket does nothing. Waving the ticket from across the room does nothing. Standing there with the ticket opens the gate. It closes behind you.
Does the gate never open? Stand there with the ticket and watch lines 2 and 4. One of them will say false. That is the sensor to move. The code is probably fine.
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.
Fool each sensor on its own. Put a bag where a person would stand. The gate must not open. Wave the ticket from across the room. It must not open then either. If one of these tricks works, you have used "or" where you meant "and".
Make the wait easy to change, and find the best one. Put the waiting time in a named variable. A variable is a named box that holds a number. Try 0.1, 0.4, 1.0 and 3.0 seconds. For each one, count two things. How many times can you fool it in thirty seconds? AND how often does it turn away a real passenger? Write both numbers down. No setting makes both zero. Say which one you chose, and why.
Add a staff button. The Touch Sensor opens the gate by itself. It needs no ticket and nobody standing there: (present and valid) or override. You have made a way in without a ticket, on purpose. Write one sentence saying why that is OK here. Write one more saying what would stop people misusing it.
Build a gate you would trust on a real barrier. Then prove it. It must deal with all four of these the right way. Show each one to somebody else: - a real passenger with a ticket: it opens quickly. - somebody with no ticket: it says no, and tells them. - an object left in the walkway: it says no. - somebody walking close behind a real passenger: it does NOT let them through when it opens for the first one. That last case is the hard one. It is why real gates have another sensor further in. Your gate must know one person has gone through. Only then can it check the next. Work out how. Then write a short, honest report. In twenty tries, how many times did you get through when you should not? How many real passengers were turned away? Did you get zero of both? Then you have probably not tested it hard enough.
