Home » BeagleBone Black » Beaglebone Black MPU6050 Interfacing: I2C Tutorial Part 2
beaglebone black mpu6050 tutorial

Beaglebone Black MPU6050 Interfacing: I2C Tutorial Part 2

We covered the Beaglebone Black I2C basics in our last tutorial. This time, we'll try to communicate with a device through the I2C protocol. This device, the MPU6050, is a gyroscope + accelerometer integrated circuit that is very popular among makers and enthusiasts.

Beaglebone Black MPU6050 Wiring Diagram

First thing you need to do, of course, is connect the MPU6050 to the Beaglebone Black. We will be using I2C-2 bus of the BBB (as per the previous tutorial) and so our SDA pin will be P9_20 while our SCL pin will be P9_19. Also, connect the MPU6050 VCC pin to the P9_4 (3.3 V VDD) and its ground pin to P9_2 (DGND). Here's the fritzing diagram:

beaglebone black mpu6050 wiring

You can check if your connections are correct using the command i2cdetect -r 2:

You'll see that address 68 is filled out. That's the MPU6050!

The MPU6050 Registers

Accelerometer and Gyroscope

The MPU6050 has registers from 0x0D to 0x75. Each of these registers are 8-bits wide and have specific functions. To see all of these registers, download the MPU6050 register map.

For the purpose of this tutorial, we'll be reading from the accelerometer and gyroscope registers. The accelerometer and gyroscope data are 16 bits wide and so data from each axis uses two registers. Here's what I'm talking about:

 

Register Address
ACCEL_XOUT_H 0x3B
ACCEL_XOUT_L 0x3C
ACCEL_YOUT_H 0x3D
ACCEL_YOUT_L 0x3E
ACCEL_ZOUT_H 0x3F
ACCEL_ZOUT_L 0x40
GYRO_XOUT_H 0x43
GYRO_XOUT_L 0x44
GYRO_YOUT_H 0x45
GYRO_YOUT_L 0x46
GYRO_ZOUT_H 0x47
GYRO_ZOUT_L 0x48

We can check out the contents of these registers through i2cdump:

Here you'll see that the registers on the table above have zero values. This is because we need to wake the MPU6050 up before it gives data.

How to Wake Up the MPU6050

To wake up the MPU6050, we need to write zero to the PWR_MGMT_1 register which is at address 0x6B. Here's how to do that:

After that, we can use i2cdump again to check.

The accelerometer and gyroscope registers now have values!

Try reorienting the MPU6050 and do the i2cdump again. You should have different values on the accelerometer and gyroscope registers.

Getting Individual Values

To get an individual register value, we can use the i2cget command. For example, I'll read the value of ACCEL_XOUT_H register (address 0x3B):

The output 0x30 is the high byte of the MPU6050's acceleration in the x-axis. To get the low byte simply change the address:

The low byte is 0xf8. This means the acceleration in the x-axis is 0x30f8 or 12, 536 in decimals.

Scaling the MPU6050 Raw Data

What we've read from the MPU6050 so far is raw data and needs to be converted to make sense. The acceleration data is measured in g (acceleration due to gravity) units while the gyroscope data is measured in degrees per second. The MPU6050 uses 2g and 250 deg/sec as default sensitivities.

Since each axes is 16 bytes and is signed, the raw values will range from -32,768 to 32, 767. This means acceleration in axes X ,Y and Z should have values within these ranges that represent the effect of gravity. For the default sensitivity which is 2g, the scaling factor should be around 16, 384 (32, 768/2).

In the same way, the X, Y and Z gyro data should have raw data readings from +250 to -250. According to the datasheet, the best scaling factor to use is 131.

However, even with scaling factors you may not get the exact acceleration and rotational speed since there will always be noise and sensor reading errors.

C++ Code

Following the discussion above, we can now write a C++ code that displays the accelerometer and gyroscope values:

You can now use your MPU6050 to create motion-based apps for the Beaglebone Black!

If you find this post useful, kindly post a comment below. Come back to this site for more updates!

Leave a Reply

Your email address will not be published. Required fields are marked *