Arduino can't write MPU6050 data to SD card I hope my MPU6050 sensor can write data to my SD card, it compiled OK, but to open the serial monitor I got error message:

Initializing I2C devices...
Testing device connections...
MPU6050 connection successful
filename :acc_0.txt
0, ax: -85, ay: 227, az: 3997
filename :acc_0.txt
error: opening att_0.txt for write failed
SD errorCode: 0X13,0XE5




Here is my code:

// Based on I2Cdevlib, MPU6050_raw
// Experiments to verify if any data loss when writing MPU6050 outputs to SD card

#include "I2Cdev.h"
#include "MPU6050.h"
#include &lt;SdFat.h&gt;

// SD card read/write
// MOSI - pin 11
// MISO - pin 12 
// CLK  - pin 13
// CS   - pin 4
const int chipSelect = 4;

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;

SdFat sd;
SdFile myFile;

int count = 0;
int idx = 0;

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

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // change accel sensitivity to 8g
    accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_8);

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

    // Initialize SdFat or print a detailed error message and halt
    // Use half speed like the native library.
    // change to SPI_FULL_SPEED for more performance.
    if (!sd.begin(chipSelect, SPI_HALF_SPEED)) 
        sd.initErrorHalt();
}

void loop() 
{
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&amp;ax, &amp;ay, &amp;az, &amp;gx, &amp;gy, &amp;gz);

    // dynamic create a file
    if (count == 0)
    {
        // dynamic filename
        char filename[11];
        sprintf(filename, "acc_%d.txt", idx);
        Serial.print("filename :");
        Serial.println(filename);

        // open the file for write at end like the Native SD library
        if (!myFile.open(filename, O_RDWR | O_CREAT | O_AT_END)) 
            sd.errorHalt("opening att_0.txt for write failed");        
    }

    // write data to file
    Serial.print(count); myFile.print(count); myFile.print(", ");
    Serial.print(", ax: "); Serial.print(ax); myFile.print(ax); myFile.print(", ");
    Serial.print(", ay: "); Serial.print(ay); myFile.print(ay); myFile.print(", ");
    Serial.print(", az: "); Serial.println(az); myFile.println(az);         

    // close file when every 1,000 records
    if (count &gt; 1000)
    {
        myFile.close();
        Serial.println("Finish.");
        count = -1;
        idx  ;
    }    

    count  ;
}
You seem to be expecting a statement of the form

idx ;


to increment a variable, but it will not, so your idx and count variables are forever 0 and you keep trying to re-open the same file without ever closing it.

To increment a variable, use the ++ operator as in

idx++;

count++;