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

Default pinout

Default pinout for L3GD20 sensor on F429-discovery board
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:

Functions and enumerations

Example

Example below produces something like this:

L3GD20 3-axis digital gyroscope example

L3GD20 3-axis digital gyroscope example



View project on Github, download library below.

Icon
TM STM32F4 L3GD20 Library

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Hui Xiong

    in the file L3GD20.c,

    /* Enable high-pass filter */

    TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG5, 0x01);//

    The value should be 0x10?

    • You are right.
      I’ve fixed this right now.

      Thanks for posting.

  • mdk

    How convert raw data to useful angles?

    • This is on you now to read about this on Google.
      I’ve made for you data available to use.

      • mdk

        OK. Thank You for good library.

        • MaKkEtRoNiX

          Hey there. Do you have a final working code?

      • MaKkEtRoNiX

        Do you know of any resources on this topic?

  • guillem

    Hi, I’m trying to port your library to my stm32f411e discovery with the cpu stm32f411ve.

    I have modified the defines.h to the pins in my board, and i have defined the function TM_SPI_InitCustomPinsCallback.

    I’m using only the SPI1 at this moment.

    SystemInit(); initialize systemcoreclock to 168MHz.
    When the program check if the sensor if connected returns TM_L3GD20_Result_Error.
    TM_L3GD20_INT_ReadSPI(L3GD20_REG_WHO_AM_I) returns 0.

    Where can be the problem?

  • Mark

    Hi,

    I have used this code on my discovery F0 with HAL drivers. Only a few lines need te be changed, the SPI read and write. Something strange happened it costs me hours to debug.
    To transmit the adress and data you can use 1 transmit action with 2 bytes of data:

    TxData[0] = address;
    TxData[1] = data;
    HAL_SPI_Transmit(&hspi2, TxData, 2, HAL_MAX_DELAY)

    This works however the read command:
    HAL_SPI_Receive (&hspi2, &RxData, 1, HAL_MAX_DELAY)

    read nothing but 0xFF after the transmit of 2 bytes.
    When splitting the transmit into 2 lines:

    HAL_SPI_Transmit(&hspi2, &address, 1, HAL_MAX_DELAY);
    HAL_SPI_Transmit(&hspi2, &data, 1, HAL_MAX_DELAY);

    It does work. My logic analyzer does not see any difference. Why does the transmit of 2 bytes corrupt the receive?

    • Igor Chudakov

      Mark,
      In original code CS signal controlled programatically and becomes active for each sent byte and passive between them. Could it be that transmitting 2 bytes of code makes it active for both bytes?
      L0 has two modes of SPI with HW controlled CS: TI and Motorola. The difference between them exactly how CS controlled. I did not work with F0 yet.