Main Content


Read rotation angle from gyroscopic sensor

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



angle = readRotationAngle(mygyrosensor) reads the total amount of rotation since the creation of the connection to the sensor, and returns the measurement in degrees. You can use the resetRotationAngle function to reset this value to zero.


collapse all

Measure angle of rotation using a gyroscopic 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.

Create a connection to the gyroscopic sensor called mygyrosensor.

mygyrosensor = gyroSensor(myev3)
mygyrosensor = 

  gyroSensor with properties:

    InputPort: 2

Measure the rotation, in degrees, since the creation of the connection to the sensor.

angle = readRotationAngle(mygyrosensor)
ans =


The sensor has rotated 59 degrees clockwise.

Reset the measurement to zero.


Input Arguments

collapse all

Connection to gyroscopic sensor, specified as a string that represents the object created using gyroSensor.

Example: mygyrosensor

Data Types: double

Output Arguments

collapse all

Angle of rotation in degrees, returned as an int32 value. With the sensor upright, positive values indicate clockwise rotation. Negative values indicate counterclockwise rotation.