Lets work with MPU6050 (GY-521) — Part1

Kavindu Gimhan Zoysa
6 min readDec 25, 2017

--

In this part I am mainly discussing the basic functionalities of MPU6050 and how to get the angular velocity and angular position using gyroscope. More advanced functionalities will be discussed later.

What is MPU6050 ?
MPU6050 is an integrated 6-axis Motion Tracking device that combines a 3-axis gyroscope, 3-axis accelerometer. It uses I2C to communicate with master
(In my case it is Arduino UNO). It contains 16-bits output channels. So we can get the measurement as a 16 bit value. Please refer datasheets of this device to get more details.

What do you need to test these examples ?
— MPU6050 (GY-521)
— Microcontroller (In my case it is Arduino UNO)
— Jumper wires
— Bread board

Lets Start …

Connection between Arduino and MPU6050

You can create circuit as shown in above diagram. SCL and SDA pins of MPU6050 is connected to the A5 and A4 pins of arduino. The reason to connect AD0 pin to GND pin will discuss soon. Since this device is using
I2C, we have to establish the communication between arduino and mpu6050.

MPU-6000 and MPU-6050 Product Specification Revision 3.3/sec. 9.2

According to the above discription,
if pin AD0 is logic low, address of the device is b1101000
if pin AD0 is logic high, address of the device is b1101001
So in my case it is b1101000. (AD0 is connected to GND). As shown in follwing code we can establish the I2C connection between master and slave.

Start transmission

Then we have to manage the power of module. For that we have to access register 6B as explained below description. (All the registers which are available and description of each register are mentioned in datasheet)

MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2/sec 4.28

Read the description carefully and set the bit values as you want. In my case I set the sleep bit as 0.

Power management

Now we are going to access register which are related to gyro and accelerometer sensors in the module. For the we have to configure 1B (gyro) and 1C(accelerometer) registers. Description for each register is shown below.

MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2/sec 4.4

By setting the FS_SEL value as shown above we can select the range which means the maximum range you can measure. In my case I set FS_SEL as 0 which means I can measure from -250deg/s to +250deg/s.

MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2/sec 4.5

In my case I set AFS_SEL as 0 which means I can measure from -2g to +2g.

There is one more important value which rises when we configure these two registers. It is Sensitivity Scale Factor.

MPU-6000 and MPU-6050 Product Specification Revision 3.4/sec 6.1 and 6.2

As shown above if we select FS_SEL=0, Sensitivity Scale Factor becomes 131 which means angular velocity of 1 deg/sec is given as 131 in gyro sensor. Therefore angular velocity (deg/sec) can be obtained by dividing sensitivity scale factor (in my case 131). Same method can be applied to the accelerometer. Since I set AFS_SEL=0, Sensitivity Scale Factor becomes 16384 which means 1g is given as 16384 in accelerometer. The code to configure register (1B and 1C) is shown below.

Configuration

Let’s read the sensor values. One reading of one axis has a 16 bits value. That means two registers. Therefore we have to access 6 registers (for 3 axis). Please read the description of registers 43–48 for gyro readings and register 3B-40 for accelerometer readings. Code to read the gyro readings is shown below.

Read gyro readings

By dividing these raw values from Sensitivity Scale Factor (131) we can obtain angular velocity with respect to each axis. The code is shown below.

Calculate angular velocity

Lets calculate angle of MPU6050 w.r.t. its’ starting position …

Before that we have to do some maths [Mathematics is everythig :-) .. ]. If angular velocity is monotonous, we can use

Calculate angular displacement for constant velocity

But in practical scenario, angular velocity is not a constant with time, it varies with the time. There for we can use,

Calculate angular displacement for changing velocity

We can calculate the angular displacement by integration the multiplication of angular velocity (at a given time) and small period of time over the given time (t). Now the question how can we implement this method programatically ? To understand that lets interpret above method graphically.

Graph for constant angular velocity

According to the above graph we can get the angular displacement by calculating the area. As I mentioned earlier, in real scenario, angular velocity is not a constant. therefore graph looks like,

Graph for constant angular velocity

we can get the angular displacement by calculating area OXYZ. So the next question is how can we calculate the area which does not have a shape ? Simple answer is that divide it into small pieces (which has a shape that can be calculated), calculate area and integrate all values. It is shown below.

Calculate angle

Full code is available on GitHub, please go through it and read the comments carefully, then you can realise the things I discussed here clearly.

Important …
At the beginning of the code we have to do a calibrations, because when the mpu is not moving there can be values. We have to remove these values from our readings. For that we get the average of this error and subtract it from our reading.

Calibration

Conclusion …

By reading this article you can get the basic knowledge of MPU6050 and how to calculate angular velocity, angular motion, and acceleration.

These output values are not 100% correct. On next part (part2), I will discuss how to get more accurate values using more advanced methods.

Reference

http://www.alldatasheet.com/datasheet-pdf/pdf/517744/ETC1/MPU-6050.html
https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf
https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf

--

--