STM32-MPU6050-MPU9250-I2C-SPI icon indicating copy to clipboard operation
STM32-MPU6050-MPU9250-I2C-SPI copied to clipboard

STM32 MPU6050/9250 I2C and SPI interface. Sensor fusion using a complementary filter yields sensor Euler angles and is implemented in both C and CPP.

STM32-MPU6050-MPU9250-I2C-SPI

Based on my original library, the algorithms and IC communication has been ported for STM32 chips using HAL drivers.

Example code was tested with the Sparkfun MPU9250 breakout board and Nucleo-F401RE dev board.

There are four examples available.

  • I2C using CPP
  • I2C using C
  • SPI using CPP (MPU9250 only)
  • SPI using C (MPU9250 only)

The examples are fundamentally the same with minor differences such as the use of data structures, pointers, printing methods, etc...

For just the libraries refer here.

Notes / Usage

  • Download STM32CubeIDE and create a new project based on your hardware (or import the example projects if the same dev board is available).

  • Select: Project Manager -> Code Generator -> Check: Generate peripheral initialization as a pair of '.c/.h' files per peripheral.

  • Add provided desired header and source files into their respective \Src and \Inc directories.

  • Parameters required by library:

    • Hardware information (e.g. hi2cX or hspiX)
    • Gyroscope full scale range (default 500 deg/s)
    • Accelerometer full scale range (default 4 g)
    • Delta time (default 0.004 s (250 Hz) -> use a timer interrupt)
    • Time constant (default 0.98)
  • The minimum functions required after initializing are: begin(), calibrate(), and attitude()

  • Data can be printed from a serial port by connecting to the hardware at 115200 baud

  • Refer to examples for more information

Pinout

Refer to the .ioc file and the pinout diagrams located here.

SPI

MPU9250 STM32F401RE Breakout board comment
VDD 3V3
VDDIO 3V3 Shorted to VDD on PCB
GND GND
AD0/SDO PB4 (MISO) Adjust default solder jumper
SDA/SDI PB5 (MOSI)
SCL/SCLK PB3 (SCK)
nCS PB6 Chip select enable = Low

I2C

MPU9250 STM32F401RE Breakout board comment
VDD 3V3
GND GND
SDA PB9 (SDA) 10 kohm pull-up limit freq to 100 KHz
SCL PB8 (SCL) 10 kohm pull-up limit freq to 100 KHz

TODO

  • WHO_AM_I check failure values?
  • Code could use a little tidy (e.g. remove global variables, make it simpler to initialize, etc...)