0

I am using MPU6050 on an Arduino Mega 2560 with Data logging shield v1.0 and an 8Gb Kingston Micro-SD HC card.I am facing a problem in logging and successfully writing to a file 100 values/sec. Now in my csv file i take only 30-35 values/sec. The delay should be (as i read in an other topic here) in "SD latency problem, which needs to be dealt using a FIFO buffer storing the incoming data in an ISR, while the SD is busy being written."

Can anyone help me? Thanks in advance.

    #include<Wire.h> 
    #include <SD.h> 
    #include <SPI.h> 
    #include <virtuabotixRTC.h> //Library used
    //RTC
    //Wiring SCLK -> 6, I/O -> 7, CE -> 8
    //Or CLK -> 6 , DAT -> 7, Reset -> 8
    virtuabotixRTC myRTC(6, 7, 8); 

    //FILE 
    File myFile;
    char myLogName[] = "MyLog.csv";
//Mega CS pin
const int chipSelect = 8;
//GYRO    
    const int MPU_addr=0x69;  // I2C address of the MPU-6050
    int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
 // Set the current date, and time in the following format:
 // seconds, minutes, hours, day of the week, day of the month, month, year
 //myRTC.setDS1302Time(0000, 23, 12, 1, 15, 7, 2019); //Here you write your actual time/date as shown above 
 //but remember to "comment/remove" this function once you're done
 //The setup is done only one time and the module will continue counting it automatically     
 //SD BEGIN      
 SD.begin(4); 
 myFile = SD.open(myLogName, FILE_WRITE);
 myFile.print("Date, Time, AcX, AcY, AcZ, GyX, GyY, GyZ");
 myFile.println();

 //WIRE BEGIN      
 Wire.begin();
 Wire.beginTransmission(MPU_addr);
 Wire.write(0x6B);  // PWR_MGMT_1 register
 Wire.write(0);     // set to zero (wakes up the MPU-6050)
 Wire.endTransmission(true);
 // Serial.begin(115200);
 myFile.close();
 }

 void loop()
 {
 //UPDATE TIME
 myRTC.updateTime();     
 //GYRO      
 Wire.beginTransmission(MPU_addr);
 Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
 Wire.endTransmission(false);
 Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C(ACCEL_XOUT_L)    
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)

      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)



      myFile.print(myRTC.hours,DEC);
      myFile.print(":");
      myFile.print(myRTC.minutes,DEC);
      myFile.print(":");
      myFile.print(myRTC.seconds,DEC);
      myFile.print(" , ");
      myFile.print(AcX);
      myFile.print(" , ");
      myFile.print(AcY);
      myFile.print(" , "); 
      myFile.print(AcZ);
      myFile.print(" , "); 
      myFile.print(GyX);
      myFile.print(" , "); 
      myFile.print(GyY);
      myFile.print(" , "); 
      myFile.println(GyZ);
      myFile.print(" , ");
      myFile.close(); //close file      
    }
  • 1
    Your question is way, waaaay too broad. You need to indicate which SD card you're using, as well as the library, provide the relevant code from your project, along with specific info about the throughput you are getting from your code vs what you need. You should also provide a link to the "other topic here" that discusses using a FIFO buffer and an ISR. – Duncan C Aug 25 '19 at 10:59
  • 1
    myFile gets closed but not opened in loop. There is no reason to open and close the file in loop. Just leave it open – chrisl Aug 25 '19 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.