Touch sensor

adapted from PyBricks

A sensor lets an EV3 program measure and collect data about its surroundings. The Touch Sensor detects when its red button has been pressed or released.

An EV3 touch sensor

Import

from pybricks.ev3devices import TouchSensor

Setup

# Initialize Touch Sensor
touch_sensor = TouchSensor(Port.S1)

Functions

pressed()

Checks if the sensor is pressed.

Returns:

  • True if the sensor is pressed, False if it is not pressed.

Example

# Beep when touch sensor is pressed
while True:
    if button.pressed():
        ev3.speaker.beep()
# Drive forward until touch sensor is pressed
robot.drive(1000, 0)
while not touch_sensor.pressed():
    pass
robot.stop()