Lesson: Exercise: Using a Gyro Sensor

Explore using a Gryo (gyroscope) sensor to determine and alter robot direction of travel.

Learn how to to use a Gyro (gyroscope) sensor to determine robot direction of travel and how you can use it to control direction of travel.


A Gyro (gyroscope) sensor can be used to monitor robot direction of travel. You can use a Gyro sensor to make sure you travel in a straight line or to control turns accurately. A Gyro sensor reports any direction change by your robot and that information can be used to control direction of travel.

As we did for the Touch and UltraSonic sensors, we are going to use a library class that wraps the EV3GryoSensor class and exposes simpler methods to use in our programs. Create a new class file called GyroSensor in the library package and copy this code into that class. You should study this class to see how the EV3GyroSensor is used to return direction information to your programs.

Once that is done, create a new package called ev3.exercises.gyroDemo. Then add a class called GryoDemo to the package, Copy the following code into the new class file:

This program will set the Gryo heading to zero at start up and then display any direction change in degrees (positive when turning left, negative when turning right) from the zero point. Hold your EV3 in your hand and rotate it to see the angle from the starting point displayed. The rate of change of the angle is also displayed. Note that the angle is cumulative. If you turn right 45 degrees and turn  turn further right you will see an angle of 90 degrees. The angle will get larger or smaller as you rotate the EV3. You can call the reset() method to set the current direction of the EV3 to zero and start measuring any direction change from the new heading.


Lesson navigation: