About Data logging of MPU6050 - Triple Axis Gyro Accelerometer Module

Hi!
I am using SD Card Shield with Arduino UNO for logging the data from MPU6050 - Triple Axis Gyro Accelerometer Module. I used SDA and SCL (A5, A4) for the MPU6050. My complete code is given below. I am not able to read the MPU chip when using with SD card inside the shield. However, when I remove the SD card it starts to read the data from MPU6050. Please help me to resolve this issue.

#include <SPI.h>
#include <SD.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

File myFile; //for SD Card Reading

// change this to match your SD shield or module;
const int chipSelect = 10;

Adafruit_MPU6050 mpu; 

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  
// open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("MPU6050.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("Ax, Ay, Az, Gx, Gy, Gz");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

// Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }

  // set accelerometer range to +-8G
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

  // set gyro range to +- 500 deg/s
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);

  // set filter bandwidth to 21 Hz
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

  delay(100);
  
}

void loop()
{
  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
  Serial.print(a.acceleration.x);
  Serial.print(",");
  Serial.print(a.acceleration.y);
  Serial.print(",");
  Serial.print(a.acceleration.z);
  Serial.print(", ");
  Serial.print(g.gyro.x);
  Serial.print(",");
  Serial.print(g.gyro.y);
  Serial.print(",");
  Serial.print(g.gyro.z);
  Serial.println("");

  delay(10);

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("MPU6050.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println(a.acceleration.x);
    myFile.println(",");
    myFile.println(a.acceleration.y);
    myFile.println(",");
    myFile.println(a.acceleration.z);
    myFile.println(",");
    myFile.println(g.gyro.x);
    myFile.println(",");
    myFile.println(g.gyro.y);
    myFile.println(",");
    myFile.println(g.gyro.z);
    
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
1 Like

What have you tried?

Not the current issue, but you make the classic mistake of opening the file, writing a tiny bit of data, and closing it again. This reduces writing speed to a crawl, and vastly increases the SD card error rate.

Open the file once in setup() and close it ONLY when you are done collecting data. With an ill-considered infinite data collection loop, you never are done, right?

1 Like

Then, what should I do to store the data continuously without using infinite loop?

Do some actual planning for your project, like adding a data collection start/stop button.

Writing the data on SD card is not the issue. Arduino not detecting the MPU6050 chip when using the SD card reader is my issue.

You have multiple issues. You just haven't seen most of the problems yet.

Nor have you posted the information required to solve the most immediate one. Post a link to the shield, and a hand drawn wiring diagram. Post the complete error messages.

Answer the question in post #2.

Code shows my attempt to read and log the data.

So this is what you see on Serial Monitor?

Initializing SD card...initialization done.
Writing to test.txt...done.
Failed to find MPU6050 chip

What if you put the mpu.begin() part before

Serial.print("Initializing SD card...");

1 Like

Thank you. I will try this.

Again it is showing:

Failed to find MPU6050 chip
Failed to find MPU6050 chip

so do you have a working code ro not?
if yes pleas write it here it will be vera nice thx

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.