TMC - 8 Digital Tech
Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto mode

Touch Sensor

adapted from PyBricks

Touch Sensor

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.

Example Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)

# Initialize the EV3 Brick.
ev3 = EV3Brick()

# Initialize the motors.
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)

# Initialize the drive base.
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)

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

# Drive forward until touch sensor is pressed
robot.drive(1000, 0)
while not touch_sensor.pressed():
    wait(1)
robot.stop()