Main Content


Read number of pressed button on infrared beacon

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



button = readBeaconButton(myirsensor,channel) returns the number of a pressed button or buttons on an infrared beacon, and returns it as a numeric value, from 0 to 11.


collapse all

Use an infrared sensor to read the number of a pressed button or buttons 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

Check which button someone presses on the IR beacon.

beaconbutton = readBeaconButton(myirsensor,2)
beaconbutton =


Someone is pressing button 3.

Input Arguments

collapse all

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

Data Types: char

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

Output Arguments

collapse all

The number of a pressed button on the IR beacon, returned as an int32 value.

Zero indicates that no one is currently pressing a button.

There are 5 buttons. The top one is a toggle button. When you press it once, it lights up and this function returns 9.

With the top button toggled on, pressing a combination of 2 buttons outputs a value from 5 to 11:

  • 1 & 2 = 10

  • 1 & 3 = 5

  • 1 & 4 = 6

  • 2 & 3 = 7

  • 2 & 4 = 8

  • 3 & 4 = 11