Index>Robot Kit>Alter All in One Kit for RPi>Lesson 8 How to Read the Data of MPU6050
No related product information!

Lesson 8 How to Read the Data of MPU6050

1977

    In this lesson, we will learn how to read the data of MPU6050.

8.1 Components used in this course


image.png



8.2 The introduction of MPU6050


MPU-6050 is the world's first integrated 6-axis MotionTracking device. It integrates a 3-axis MEMS gyroscope, a 3-axis MEMS accelerometer, and a scalable digital motion processor DMP (Digital Motion Processor). We can use an I2C interface to connect a third-party digital sensor, such as a magnetometer. After expansion, it can output a 9-axis signal through its I2C or SPI interface (SPI interface is only available on MPU-6000). MPU-6050 can also connect non-inertial digital sensors through its I2C interface, such as pressure sensors.

The MPU-6050 uses three 16-bit ADCs for the gyroscope and accelerometer respectively, and it converts the analog quantity into a digital quantity that can be output. The gyroscope can measure angular velocity, and the accelerometer can measure acceleration. In order to accurately track fast and slow movements, the measurement range of the sensor is controllable. The gyro measurement range is ±250, ±500, ±1000, ±2000°/sec (dps), and the accelerometer measurement range is ±2 , ±4, ±8, ±16g (gravity acceleration).

The MPU6050 has an on-chip 1024-byte FIFO, which helps reduce system power consumption. It communicates with all device registers using 400kHz I2C interface. In addition, a temperature sensor and an oscillator with only ±1% variation in the working environment are embedded on the MPU6050 chip. And there are programmable low-pass filters.


8.3 Wiring diagram (Circuit diagram)

    When the MPU6050 module is in use, it needs to be connected to the MPU6050 interface on the Robot HAT drive board, as shown in the figure below:

image.png


8.4 How to read the data of MPU6050 

8.4.1Run the program of this course

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:

cd adeept_alter/02CourseCode/01ComponentCode

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5. The 07MPU6050 folder stores the sample code of this course. Enter the command to enter this folder:

cd 07MPU6050

image.png


6. Enter the command to display the contents of the current directory:

ls

image.png


7.MPU6050.py is a python program.When using the MPU6050 module, we need to download the mpu6050-raspberrypi library and enter the following command:

sudo pip3 install mpu6050-raspberrypi

image.png


8.After the download is successful, we also need to enable the I2C function, need to enter raspi-config to set up, you need to enter the following command:

sudo raspi-config

image.png


9.Press the Enter key to confirm, enter the raspi-config configuration interface as follows, we can use the up, down, left, and right keys on the keyboard to operate, by the up and down arrow keys we select the fifth item: Interfacing Options: configure the connection of peripheral devices. Select "<select>" by the left and right arrow keys and press Enter to confirm.

image.png


    10.Then continue to enter the "Interfacing Options" interface, we select the fifth item "P5 I2C", select "<select>" with the left and right arrow keys, and press Enter to confirm.

image.png


11.Then the options in the figure below will pop up, we select "<Yes>" with the left and right arrow keys:

image.png


12. Select "<Yes>", press Enter to confirm, an option box will pop up, continue to press Enter to confirm:

image.png


13. After confirming, it will jump back to the main interface. We select "<Finish>" with the left and right arrow keys, and press the enter key to confirm. At this time, then we will return to the command window after completing the settings.

image.png


14. Directly enter the following commands to run MPU6050.py on the Raspberry Pi:

sudo  python3  MPU6050.py

image.png


15. After running the program successfully, you will observe some data. The data from left to right are the data of the X, Y, and Z axis of the MPU6050 sensor.

image.png


16.If you want to terminate the running program, you can press the shortcut key Ctrl+C on the keyboard.



8.4.2 The main code program of this lesson

After the above hands-on practice, you already know how to use and run our course sample code program. You must be curious about how our code program is programmed to read the data of MPU6050 on the Raspberry Pi. Let’s get to know the main code program.Here we use Sublime IDE to view and edit the code program of this lesson, please refer to the content of lesson 2 for specific methods.

1. Import the dependent libraries of MPU6050.

image.png 

 

2. Obtain data from MPU6050 sensor.

image.png 


3. Read the data of X, Y, Z axis respectively.

image.png 


4. Print out the acquired X, Y, Z axis data every 0.5s.

image.png