#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// Define your I2C pins here
const int SDA_PIN = 18; // Replace with your SDA pin
const int SCL_PIN = 19; // Replace with your SCL pin
// Create an MPU6050 object
Adafruit_MPU6050 mpu;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize I2C communication
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Set up the accelerometer and gyroscope range
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.println("MPU6050 ready!");
}
void loop() {
// Read the acceleration and gyro values
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Print the acceleration values to the serial monitor
Serial.print("Accel X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.println(a.acceleration.z);
// Print the gyro values to the serial monitor
Serial.print("Gyro X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.println(g.gyro.z);
// Small delay before the next reading
delay(100);
}