|
| 1 | +#include <Adafruit_MPU6050.h> |
| 2 | +#include <Adafruit_Sensor.h> |
| 3 | +#include <Wire.h> |
| 4 | +Adafruit_MPU6050 mpu; |
| 5 | +void setup(void) { |
| 6 | + Serial.begin(115200); |
| 7 | + // Try to initialize! |
| 8 | + if (!mpu.begin()) { |
| 9 | + Serial.println("Failed to find MPU6050 chip"); |
| 10 | + while (1) { |
| 11 | + delay(10); |
| 12 | + } |
| 13 | + } |
| 14 | + Serial.println("MPU6050 Found!"); |
| 15 | + // set accelerometer range to +-8G |
| 16 | + mpu.setAccelerometerRange(MPU6050_RANGE_8_G); |
| 17 | + // set gyro range to +- 500 deg/s |
| 18 | + mpu.setGyroRange(MPU6050_RANGE_500_DEG); |
| 19 | + // set filter bandwidth to 21 Hz |
| 20 | + mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); |
| 21 | + delay(100); |
| 22 | +} |
| 23 | + |
| 24 | +void loop() { |
| 25 | + /* Get new sensor events with the readings */ |
| 26 | + sensors_event_t a, g, temp; |
| 27 | + mpu.getEvent(&a, &g, &temp); |
| 28 | + /* Print out the readings */ |
| 29 | + Serial.print("Acceleration X: "); |
| 30 | + Serial.print(a.acceleration.x); |
| 31 | + Serial.print(", Y: "); |
| 32 | + Serial.print(a.acceleration.y); |
| 33 | + Serial.print(", Z: "); |
| 34 | + Serial.print(a.acceleration.z); |
| 35 | + Serial.println(" m/s^2"); |
| 36 | + Serial.print("Rotation X: "); |
| 37 | + Serial.print(g.gyro.x); |
| 38 | + Serial.print(", Y: "); |
| 39 | + Serial.print(g.gyro.y); |
| 40 | + Serial.print(", Z: "); |
| 41 | + Serial.print(g.gyro.z); |
| 42 | + Serial.println(" rad/s"); |
| 43 | + Serial.print("Temperature: "); |
| 44 | + Serial.print(temp.temperature); |
| 45 | + Serial.println(" degC"); |
| 46 | + Serial.println(""); |
| 47 | + delay(500); |
| 48 | +} |
0 commit comments