Play Raspberry Pie Together (18) - Application of MPU6050 Gyro Acceleration Sensor Module

Play Raspberry Pie Together (18) - Application of MPU6050 Gyro Acceleration Sensor Module

I. Introduction

Smartphones are now very powerful. In addition to the basic communication functions, location and speed measurement, spatial angle measurement is also very convenient, which is very important in applications such as line navigation, maps, somatosensory games and so on. I don't know if you ever thought about how smart devices can get data about their spatial state and acceleration? MPU6050 is a sensor module that provides such data measurements.

First, it is not complicated to read the data of MPU6050 sensor module through raspberry pie. MPU60X0 is a highly scalable digital motion processor. We used the encapsulated module in this experiment, as shown in the following figure:

The MPU6050 chip itself has 24 pins, with 6 pins on each side. It has more functions. The pin functions and positioning are shown in the following figure:

For advanced usage of MPU6050, you can find it by looking at the chip manual. This blog focuses on its application in the raspberry pie. We just need to focus on the eight pins of the module we are using.

2. Connection and preparation

The module we used in this experiment has 8 pins, only 4 of which are needed to achieve the function. For the application of Raspberry Pie I2C protocol bus, this series of blogs has been experimented with a lot of applications before. First connect the sensor module to the raspberry pie as follows:

Sensor moduleraspberry pi
VCC+5V
GNDGND
SCLSCL
SDASDA

Before you start using the I2C bus, don't forget to turn on the I2C function of the raspberry pie. If you don't know much about the I2C bus usage, you can read the following blog:

https://my.oschina.net/u/2340880/blog/5142788

When the connection is complete, enter the following command at the Raspberry Pie terminal to view the connected I2C devices:

sudo i2cdetect -y 1









If the connection is correct, the terminal output will be as follows:

As you can see, the 68 shown in the diagram is the address of the sensor to which we are connected.

Now it's time to understand how the MPU6050 sensor is used. We know that through the I2C bus, we can easily read and write data to a register in the device. Therefore, the core of using the MPU6050 sensor is to understand how the registers are used. We will use several registers as examples, and complete register usage is available from the chip manual.

1. Power management registers

The address of the power management register is 107, corresponding to the hexadecimal number 0x6b. It is an 8-bit register, of which the fourth bit is reserved and cannot be used. The functions are as follows:

DECIVE_RESET: This bit resets the sensor, resets all registers inside the sensor to their initial state, and when reset is complete, this bit clears.

SLEEP: Sleep control bit, when this bit is 1, the sensor will be in sleep mode. It is important to note that the sensor starts in sleep mode by default and we need to wake it up manually.

CYCLE: When the device is set to non-sleep mode, the SLEEP bit is not 1. If set to 1, the sensor will be in cycle mode and the data will be sampled in cycle at the speed set by register 108.

TEMP_DIS: Disable the temperature sensor, set to 1, disable the temperature sensor.

CLKSEL:3-bit unsigned value used to instruct the clock source.

For the CLKSEL option, it is set as follows:

CLKSEL valueSignificance
0Using an internal 8MHz oscillator
1Frequency using gyroscope X-axis
2Frequency using gyroscope Y-axis
3Frequency using gyroscope X-axis
4Use external 32.768 kHz frequency
5Use external 19.2 MHz frequency
6Reserved Value
7Stop the clock and keep the oscillator reset

2. Gyro Data Register

The addresses of the gyroscope data registers are 67 to 72 with a total of 48 binary bits, as follows:

These registers are read-only and cannot be written to.

The 67 and 68 registers store 16-bit data, which is measured on the X-axis of the gyroscope. 68 registers store 8 bits lower and 67 registers store 8 bits higher.

69 and 70 registers store 16-bit data, which is the measurement data of the gyroscope's Y-axis. 70 registers store 8 bits lower and 69 registers store 8 bits higher.

The 71 and 72 registers store 16-bit data, which is the measurement data for the Z-axis of the gyroscope. 72 registers store 8 bits lower and 71 registers store 8 bits higher.

It is important to note that the data extracted from these registers is raw data. To obtain the true rotation angle measured by a gyroscope, conversion is required in units related to the total range, which is configured in register 27.

3. Gyro Configuration Register

The address of the gyro configuration register is 27, which is a readable and writable register as follows:

