Low data logging sample rate for MPU 6050

Hi,

I think the problem is it takes about 15 milliseconds to run the loop each time. Is there any way that anyone knows i can improve this? I’ve attached my code. Any help would be greatly appreciated.

I’ve tried a lot to find out about sampling onto SD cards at a high enough sampling rate.
I have an Arduino uno, MPU 6050, and Adafruit data logger. They’re all conected correctly and providing data which i can log to an SD card at a sampling rate of about 50Hz. This isn’t nearly fast enough for what i require and i was wondering if anyone knew the fastest way to log data to an SD card (preferably up to about 1000Hz)?

Thanks

#include "I2Cdev.h"
#include "MPU6050.h"
#include <SPI.h>
#include <SD.h>
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  20 // mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to 
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 200 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   0 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

MPU6050 accelgyro(0x69);
int16_t ax, ay, az;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO
//#define OUTPUT_BINARY_ACCELGYRO

const int chipSelect = 10;

File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}
void setup() 

{
  
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif
  
Serial.begin(38400);
Serial.println(F("Initialising the SD card..."));
accelgyro.initialize();

Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");


if (!SD.begin(10))
{
  Serial.println("Initialisation failed!!!");
  return;
}

Serial.println("Initialisation complete");

logfile.println("millis,&ax,&ay,&az,&gx,&gy,&gz");    
#if ECHO_TO_SERIAL
  Serial.println("millis,&ax,&ay,&az,&gx,&gy,&gz");
#endif //ECHO_TO_SERIAL
}



void loop() {
  
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
logfile = SD.open("filename.csv", FILE_WRITE);

// delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));


if (logfile)
{
  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    
#if ECHO_TO_SERIAL
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");  
#endif

#ifdef OUTPUT_READABLE_ACCELGYRO
logfile.print(ax);
logfile.print(",");
logfile.print(ay);
logfile.print(",");
logfile.print(az);
logfile.print(",");
logfile.print(gx);
logfile.print(",");
logfile.print(gy);
logfile.print(",");
logfile.println(gz);

#if ECHO_TO_SERIAL
Serial.print(ax);
Serial.print(",");
Serial.print(ay);
Serial.print(",");
Serial.print(az);
Serial.print(",");
Serial.print(gx);
Serial.print(",");
Serial.print(gy);
Serial.print(",");
Serial.println(gz);
 #endif
    #endif
  
  logfile.close();
}

else
{
  Serial.println("Error in opening file");
}

if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();

}

Opening and closing the file every time you want to log data is slower than opening it once.

Of course, opening it once means that you need to provide some other means of stopping logging and closing the file.

// delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

You want speed, huh? It doesn't seem so.