Library 28- L3GD20 3-axis digital gyroscope
MEMS sensor L3GD20 is included on STM32F429-Discovery board, so I made a library to use it. This is 3-axis digital gyroscope, so it can measure rotation in X, Y and Z axis. L3GD20 sensor works with SPI communication, or I2C. On Discovery board is connected for SPI mode, SPI5 is used for communication.
L3GD20 can be set to work in 3 different full scales: 250, 500 and 2000 dps.
Sensor has also possibility to measure temperature, but this is not included in this library.
L3GD20 Library
Features
- Measure 3 axis rotation
- Enables high pass filter to stabilize output
- Selectable 3 full scale modes
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- STM32F4xx SPI
- TM
- TM SPI
- TM GPIO
- defines.h
- TM SPI
Default pinout
L3GD20 | STM32F4 | Description |
---|---|---|
VCC | 3.3V | 3.3V power |
GND | GND | Ground |
MOSI | PF9 | Master Out Slave In for SPI |
MISO | PF8 | Master In Slave Out for SPI |
SCK | PF7 | Clock for SPI |
CS | PC1 | Chip select for SPI |
This pinout is used on F429-Discovery board by default. If you want to change your pinout for other application, add these lines in defines.h file and edit them:
1 2 3 4 5 6 7 8 |
/* Default SPI, used on STM32F429 Discovery board */ #define L3GD20_SPI SPI5 #define L3GD20_SPI_PINSPACK TM_SPI_PinsPack_1 /* Default CS pin on STM32F429 Discovery board */ #define L3GD20_CS_RCC RCC_AHB1Periph_GPIOC #define L3GD20_CS_PORT GPIOC #define L3GD20_CS_PIN GPIO_Pin_1 |
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/** * Main working struct * * Parameters: * - int16_t X * X axis rotation * - int16_t Y * Y axis rotation * - int16_t Z * Z axis rotation */ typedef struct { int16_t X; int16_t Y; int16_t Z; } TM_L3GD20_t; /** * Result enumerations * * Parameters: * - TM_L3GD20_Result_Ok * Everything ok * - TM_L3GD20_Result_Error * Error occured */ typedef enum { TM_L3GD20_Result_Ok, TM_L3GD20_Result_Error } TM_L3GD20_Result_t; /** * Enumeration for scale select * * Parameters: * - TM_L3GD20_Scale_250 * Set full scale to 250 mdps * - TM_L3GD20_Scale_500 * Set full scale to 500 mdps * - TM_L3GD20_Scale_2000 * Set full scale to 2000 mdps */ typedef enum { TM_L3GD20_Scale_250, TM_L3GD20_Scale_500, TM_L3GD20_Scale_2000 } TM_L3GD20_Scale_t; /* Public */ /** * Initialize L3GD20 sensor * * Parameters: * - TM_L3GD20_Scale_t scale * Working scale used * * Returns TM_L3GD20_Result_Ok is sensor detected, otherwise TM_L3GD20_Result_Error */ extern TM_L3GD20_Result_t TM_L3GD20_Init(TM_L3GD20_Scale_t scale); /** * Read rotation data from sensor * * Parameters: * - TM_L3GD20_t* L3DG20_Data * Pointer to TM_L3GD20_t struct * * Returns TM_L3GD20_Result_Ok */ extern TM_L3GD20_Result_t TM_L3GD20_Read(TM_L3GD20_t* L3DG20_Data); |
Example
Example below produces something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * Keil project for L3GD20 3x Gryo on STM32F429 Discovery board * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ #include "defines.h" #include "stm32f4xx.h" #include "tm_stm32f4_l3gd20.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_ili9341_ltdc.h" #include <stdio.h> int main(void) { /* L3GD20 Struct */ TM_L3GD20_t L3GD20_Data; char buffer[20]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Init ILI9341 with LTDC on STM32F429 Discovery board */ TM_ILI9341_Init(); TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); /* Fill layer 1 */ TM_ILI9341_Fill(ILI9341_COLOR_RED); /* Put some text */ TM_ILI9341_Puts(10, 10, "L3GD20 3-Axis\nGyroscope", &TM_Font_11x18, 0x0000, ILI9341_COLOR_RED); TM_ILI9341_Puts(90, 310, "stm32f4-discovery.net", &TM_Font_7x10, ILI9341_COLOR_BLACK, ILI9341_COLOR_RED); /* Init L3GD20 sensor */ if (TM_L3GD20_Init(TM_L3GD20_Scale_2000) != TM_L3GD20_Result_Ok) { /* Sensor error */ TM_ILI9341_Puts(10, 100, "Sensor ERROR!", &TM_Font_11x18, 0x0000, ILI9341_COLOR_RED); while (1); } while (1) { /* Read data */ TM_L3GD20_Read(&L3GD20_Data); /* Display data on LCD */ sprintf(buffer, "X rotation: %4d", L3GD20_Data.X); TM_ILI9341_Puts(10, 100, buffer, &TM_Font_11x18, 0x0000, ILI9341_COLOR_RED); sprintf(buffer, "Y rotation: %4d", L3GD20_Data.Y); TM_ILI9341_Puts(10, 122, buffer, &TM_Font_11x18, 0x0000, ILI9341_COLOR_RED); sprintf(buffer, "Z rotation: %4d", L3GD20_Data.Z); TM_ILI9341_Puts(10, 144, buffer, &TM_Font_11x18, 0x0000, ILI9341_COLOR_RED); } } |
View project on Github, download library below.
Recent comments