Arduino

The Adafruit L3GD20 Library for the Arduino implements a convenient device class to handle the the low-level device communication with the Gyro module.  The programming interface is described below:

Install Arduino Libraries

Before you can use the L3GD20, you'l need to install the required libraries using the Arduino Library Manager, which you can open via the menu entry shown below:

You will need to install the Adafruit Unified Sensor library ...

... as well as Adafruit L3GD20 U:

Construction:

To use the L3GD20 in your sketch, you must first call a constructor to create a device object.  There are two forms of the constructor:
  • Adafruit_L3GD20(void);
  • Adafruit_L3GD20(int8_t cs, int8_t mosi, int8_t miso, int8_t clk);    
The first version takes no parameters and is used for I2C communication.  The second version is for SPI communication and requires that you specify the pins to be used.

I2C Example: (use with I2C wiring)
Download: file
// No need to specify pins for I2C
Adafruit_L3GD20 gyro();
SPI Example: (use with SPI wiring)
Download: file
// Define the pins for SPI
#define GYRO_CS 4 // labeled CS
#define GYRO_DO 5 // labeled SA0
#define GYRO_DI 6  // labeled SDA
#define GYRO_CLK 7 // labeled SCL
  
Adafruit_L3GD20 gyro(GYRO_CS, GYRO_DO, GYRO_DI, GYRO_CLK);

Initialization:

Before using the device object you constructed, you must initialize it with the sensitivity range you want to use:

  • bool begin(gyroRange_t rng);

where "rng" can be one of:

  • L3DS20_RANGE_250DPS - for 250 degrees-per-second range (default)
  • L3DS20_RANGE_500DPS - for 500 degrees-per-second range
  • L3DS20_RANGE_2000DPS - for 2000 degrees-per-second range

Example:

Download: file
void setup() 
{
  Serial.begin(9600);
  
  // Try to initialise and warn if we couldn't detect the chip
  if (!gyro.begin(gyro.L3DS20_RANGE_250DPS))
  {
    Serial.println("Oops ... unable to initialize the L3GD20. Check your wiring!");
    while (1);
  }
}

Sensing Rotation:

To sense rotation, you must first call the "read()" function to take a reading:
  • void read(void);
This function takes no parameters.  After calling "read()".  The raw x, y and z readings can be retrieved from the device object's "data" member.

  • data.x - x-axis rotation rate in degrees-per-second
  • data.y - y-axis rotation rate in degrees-per-second
  • data.z - z-axis rotation rate in degrees-per-second

Example:
Download: file
void loop() 
{
  gyro.read();
  Serial.print("X: "); Serial.print((int)gyro.data.x);   Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)gyro.data.y);   Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)gyro.data.z); Serial.print(" ");
  delay(100);
}

Alternate Units:

The values reported by the read() function are in degrees-per-second (dps)  For some calculations, it may be more convenient to work in radians.  To convert dps to radians-per-second (rad/s), simply multiply by 0.017453293 as in the following code:
Download: file
#define SENSORS_DPS_TO_RADS               (0.017453293F)          /**< Degrees/s to rad/s multiplier */

void loop() 
{
  gyro.read();
  Serial.print("X: "); Serial.print((int)gyro.data.x * SENSORS_DPS_TO_RADS);   Serial.print(" ");
  Serial.print("Y: "); Serial.print((int)gyro.data.y * SENSORS_DPS_TO_RADS);   Serial.print(" ");
  Serial.print("Z: "); Serial.println((int)gyro.data.z * SENSORS_DPS_TO_RADS); Serial.print(" ");
  delay(100);
}

Calibration:

The L3GD20 is calibrated at the factory to close tolerances and will provide sufficient accuracy for most applications.

For critical applications where maximum accuracy is required, the gyro should be calibrated for zero-rate and sensitivity. For detailed information on how to calibrate a MEMS gyro, please refer to section 5.3 of this technical article.
The L3GD20 includes an on-chip temperature sensor, but this sensor isn't intended to be used to measure ambient temperature. The register seems to be used to provide a temperature offset for the die temperature to account for variation across multiple gyros or over time, but the value this offset is relative to unfortunately isn't specified in the datasheet, so there is no obvious way to use it in a useful manner as-is.
This guide was first published on Jan 02, 2013. It was last updated on Jan 02, 2013. This page (Arduino) was last updated on Nov 13, 2019.