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);
I2C Example: (use with I2C wiring)
// No need to specify pins for I2C Adafruit_L3GD20 gyro();
// 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:
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);
-
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:
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:#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.
Text editor powered by tinymce.