Main Content


Read distance from infrared sensor to object



proximity = readProximity(myirsensor) measures the relative distance from the infrared sensor to a nearby object, and returns it as a value from 0 to 100 (near to far). The maximum range of the sensor is approximately 70 cm (27 inches).


collapse all

Measure the relative distance from an infrared sensor to an object.

Create a connection to the EV3 brick called myev3.

myev3 = legoev3
myev3 = 

  legoev3 with properties:

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

The sensor appears in the list of connected sensors.

Create a connection to an infrared sensor.

myirsensor = irSensor(myev3)
myirsensor = 

  irSensor with properties:

      Channel: 1
    InputPort: 2

Read the relative distance from the infrared sensor to the nearest object.

proximity = readProximity(myirsensor)
proximity =


Input Arguments

collapse all

Connection to infrared sensor, specified as a string that represents the object created using irSensor.

Data Types: char

Output Arguments

collapse all

The relative distance, returned as an int32 value, from the infrared sensor to a nearby object based on the amount of infrared light that the object reflects.