EV3 RoboticsComponent tutorialsThe Touch Sensor

Sensing · Level 1 · 5 min

The Touch Sensor

Tell when the red button is pressed, released or bumped.

ComponentSensing5 min

The Touch Sensor

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.

EV3 Touch Sensor from the side with its red button outreleased
EV3 Touch Sensor from the side with its red button pushed inpressed
The red button out, and the same sensor with it pushed in. These two states are the entire output of this sensor — there is nothing in between.

Blocks reference

BlockWhat 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.

Three different events

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.

when program starts
forever
  if 1 is pressed? then
    change count by 1

versus two hat blocks

1 when pressed
1 when bumped
0is pressed? in a loop0when pressed0when bumped
In a loop: 0 answers from one pressBumped: exactly one
Nobody is touching the sensor. All three programs are watching it.
released

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:

  • Pressed — the button is down right now. Good for “hold to run”.
  • Released — it is up again. Good for acting when somebody lets go.
  • Bumped — pressed and released. This is what you want for “click to start”, because it will not fire repeatedly while a finger stays down.

The classic bumper

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.

Why it matters

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.

More sensing tutorials