XG\u ST, YG\u ST and ZG\u The three bits ST are used to set up the gyroscope to perform self-test. FS\u The SEL is used to set the range of the gyroscope. The lower 3 bits are reserved bits and need not be used. FS_ The SEL can set a range of quantities as follows:

FS_SELRange RangeSensitivity parameters
0±250 °/s131
1±500 °/s65.5
2±1000 °/s32.8
3±2000 °/s16.4

For different range ranges, once we get the original data, we need to divide the corresponding sensitivity parameters to get the final gyro data.

4. Accelerator Data Register

The address of the accelerator data register is 59 to 64, with a total of 48 binary bits, as follows:

Similar to gyroscope data registers, the accelerometer needs register 28 to configure and set the range, and the true acceleration value is calculated by the sensitivity corresponding to the range.

5. Accelerator Configuration Register

The address of the gyro configuration register is 28, which is a readable and writable register as follows:

XA\u ST, YA\u ST, ZA\u ST are self-checking execution control bits, AFS\u The SEL is used to set the range and corresponds to the sensitivity as follows:

AFS_SELRange RangeSensitivity parameters
0±216384
1±4g8192
2±8g4096
3±16g2048

6. Temperature data registers

The address of the temperature data register is 65 to 66 with 16 binary bits as follows:

For the temperature data measured to be a signed number, the following calculation formulas are needed to obtain true Celsius data:

Celsius = TEMP_OUT / 340 + 36.53

In addition, there are many functional sensors, such as sampling rate configuration, which can be found in the chip manual.

3. Coding

Once you've finished the basic preparations, it's easy to write code to get the data for the sensor module, as follows:

#coding:utf-8

import smbus
import math
import time

# Power Control Register Address
power_regist = 0x6b

# I2C module initialization
bus = smbus.SMBus(1)
# Address of external I2C device
address = 0x68

# Encapsulate some function to read data

# Read data of one word length (16 bits)
def readWord(adr):
    high = bus.read_byte_data(address, adr)
    low = bus.read_byte_data(address, adr+1)
    val = (high << 8) + low
    return val

# Convert the read data to the source code (the signed number itself is stored as a complement)
def readWordReal(adr):
    val = readWord(adr)
    x = 0xffff
    # The first 1 indicates a negative number
    if (val >= 0x8000):
        # Find the source code
        return -((x - val)+1)
    else:
        return val

# Obtaining an angle with known acceleration
def dist(a, b):
    return math.sqrt((a*a)+(b*b))

def getRotationX(x, y, z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)

def getRotationY(x, y, z):
    radians = math.atan2(x, dist(y,z))
    return math.degrees(radians)

# Set power mode
bus.write_byte_data(address, power_regist, 0)


while True:
    time.sleep(0.5)
    print("Helix Data-----------")
    gyroX = readWordReal(0x43)
    gyroY = readWordReal(0x45)
    gyroZ = readWordReal(0x47)

    print("X Axis gyroscope raw data:", gyroX, "X Axis rotation per second:", gyroX/131)
    print("Y Axis gyroscope raw data:", gyroY, "Y Axis rotation per second:", gyroY/131)
    print("Z Axis gyroscope raw data:", gyroZ, "Z Axis rotation per second:", gyroZ/131)

    print("Acceleration data----------")
    accelX = readWordReal(0x3b)
    accelY = readWordReal(0x3d)
    accelZ = readWordReal(0x3f)

    print("X Axis acceleration raw data:", accelX, "X Axis acceleration:", accelX/16384)
    print("Y Axis acceleration raw data:", accelY, "Y Axis acceleration:", accelY/16384)
    print("Z Axis acceleration raw data:", accelZ, "Z Axis acceleration:", accelZ/16384)

    print("Celsius temperature data--------")
    temp = readWordReal(0x41)
    print("Temperature raw data:", temp, "Centigrade:", temp/340 + 36.53)

    print("Rotator Angle Data-------")
    print("X Axis rotation:", getRotationX(accelX/16384, accelY/16384, accelZ/16384))
    print("Y Axis rotation:", getRotationX(accelX/16384, accelY/16384, accelZ/16384))



Run the above code on the raspberry pie as shown in the following image:

Focus on technology, understand love, willing to share, be a friend

QQ: 316045346

Tags: Sensor microchip

Posted on Thu, 28 Oct 2021 13:00:45 -0400 by dwees