MPU6050 with Raspberry Pi

Article thumbnail

Published

raspberry-pi sensors

Measuring acceleration and rotation with a Raspberry Pi and MPU6050 IMU

About MPU6050

The MPU6050 consists of a 3-axis gyroscope and 3-axis accelerometer. It also contains a DMP (digital motion processor) to perform complex calculations, which frees up the controller to do other things.

The MPU6050 has a 16-bit ADC (analog to digital) chip. Because of this, it can receive motion in all three planes.

MPU6050 axes

The MPU6050 uses the I2C interface.

MPU6050 Pinout

PinDescription
VCCV_{CC}3-5 V supply voltage
GNDGround connection
SCLI2C clock connection
SDAI2C data connection
XDASee below
XCL
AD0Address pin
INTInterrupt pin

XDA and XCL stand for auxiliary data and auxiliary clock, respectively. These pins can be used for interfacing with other I2C devices.

The address pin can be used to change the address of the MPU6050. (default is 0x68)

The interrupt pin is used to indicate that data is available for the microcontroller to read.

MPU6050 Registers

AddressName in codePurpose
0x6BPWR_MGMT_1Writing to power management register
0x19SMPLRT_DIVWriting to sample rate register
0x1ACONFIGWriting to configuration register
0x1BGYRO_CONFIGWriting to gyro configuration register
0x38INT_ENABLEWriting to interrupt enable register
0x3BACCEL_XReading X Accelerometer raw data
0x3DACCEL_YReading Y Accelerometer raw data
0x3FACCEL_ZReading Z Accelerometer raw data
0x43GYRO_XReading X Gyro raw data
0x45GYRO_YReading Y Gyro raw data
0x47GYRO_ZReading Z Gyro raw data

Connections to Raspberry Pi

MPU6050 pinConnects to RPi pin
VCCV_{CC}3.3 V power
GNDGND
SCLGPIO 3 (SCL)
SDAGPIO 2 (SDA)

Code

I2C needs to be enabled on your Raspberry Pi before running this code. See "ADS1115 with Raspberry Pi" for details.

This code prints the acceleration and gyro values on each of the three axes.

Getting Rotation from Degrees per Second

The gyroscope measures degrees per second (how many degrees it has rotated in a second, or speed of rotation). We can plug in deg/sec for rate and seconds for time, so (degrees per second) * (seconds) equals degrees in rotation.

Below is the code showing this: