#include <Wire.h>
#include <MPU6050.h>
// Create an MPU6050 object
MPU6050 mpu;
// Variables to hold accelerometer and gyroscope values
int16_t ax, ay, az;
int16_t gx, gy, gz;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize I2C communication
Wire.begin();
// Initialize the MPU6050
mpu.initialize();
// Check if MPU6050 is connected
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
Serial.println("MPU6050 connection successful");
}
void loop() {
// Get raw accelerometer and gyroscope measurements
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print out the values
Serial.print("aX = "); Serial.print(ax); Serial.print(" ");
Serial.print("aY = "); Serial.print(ay); Serial.print(" ");
Serial.print("aZ = "); Serial.print(az); Serial.print(" ");
Serial.print("gX = "); Serial.print(gx); Serial.print(" ");
Serial.print("gY = "); Serial.print(gy); Serial.print(" ");
Serial.print("gZ = "); Serial.print(gz); Serial.println(" ");
// Wait a bit before the next loop
delay(500);
}