IMU with Mega 2560 and SD

Hello Everyone,

I am the beginner of the arduino.

I am trying to use the mega 2560 R3 and SD shield to store the data.

SD shield with RTC

I used the PC software to set the frequency at 50Hz which provided by the IMU. In the PC datalogging function, i could able to store 500 data at 10s (0.02s/per data).

However, when i adopted Arduino Mega 2560 R3, i only can save 50+ data in 10s.

Here is my code.

#include "N1Lib.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
N1Lib N1info;

U8 inChar[100];
U8 i;
U32 Succpack;

F32 pitch;
F32 roll;
F32 yaw;
F32 AccX;
F32 AccY;
F32 AccZ;
F32 AirHeight;

const int chipSelect = 4;
File dataFile;
void setup() {
  
    Serial.begin(115200);
    Serial1.begin(115200);
    Serial.print("Initializing SD card...");
    if (!SD.begin(10, 11, 12, 13)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  // put your main code here, to run repeatedly:

 while(Serial1.available()) {
        inChar[i] =  Serial1.read();
        if (i ==49) {
          i=0;
          Succpack = N1info.SuccPackageCnt();
          N1info.N1_RawDataRx( &inChar[0], 50);
        }
        else i++; 
  }
  if(N1info.SuccPackageCnt()!=Succpack){
      pitch = N1info.N1_GetPitch();
      roll = N1info.N1_GetRoll();
      yaw = N1info.N1_GetYaw();
      AccX = N1info.N1_GetAccX();
      AccY = N1info.N1_GetAccY();
      AccZ = N1info.N1_GetAccZ();
      AirHeight = N1info.N1_GetAirHeight();
     File dataFile = SD.open("IMU.txt", FILE_WRITE);
      if (dataFile) {
          dataFile.print(pitch);
          dataFile.print("      ");
          dataFile.print(roll);
          dataFile.print("      ");
          dataFile.print(yaw);
          dataFile.print("      ");
          dataFile.print(AccX);
          dataFile.print("      ");
          dataFile.print(AccY);
          dataFile.print("      ");
          dataFile.print(AccZ);
          dataFile.print("      ");
          dataFile.println(AirHeight);
          dataFile.close();
     }
      else 
        Serial.println("error opening IMU.txt");
  }
  }

Here is my question.

How can i write the data to SD through Mega 2560 which have same data no of the PC (500data at 10s)?
I have searched some information about fast write. There is a lib call SdFat-master. Can it apply in Mega 2560??

Thank you for your kindly help.

Yes, you should lower your expectations.

I think you can do better than 500 bytes/sec, though. You could improve things a lot by opening the dataFile just once, in setup. Every once in a while in loop, say once per second, do

    dataFile.flush()

It's also a good idea to close the file before power-down or pulling the card. Is there a button you can use? Or a condition, like not moving for 10 seconds?

Did you know that some writes to the SD card can take 100ms or more? This will cause ~30 (or more) characters to get dropped. Is it ok to lose a few IMU records? If not, you'll have to increase the serial buffer OR handle the received characters in an interrupt.

Cheers,
/dev