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.
#!/usr/bin/env pybricks-micropythonfrompybricks.hubsimportEV3Brickfrompybricks.ev3devicesimportMotorfrompybricks.parametersimportPort,Stopfrompybricks.roboticsimportDriveBasefrompybricks.toolsimportwaitfrompybricks.ev3devicesimport(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 Sensortouch_sensor=TouchSensor(Port.S1)# Drive forward until touch sensor is pressedrobot.drive(1000,0)whilenottouch_sensor.pressed():wait(1)robot.stop()