Code
In this project, we will read the acceleration data and gyroscope data of MPU6050, and print them out.
C Code 25.1.1 MPU6050RAW
First observe the project result, and then analyze the code.
1. Use cd command to enter 25.1.1_MPU6050RAW directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/24.1.1_MPU6050RAW
2. Use following command to compile "MPU6050RAW.c", "MPU6050.cpp" and "I2Cdev.cpp", and generate
executable file "MPU6050RAW".
gcc MPU6050RAW.cpp MPU6050.cpp I2Cdev.cpp -o MPU6050RAW
3. Then run the generated file "MPU6050RAW".
sudo ./MPU6050RAW
After the program is executed, the terminal will display the original acceleration and gyroscope data of
MPU6050, as well as the conversion to gravity acceleration and angular velocity as the unit of data. As shown
in the following figure:
The following is the program code:
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro; //instantiate a MPU6050 class object
int16_t ax, ay, az; //store acceleration data
int16_t gx, gy, gz; //store gyroscope data
void setup() {
// initialize device
printf("Initializing I2C devices...\n");
accelgyro.initialize(); //initialize MPU6050
// verify connection
printf("Testing device connections...\n");