Untitled

 avatar
unknown
plain_text
5 months ago
1.4 kB
2
Indexable
#include <Wire.h>
#include <MPU6050.h>

MPU6050 imu;  // Create an MPU6050 object

void setup() {
  Serial.begin(115200);      // Initialize serial communication
  Wire.begin();               // Initialize I2C communication
  
  // Initialize the MPU6050 sensor
  imu.initialize();
  if (!imu.testConnection()) {
    Serial.println("MPU6050 connection failed");
    while (1);  // Halt if there's no connection to the IMU
  }
  Serial.println("MPU6050 connected successfully");
}

void loop() {
  // Variables to hold IMU data
  int16_t ax, ay, az;
  int16_t gx, gy, gz;

  // Read acceleration and gyroscope data
  imu.getAcceleration(&ax, &ay, &az);
  imu.getRotation(&gx, &gy, &gz);

  // Convert raw data to G and °/s for easier interpretation
  float ax_g = ax / 16384.0;  // Convert to G
  float ay_g = ay / 16384.0;
  float az_g = az / 16384.0;
  
  float gx_dps = gx / 131.0;  // Convert to degrees per second
  float gy_dps = gy / 131.0;
  float gz_dps = gz / 131.0;

  // Print data in CSV format: ax, ay, az, gx, gy, gz
  Serial.print(ax_g, 4); Serial.print(",");
  Serial.print(ay_g, 4); Serial.print(",");
  Serial.print(az_g, 4); Serial.print(",");
  Serial.print(gx_dps, 4); Serial.print(",");
  Serial.print(gy_dps, 4); Serial.print(",");
  Serial.println(gz_dps, 4);  // End with newline

  delay(100);  // Delay to control the rate (adjust as needed)
}
Editor is loading...
Leave a Comment