Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.

Need help reading direction with L3GD20H Triple-Axis Gyro
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need help reading direction with L3GD20H Triple-Axis Gyro

by Mr_Maxwell on Mon Mar 09, 2020 4:16 pm

I recently purchased the L3GD20H Triple-Axis Gyro found at the link:
I was having struggles with the offered guides and I was wondering if someone would be so kind as to help me.

In essence, I need to be able to check what direction my device is facing. A 90-degree angle from its starting direction, a 45-degree angle, a 180-degree angle, etc.

The only code provided that i was able to make work was this:
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_L3GD20_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);

void displaySensorDetails(void)
  sensor_t sensor;
  Serial.print  ("Sensor:       "); Serial.println(;
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" rad/s");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" rad/s");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" rad/s"); 

void setup(void)
  Serial.println("Gyroscope Test"); Serial.println("");
  /* Enable auto-ranging */
  /* Initialise the sensor */
    /* There was a problem detecting the L3GD20 ... check your connections */
    Serial.println("Ooops, no L3GD20 detected ... Check your wiring!");
  /* Display some basic information on this sensor */

void loop(void)
  /* Get a new sensor event */
  sensors_event_t event;
  /* Display the results (speed is measured in rad/s) */
  Serial.print("X: "); Serial.print(event.gyro.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.gyro.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.gyro.z); Serial.print("  ");
  Serial.println("rad/s ");

I received these values: X: 0.09 Y: -0.12 Z: -0.02 rad/s
They stayed consistent until I moved the device, then they returned to the original values. I don't understand what it's reading, but it's not direction. If anyone could show me the correct code to use for my application it would be greatly appreciated I'm very lost. Thank you!

Posts: 1
Joined: Mon Mar 09, 2020 3:54 pm

Re: Need help reading direction with L3GD20H Triple-Axis Gyr

by adafruit_support_bill on Mon Mar 09, 2020 4:34 pm

A gyro does not detect absolute orientation. It can only detect changes in orientation. It reports the rate of rotation (in degrees per second) for each of the 3 axis. To detect the total degrees of rotation, you need to integrate the rate of rotation over time.

In theory, when stationary, the rate of rotation is zero. But most sensors exhibit a small 'zero rate' error. For best results, you should make note of this error when stationary and subtract it from your readings.

When integrating rate of rotation over time, you will gradually accumulate small errors and your readings will tend to 'drift' from the true orientation over time. This is and unavoidable limitation of gyros.

Like all sensors, gyros have their strong points and their weak points. IMUs (Inertial Measurement Units) combine gyros with accelerometers and magnetometers and use complex algorithms calculate a more accurate orientation using the combined strong points of each sensor type.

Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.