Library 43- MPU-6050 6-axes gyro and accelerometer for STM32F4

MPU-6050 6-axes gyroscope and accelerometer

MPU-6050 6-axes gyroscope and accelerometer

MPU-6050 is a 3-axes accelerometer and 3-axes gyroscope MEMS sensor in one piece. It can also measure temperature. It is very cheap device but also very powerful. It can measure simultaneously three (X, Y and Z) channels for accelerometer and gyroscope at the same time with 16-bit resolution. This chip is also compatible with MPU9150, except that MPU9150 has 3axes magnetometer (or compass) included.

MPU-6050 Features

  • Accelerometer
    • 4 selectable full scales (2G, 4G, 8G and 16G)
  • Gyroscope
    • 4 selectable full scales (250°, 500°, 1000° and 2000°)
  • Temperature sensor
    • 1°C accuracy
  • I2C driven
    • Selectable LSB bit for I2C address, used with AD0 pin
      • AD0 low: I2C address is 0xD0
      • AD0 high: I2C address is 0xD2
    • Max SCL speed is 400kHz



  • Read accelerometer data
  • Read gyroscope data
  • Read temperature
  • Read everything above in one single function or separatelly
  • Use 2 independent MPU-6050 devices on the same I2C bus
    • Devices must have different AD0 pin state


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx I2C
  • TM
    • TM I2C
    • defines.h
MPU-6050 STM32F4 Description
SCL PA8 I2C clock line
SDA PC9 I2C data line
AD0 LSB address pin. If pin is high, address is 0xD2, otherwise 0xD0
VCC 3.3V power supply

By default, I2C3 is used for communication. If you want to change it, open defines.h file and edit lines:

Note: If you want to connect 2 MPU6050 devices, you have to connect them to the same I2C pins but with different AD0 pin on MPU6050 device.

Functions and enumerations

Example 1

  • One MPU-6050 device is connected to pins PA8 and PC9 on STM32F4
  • AD0 of MPU6050 is connected to GND, gives us address 0xD0
  • Read is performed every 500ms, displayed on USART1 (PB6, at 115200 baud) to the computer

Example 2

  • Two MPU-6050 devices are connected to STM32F4, both to pins PA8 and PC9
  • First device has AD0 pin connected to GND, gives us 0xD0 address
  • Second device has AD0 pin connected to 3.3V, gives is 0xD2 address
  • Both devices are send to USART

Both projects are available on Github, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...