Main Content

irSensor

Connection to infrared sensor

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

Description

This object represents a connection to an EV3 Infrared Sensor (item number 45509). To get the relative distance of an object in front of the sensor, or to get data from an EV3 Remote Infrared Beacon (item number 45508), use this object with the functions listed in Object Functions.

Creation

Description

myirsensor = irSensor(myev3) creates a connection to an infrared sensor. You can use this connection with the functions listed in Object Functions to get the relative distance of an object in front of the sensor, or to get data from an EV3 Remote Infrared Beacon.

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

example

myirsensor = irSensor(myev3,inputport) creates a connection to an infrared sensor that uses a specific EV3 input port by the InputPort property.

example

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 infrared sensor, returned as an object handle.

Properties

expand all

The channel number that the IR beacon uses, specified as a double. The red switch on the beacon has four positions that determine the channel number. The forward position is channel 1.

Example: 2

Data Types: double

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

readProximityRead distance from infrared sensor to object
readBeaconProximityRead distance and heading from infrared sensor to beacon
readBeaconButtonRead number of pressed button on infrared beacon

Examples

collapse all

Get data from an infrared sensor, such as the distance to an object, the distance and heading to an IR beacon, or the number of a pressed button on an IR beacon.

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 =

          76

Read the relative distance and heading from the infrared sensor to an IR beacon that is using channel 2.

[proximity,heading] = readBeaconProximity(myirsensor,2)
proximity =

 -128


heading =

    0

These values indicate that no one is pressing a button on the IR beacon.

Press a button on the IR beacon and try again.

[proximity,heading] = readBeaconProximity(myirsensor,2)
proximity =

   26


heading =

  -14

The IR beacon is 26 distance units away from, and 14 heading units to the left of, the sensor.

Check which button someone presses on the IR beacon.

beaconbutton = readBeaconButton(myirsensor,2)
beaconbutton =

           3

Someone is pressing button 3.