Main Content


Connection to touch sensor

Add-On Required: This feature requires the MATLAB Support Package for LEGO MINDSTORMS EV3 Hardware add-on.


This object represents a connection to an EV3 Touch Sensor (item number 45507). To check whether an item is pressing the front of the sensor, use this object with the readTouch function.




mytouchsensor = touchSensor(myev3) creates a connection to a touch sensor.

If multiple touch sensors are attached to the EV3 brick, this function chooses the sensor that is attached to the EV3 input port with the lowest number.


mytouchsensor = touchSensor(myev3,inputport) creates a connection to a touch sensor that uses a specific EV3 input port defined by the InputPort property.

Input Arguments

expand all

Connection to EV3 brick, specified as a string that represents the object created using legoev3.

Example: myev3

Data Types: char

Output Arguments

expand all

Connection to touch sensor, returned as an object handle.


expand all

This property is read-only.

Number of the EV3 input port that the sensor uses, returned as a double.

Example: 1

Data Types: double

Object Functions

readTouchRead touch sensor value


collapse all

Determine whether an object is pressing the touch sensor.

Create a connection to the EV3 brick called myev3.

myev3 = legoev3
myev3 = 

  legoev3 with properties:

      FirmwareVersion: 'V1.03E'
           HardwareID: []
            IPAddress: []
    CommunicationType: 'USB'
         BatteryLevel: 100
     ConnectedSensors: {'touch'  'gyro'  'color'  'sonic'}

The sensor appears in the list of connected sensors.

Connect to a touch sensor.

mytouchsensor = touchSensor(myev3)
mytouchsensor = 

  touchSensor with properties:

    InputPort: 1

Read the state of the touch sensor.

pressed = readTouch(mytouchsensor)
pressed =


This result indicates that an object is not pressing the touch sensor.

Press the touch sensor while you take another reading.

pressed = readTouch(mytouchsensor)
pressed =


This result indicates that an object is pressing the touch sensor.