Chapter 25 Attitude Sensor MPU6050
264
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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"
);
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...