The Rattlesnake: a coiled machine. Its scissor arm strikes forwards, and its tail shakes. An Ultrasonic Sensor watches the space in front of it. You used one on the Radar in Lesson 41. Today you get to know it well.
A Touch Sensor answers a yes-or-no question. Is the button pressed? Yes or no. That is all it can ever say.
This one answers a how much question. It gives a distance in centimetres, as a number. With a number, the machine can do one thing when something is near. It can do another when it is far. No yes-or-no sensor can do that.
In the real world 5 min
Where you have seen it
A rattlesnake does not chase. It coils up and waits. It only strikes when something comes close enough to reach. And it warns you first, with its rattle. A snake would much rather you went away. It does not want a fight.
A South American rattlesnake, Crotalus durissus, coiled. Photo: Holger Krisp / Wikimedia Commons (CC BY 3.0).
Why it is built that way
A strike takes a lot of work. The snake can only reach about half its own length. So it all comes down to judging distance. Too far, and the strike is wasted. Too close, and the warning came too late.
A real rattlesnake does this with heat. It has small holes near its eyes. They feel an animal’s warmth. Your robot cannot feel heat. It does the same job with sound, like a bat. It sends out a click, too high for you to hear. It listens for the echo. Then it times how long the echo took.
Sound travels about 34 centimetres every millisecond. Time the echo, and a little maths gives the distance. That is all the sensor does. It does it many times a second.
What would go wrong without it
A snake that struck at everything would soon be worn out. A snake that never struck would be eaten.
The best way is in between. It needs a threshold. That is a distance where the answer changes. Further away means “ignore”. Closer means “act”. You need a number to compare with a threshold.
A yes-or-no sensor can tell you something is there. Only a measuring sensor can tell you if it is close enough.
The main concept — the Ultrasonic Sensor 6 min
It has two round holes on the front. One sends a click. The other listens for the echo. The rest is timing.
one sends, one listens
The Ultrasonic Sensor from the front. The two holes are not eyes. One is a speaker and one is a microphone. It makes a sound. Then it listens for the reply.
Block
What it does
([4 v] distance in [cm v] :: sensors)
Gives the distance as a number. It fits where a number fits, even in a write block.
Makes the program wait. It waits until something is closer than your threshold.
[4 v] when distance [< v] (15) [cm v] :: events hat
A hat block, like the Clamp’s in Lesson 39. Its stack runs each time something comes closer than the distance.
The menu next to the number picks the comparison. < means closer than. > means further than. The big idea today is that symbol and its number. That is the threshold. Change the 15, and you change the snake’s temper.
when program starts
start moving straight: 0
4 wait until distance <15cm
stop moving
60cm · reading15cm · threshold
Nothing is close. The sensor reports about 60 cm and the program waits.The robot drives forward. The sensor is sending a burst of sound and timing its echo, over and over, and the number falls.The reading has dropped past the threshold. The condition is true, so the robot stops.Try another threshold. The program is identical — only that one number is different.Finished. The threshold is yours to choose — the sensor only supplies the number.
stopped
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.
This sensor is bad at three things. You will meet all three today. Soft things, like cloth or a jumper, soak up the echo. They look far away. Tilted things bounce the click off to the side. It never comes back. And closer than about 3 cm, it cannot measure at all. The echo comes back before the click has finished.
It does not see. It shouts and listens. If something does not echo back, the sensor cannot find it.
▶The Ultrasonic Sensor, in fullDistance, thresholds, the blocks, and when it goes wrong. Open it if any of that is not clear yet.Show meHide
ComponentSensing6 min
The Ultrasonic Sensor
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.
front
side
The two round openings on the front are the point of this sensor: one sends the burst of sound out, the other listens for the echo coming back.
Blocks reference
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.
A number, not a yes or no
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.
when program starts
start moving straight: 0
4 wait until distance <15cm
stop moving
60cm · reading15cm · threshold
Nothing is close. The sensor reports about 60 cm and the program waits.The robot drives forward. The sensor is sending a burst of sound and timing its echo, over and over, and the number falls.The reading has dropped past the threshold. The condition is true, so the robot stops.Try another threshold. The program is identical — only that one number is different.Finished. The threshold is yours to choose — the sensor only supplies the number.
stopped
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.
Why it matters
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.
If your set has an Infrared Sensor instead
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.
IR Sensor
Beacon
The Infrared Sensor and its Beacon, from the Home set. If your kit has these, expect proximity numbers rather than centimetres — and retune any threshold accordingly.
Say this back: “It sends a click. It times the echo. The answer is a distance in centimetres.”
What’s in this build 4 min
Three parts have cables. One of them is missing from the catalogue’s parts list. That is good to know.
Part
What it is doing here
EV3 Intelligent Brick
Runs the program. It is also the snake’s body.
Large Motor ×2
One moves the scissor arm that strikes. The other shakes the tail.
Ultrasonic Sensor
Faces forward. It watches where the snake strikes. The catalogue’s parts list forgets it. The manual and the model do not.
Aim the sensor before anything else
This sensor only knows what is in front of it. So where it points is what the snake can sense.
Look at the sensor from the front. Is it pointing straight ahead and level? Does it point the way the strike will go?
Is any part of your model in front of it? A beam, the striking arm, the tail? Can the sensor see part of the snake? Then it reads that distance for ever. It will never strike.
Put your hand slowly in front of it. Picture the sound spreading out like a cone. It is wider than you think. It is about as wide as a torch beam.
Move the striking arm by hand, all the way. Check it moves freely. It must not hit the sensor.
Ports — and the rule 4 min
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.
You have now met all four sensors. Touch has used port 1 since Lesson 16. The Ultrasonic Sensor’s home is port 4, just as it was on the Radar.
Part
Port
Why this one
Large Motor (the strike)
A
The main motor that does the work.
Large Motor (the tail)
D
A second job. So we use A and D, not B and C. B and C are for a pair that moves together. These two do different things at different times.
Ultrasonic Sensor
4
The Ultrasonic Sensor always goes in port 4. Touch 1, Gyro 2, Colour 3, Ultrasonic 4.
Check your own build now:
Strike motor in A, tail motor in D, sensor in 4. Two letters and one number.
Clear the space in front of the snake, about half a metre.
Point it at a wall or a book. Do not point it at an empty room. Empty air gives the click nothing to bounce off. The number will be big, or jump about.
Agree with your group what will set it off. A hand far enough away is fine. A book you slide closer on the table is better.
Connect the Brick 4 min
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.
▶How to connect the BrickUSB and Bluetooth, step by step, with a photograph of every screen. Open it if you have not done this before — or if pairing is not working.Show meHide
USB — the reliable one
Switch the Brick on with the dark grey centre button.
Cable into the Brick’s PC port — the small square socket beside the numbered ports, not one of the numbered ones.
Other end into the computer.
Bluetooth — name it first
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.
Name your Brick. On the Brick: Settings (the spanner) → Brick Name. Type something nobody else will pick, then press the tick. Every Brick is called EV3 until somebody changes it.
Turn Bluetooth on. Settings → Bluetooth. Tick Bluetooth and Visibility. Leave iPhone/iPad/iPod unticked.
Connect from EV3 Classroom. Click the Brick icon at the top of the programming area, find your Brick by name, and click Connect.
Say yes on the Brick. It asks “Connect?” with the computer’s name — choose the tick, then accept the passkey, which is already 1234.
Where to read it. The name sits in the bar across the very top of the screen, on every screen — so you can check which Brick you are holding at any moment without going into a menu. This one is EV3VE. A Brick nobody has renamed says EV3.Step 3, and the reason step 1 exists. Three Bricks in range — read the name before you click Connect. Pairing with the wrong one is not an error: it works perfectly, on somebody else’s robot.
Step 2.Bluetooth switches the radio on; Visibility is what lets the computer find you. With Visibility off your Brick works perfectly and simply never appears in the list.Step 4. Look at the Brick. It asks whether to accept and names the computer. Choose the tick.Then the passkey, already 1234. Press the tick again and you are connected.
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.
Keep the cables out of the sensor’s way. A cable in front of the two holes is something to bounce off. The snake will think something is twenty centimetres away. It never moves.
Confirm the connection 2 min
This is a very useful check. The port tile shows a changing number, not just yes or no. Spend the full two minutes here.
Check the Brick icon. Look for a motor on A and D. Look for an Ultrasonic Sensor on 4.
Watch the port 4 tile. Move a book slowly towards the sensor. The number goes down. Move it away, and the number goes up.
Hold the book at what looks like 20 cm. Read the tile. Now measure it with a ruler. How good was your guess?
Now the fun part. Point it at something soft, like a jumper or a bag. Then point it at something hard and flat. Keep it the same distance away. The numbers will not match.
Tilt the book a lot. Watch the number jump about or get big. The click is bouncing away, not back.
You have seen this sensor get it wrong twice. You have not written a block yet! Remember what it was looking at each time.
Change it and test 8 min
One change at a time. Guess what will happen before each run. Most of today’s changes are to one number.
Change the threshold to 50 cm. How will the snake’s temper change? Now it strikes at people walking past the table. It is jumpy, and most strikes are wasted.
Now 5 cm. You have to put your hand almost on it. A shy animal might do this. But it is no good as a warning.
Find the threshold you would really choose. Say why, in one sentence. There is no right answer. But you need a good reason.
Change the comparison from < to >. Set it to 30. Now the snake strikes when things go away. That is silly for a snake. But it shows the menu really matters.
Put it back. Then add a write ([4 v] distance in [cm v]) at line (1) just inside the loop. Now the model shows what it can see. You can watch the number it compares with the threshold.
Keep step 5 for the rest of the course. Does a sensor program go wrong? Put the reading on the screen. Most ultrasonic problems are the same. The sensor was reading something you had not thought about.
Is a sensor program doing the wrong thing? Look at what the sensor is really reading. Do that before you change the program.
This is what you are building: the Rattlesnake.
Make it move 10 min
Seven blocks. Wait for something to come close. Rattle a warning. Then strike and go back. Forever, because a snake does not stop after one.
when program starts :: events hat
[A v] set speed to (70) % :: motors
forever
[4 v] wait until distance [< v] (20) [cm v] :: sensors
play sound [Animals / Snake rattle v] until done :: sound
[A v] run [clockwise v] for (1) [rotations v] :: motors
[A v] run [counterclockwise v] for (1) [rotations v] :: motors
end
The warning comes before the strike. That is the Lesson 6 rule. The snake thought of it first!
Walk it in the Brick's order:
set speed to 70 % — a setting. A strike should be fast.
forever — the snake stays ready.
wait until distance < 20 cm — the program waits here. It checks the sensor over and over. It stops when something is closer than twenty centimetres. This is the threshold.
play sound Snake rattle until done — the warning. It finishes before anything moves.
run clockwise for 1 rotation — the strike.
run counterclockwise for 1 rotation — back to coiled up. Ready for the next one.
What success looks like: the snake ignores you at an arm's length away. Bring your hand closer than about twenty centimetres. It rattles and strikes. Move away, and it gets ready again. Come back, and it does it again.
Does it strike again and again without stopping? Then something is always closer than twenty centimetres. It is probably part of your own model. Or the sensor is tilted down at the table. Check the port 4 number with nothing in front of it.
Challenges 27 min
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.
Challenge 1
Show the sensor's reading on the screen. Keep it there while the snake works. Then hold up three things at three distances you know. One is hard and flat. One is soft. One is at a steep angle. Tell us what the sensor said for each one. Tell us what a ruler said.
Challenge 2
Give the snake two ranges. It should rattle a warning when something comes inside a far distance. It should strike only when it comes inside a nearer one. Then a hand coming close is warned before it is struck. Choose both distances. Say why you chose them.
Challenge 3
Find where you can no longer trust the sensor. Test at 5 cm, 10, 20, 50, 100 and 200. Each time, check the reading against a tape measure. Tell us the range where you would trust it. Say what happens outside that range.
Extra challenge
Build a guard that knows when to act.
Your snake must sit ready. It must not mind normal moving around the table. It must warn when something really comes close. It must strike only if the thing keeps coming. It must not strike at someone walking past. It must not miss a hand that comes right up to it.
Plan on paper before you build. Write down what should happen at a few distances: a metre, half a metre, twenty centimetres, five. Only then choose the thresholds that make that happen. A threshold is the number where the robot changes what it does.
Two questions when you show it. What can your sensor really not see? How did you find out? And why is a false alarm a worse fault in this machine than a missed strike?
Build it 15 min
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.