MPU6050 with Raspberry Pi
Measuring acceleration and rotation with a Raspberry Pi and MPU6050 IMU
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.
The MPU6050 uses the I2C interface.
|3-5 V supply voltage
|I2C clock connection
|I2C data connection
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.
|Name in code
|Writing to power management register
|Writing to sample rate register
|Writing to configuration register
|Writing to gyro configuration register
|Writing to interrupt enable register
|Reading X Accelerometer raw data
|Reading Y Accelerometer raw data
|Reading Z Accelerometer raw data
|Reading X Gyro raw data
|Reading Y Gyro raw data
|Reading Z Gyro raw data
Connections to Raspberry Pi
|Connects to RPi pin
|3.3 V power
|GPIO 3 (SCL)
|GPIO 2 (SDA)
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: