]> git.piffa.net Git - arduino/blob - sheets/gyro/Test program/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
cli intro
[arduino] / sheets / gyro / Test program / Arduino / MPU6050 / Examples / MPU6050_raw / MPU6050_raw.ino
1 // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
2 // 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
3 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
4 //
5 // Changelog:
6 //     2011-10-07 - initial release
7
8 /* ============================================
9 I2Cdev device library code is placed under the MIT license
10 Copyright (c) 2011 Jeff Rowberg
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 ===============================================
30 */
31
32 // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
33 // is used in I2Cdev.h
34 #include "Wire.h"
35
36 // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
37 // for both classes must be in the include path of your project
38 #include "I2Cdev.h"
39 #include "MPU6050.h"
40
41 // class default I2C address is 0x68
42 // specific I2C addresses may be passed as a parameter here
43 // AD0 low = 0x68 (default for InvenSense evaluation board)
44 // AD0 high = 0x69
45 MPU6050 accelgyro;
46
47 int16_t ax, ay, az;
48 int16_t gx, gy, gz;
49
50 #define LED_PIN 13
51 bool blinkState = false;
52
53 void setup() {
54     // join I2C bus (I2Cdev library doesn't do this automatically)
55     Wire.begin();
56
57     // initialize serial communication
58     // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
59     // it's really up to you depending on your project)
60     Serial.begin(38400);
61
62     // initialize device
63     Serial.println("Initializing I2C devices...");
64     accelgyro.initialize();
65
66     // verify connection
67     Serial.println("Testing device connections...");
68     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
69
70     // configure Arduino LED for
71     pinMode(LED_PIN, OUTPUT);
72 }
73
74 void loop() {
75     // read raw accel/gyro measurements from device
76     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
77
78     // these methods (and a few others) are also available
79     //accelgyro.getAcceleration(&ax, &ay, &az);
80     //accelgyro.getRotation(&gx, &gy, &gz);
81
82     // display tab-separated accel/gyro x/y/z values
83     Serial.print("a/g:\t");
84     Serial.print(ax); Serial.print("\t");
85     Serial.print(ay); Serial.print("\t");
86     Serial.print(az); Serial.print("\t");
87     Serial.print(gx); Serial.print("\t");
88     Serial.print(gy); Serial.print("\t");
89     Serial.println(gz);
90
91     // blink LED to indicate activity
92     blinkState = !blinkState;
93     digitalWrite(LED_PIN, blinkState);
94 }