Using the Touch Sensor

The Touch Sensor detects whether the button on the face of the sensor is pressed in. You can use a Touch Sensor to detect, for example, when your robot drives into something. You could also use a finger press on a Touch Sensor to trigger an action.
The Touch Sensor can indicate that it is either pressed in, or not. It cannot measure how far or how hard the button is pressed in. The Touch Sensor gives Logic data (True or False). The position of the Touch Sensor button is called its State, and is True for pressed in and False for not pressed in (released).

The Touch Sensor can also keep track of whether the button has been pressed and then released in the past. This is called Bumped and is useful to detect, for example, finger presses. See Understanding Bumped for more information.
Touch Sensor Data
The Touch Sensor can give the following data:
DataTypeNotes
State LogicTrue if the button is pressed in, False if not.
Pressed LogicTrue if pressed, False if not (same as State).
Released LogicFalse if pressed, True if not (opposite of State).
Bumped LogicTrue if the button has been pressed and released in the past. The next Bumped occurrence will then require a new press and release.
Examples Using Pressed
Some examples of how you can use the Pressed data of the Touch Sensor are below.
Example 1: Drive until a Touch Sensor is Pressed
This program makes a robot drive straight forward until a Touch Sensor is pressed, then the robot is stopped. It uses the Wait block with the Touch Sensor - Compare – Touch mode to test for Pressed.
Tips and Tricks
Remember to use the On mode of the Move Steering block when you want to drive while waiting for a sensor.
Example 2: Run a Motor whenever a Touch Sensor is Held in (Method 1)
This program makes a motor run whenever the Touch Sensor is pressed and held in. The motor is stopped whenever the Touch Sensor is released. The program uses a Switch block with the Touch Sensor - Compare – Touch mode to test for Pressed. The result of the test is used to choose between turning the motor on or off. The test is repeated continuously in a Loop.
Example 3: Make the Brick Status Light Pulse whenever a Touch Sensor is Held in
This program turns the Brick Status Light on in orange, and makes it pulse whenever the Touch Sensor is held in. It uses the Touch Sensor block in Measure mode to get the state of the Touch Sensor. The result is wired to the Pulse input of the Brick Buttons block with a Logic data wire.
Examples Using Released
Some examples of how you can use the Released data of the Touch Sensor are below.
Example 4: Beep on each Touch Sensor Press
This program sounds a short tone each time the Touch Sensor is pressed. Only one tone is sounded for each press. The program uses a Wait block in the Touch Sensor - Compare – Touch mode to test for Pressed and then another Wait block to wait for Released before letting the loop continue.
Tips and Tricks
If you delete the Wait for Release from this program, you will find that the tone will repeat as long as the Touch Sensor is held in. This is because the Wait for Pressed will immediately continue to the next block if the Touch Sensor is already pressed. Try it!
Example 5: Run a Motor whenever a Touch Sensor is Held in (Method 2)
This program makes a motor run whenever the Touch Sensor is pressed and held in. The motor is stopped whenever the Touch Sensor is released. The program uses a Wait block in the Touch Sensor - Compare – Touch mode to wait for Pressed to start the motor, then another Wait to wait for Released before stopping the motor. The process is repeated in a Loop.
Tips and Tricks
This program does the same thing as Example 2 above, using a different method.
Understanding Bumped
In addition to telling you whether the Touch Sensor button is currently pressed or released, the Touch Sensor also keeps track of whether it has been pressed and released in the past, which is called Bumped. This makes it easy to find out whether a Touch Sensor has been pressed like a pushbutton, without needing to check it constantly for a press and then wait for the release.

Once a Touch Sensor indicates that it has been Bumped, it will not indicate Bumped again until the Touch Sensor is pressed and then released a new time. This makes it easy to make sure that, for example, each press corresponds to an action happening only once.

The table below shows an example where a Touch Sensor is pressed and released twice as a series of steps. The table shows the result of a program testing for Pressed, Released, and Bumped after each action.
StepActionPressedReleasedBumped
1 Button starts releasedFalseTrueFalse
2 Button is pressed inTrueFalseFalse
3 Button is releasedFalseTrueTrue
4 Button is still released, and the program tests the Touch Sensor againFalseTrueFalse
5 Button is pressed a second timeTrueFalseFalse
6 Button is held in, and the program tests the Touch Sensor againTrueFalseFalse
7 Button is releasedFalseTrueTrue
8 Button is still released, and the program tests the Touch Sensor againFalseTrueFalse
Note that when the button is held in, the Touch Sensor will continue to indicate Pressed each time the program tests it. However, once the button is released, the sensor will only indicate Bumped the first time the program tests it for Bumped. The sensor will not indicate Bumped again until it is pressed and released a new time.
Examples Using Bumped
Some examples of how you can use the Bumped data of the Touch Sensor are shown below.
Example 6: Change the Display when the Touch Sensor is Pressed
This program will make the EV3 Brick Display show “Zero”, then “One”, then “Two”, changing the Display each time the Touch Sensor is bumped (pressed and then released).
Tips and Tricks
If you change the Wait for blocks in this program to test for Pressed instead of Bumped, you will find that the display goes from “Zero” directly to “Two”, skipping “One”. Try it! This is because the Display blocks execute so quickly that when the second Wait for Pressed test happens, your finger is still holding the button in from the first press, so the second Wait for ends immediately. When testing for Bumped, only one test will succeed for each different press.
Example 7: Drive in a Pattern until the Touch Sensor is Pressed
This program makes a robot repeat a pattern of driving straight and then turning, until a Touch Sensor, acting as a “Stop” button on the robot, is pressed. After the Touch Sensor is pressed, the robot will stop after the next turn. The program uses a Loop in Touch Sensor mode to repeat the driving until the Touch Sensor is Bumped (pressed and then released).
Tips and Tricks
If you try this program using Pressed instead of Bumped, you will find that pressing the Touch Sensor usually does not make the robot stop. Try it! This is because the loop tests the sensor only briefly after the two Move blocks have completed. If you press and release the sensor while the Move blocks are running, the Pressed state will not be seen. Using Bumped, the Touch Sensor remembers that it was pressed and released in the past.
Touch Sensor Blocks and Modes
The table below shows all of the programming blocks and modes that you can use with the Touch Sensor.
BlockModeUse
WaitTouch Sensor – CompareWait for the Touch Sensor to be Pressed, Released, or Bumped.
WaitTouch Sensor - ChangeWait for the Touch Sensor state to change.
LoopTouch SensorRepeat a sequence of blocks until the Touch Sensor is Pressed, Released, or Bumped.
SwitchTouch SensorChoose between two sequences of blocks depending on whether the Touch Sensor is Pressed or not, Released or not, or Bumped or not.
Touch SensorMeasureGet the current Touch Sensor state (Pressed or not) on a Logic data wire.
Touch SensorCompareTest the Touch Sensor for Pressed, Released, or Bumped, and get the result on a Logic data wire.
Data LoggingSee Data Logging.
Touch
Quick links