-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU6050_test.ino
85 lines (70 loc) · 2.84 KB
/
MPU6050_test.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high
int16_t ax, ay, az;
int16_t gx, gy, gz;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(9600);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// use the code below to change accel/gyro offset values
Serial.println("Updating internal sensor offsets...");
// -640, 2047, 4926, -33, -8, 56
accelgyro.setXAccelOffset(-640);
accelgyro.setYAccelOffset(2047);
accelgyro.setZAccelOffset(4926);
accelgyro.setXGyroOffset(-33);
accelgyro.setYGyroOffset(-8);
accelgyro.setZGyroOffset(56);
Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t");
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t");
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t");
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t");
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t");
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t");
Serial.print("\n");
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
// display tab-separated accel/gyro x/y/z values
Serial.print("Acceleration: ");
Serial.print(ax / 16384.0); // Divide by sensitivity scale factor
Serial.print(", ");
Serial.print(ay / 16384.0);
Serial.print(", ");
Serial.print(az / 16384.0);
Serial.print("g; ");
Serial.print("Rotation: ");
Serial.print(gx / 131.0); // Divide by sensitivity scale factor
Serial.print(", ");
Serial.print(gy / 131.0);
Serial.print(", ");
Serial.print(gz / 131.0);
Serial.println("°/s");
delay(1000);
}