Logging to an SD card issues with data

Hey all,

I'm currently working on a project to take data from IMU sensors and timestamp/clock that data using an Arduino UNO and save that data on an SD card.

At the moment, my code is able to take the input from a single IMU, (the MPU-9150), and save it to an SD Card. The problem is that sometimes the data I get on the SD card is corrupted, or has erroneous data. Also, sometimes the overall loop repeats itself, as it re-initializes the SD card more than once and causes the data to restart. Is there a specific type of SD Card that I need (i.e. class 10) in order to write to the SD card quick enough?

Below I've posted the current code for my Arduino. The IMU library was the given code used from the MPU6050 IMU. This code currently initializes the SD card, collects data from the IMU, and writes that data to the SD card in the file name of "LOG.csv".

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//     2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 //This is the I2C address

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

//intializes the library for the SD shield
#include <SD.h>

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;

const int chipSelect = 4;
String toprint;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;

#define LED_PIN 13
bool blinkState = false;

void setup() 
{
  
  // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // 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);
  
    // intialize SD Card
    Serial.println("Initializing SD Card");
    // Make sure that the default chip select pin is set to 
    // output, even if you don't use it:
    pinMode(chipSelect, OUTPUT);
    Serial.print("chipSelect set to output\n");
    
    if (!SD.begin(chipSelect))
    {
      Serial.println("Card failed, or not present\n");
      // don't do anything more;
      return;
    }
    Serial.println("card initialized");
        
    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

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

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);

    File logFile = SD.open("LOG.csv", FILE_WRITE);
    if (logFile)
    {
      logFile.println(", , , , , , , , ,"); // Just a leading blank line, in case there was previous data
      String header = "time(us), ax, ay, az, gx, gy, gz, mx, my, mz";
      logFile.println(header);
      logFile.close();
      Serial.println(header);
    }
    else
    {
      Serial.println("Couldn't open log file");
    }
    
}

void loop() 
{
  
 
  //make a string for assembling the data to log:
  String dataString = String(toprint) + "," + String(ax) + "," + String(ay) + "," + String(az) + "," + String(gx) + "," + String(gy) + "," + String(gz) + "," + String(mx) + "," + String(my) + "," + String(mz);
  
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opeing another
  
    // read raw accel/gyro measurements from device
    accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);  

    toprint = 0;
    toprint += micros();

    
    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g/m:\t");
    Serial.println(toprint); Serial.print("\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.print(gz); Serial.print("\t");
    Serial.print(mx); Serial.print("\t");
    Serial.print(my); Serial.print("\t");
    Serial.println(mz);

 
    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
    
  File dataFile = SD.open("LOG.csv", FILE_WRITE);
  if(dataFile)
  {
      dataFile.println(dataString);
      dataFile.close();
      Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't access file");
  }
  
}
String toprint;

Loose this.

      String header = "time(us), ax, ay, az, gx, gy, gz, mx, my, mz";

And this. There is no reason to be so lazy. Multiple calls to logFile.print() will prevent the issues that String causes.

  String dataString = String(toprint) + "," + String(ax) + "," + String(ay) + "," + String(az) + "," + String(gx) + "," + String(gy) + "," + String(gz) + "," + String(mx) + "," + String(my) + "," + String(mz);

This just screams clueless. Stop it!

thanks for the reply, but there's no need to talk down to someone who is just asking for help. If I knew what I was doing then I wouldn't be posting on the forum to begin with. Almost all the code written in here is based off of tutorials and my small amount of experience with the Arduino platform, which would be less than a months worth of experience.

Instead of just posting that my code "sucks", which it probably does based on my lack of experience, at least be a bit more helpful on how I can "fix" the code.

at least be a bit more helpful on how I can "fix" the code.

Find all occurrences of String, and delete them. There are known issues with the String class (actually the free() function that the destructor calls, hundreds of times in you code). The String class, as you are (mis)using it adds NO value to your sketch.

Lum1n0us:
The problem is that sometimes the data I get on the SD card is corrupted, or has erroneous data. Also, sometimes the overall loop repeats itself, as it re-initializes the SD card more than once and causes the data to restart.

The String class that PaulS was so scornful about can cause memory corruption and one symptom can be the Arduino resetting. It's not your fault; it's quite disgraceful IMO that there is STILL no warning in the official documentation against using the String class - as if leaving the bug un-fixed for so long wasn't bad enough. Anyway, you need to make a big mental note that the String class gives no substantial benefit over plain old c-strings and is currently broken and dangerous to use. I don't know for certain that's causing your problem, or that it's the only cause, but getting rid of all use of String should be your first step.

Thanks again for the replies, I'll look for an alternative method to the string class then. Not sure if you could provide me with a place to start looking as alternative ways to log my incoming data?

All you need do is avoid anything to do with strings, and even anything to do with the reasons why, for that matter. All you need is the numbers from the sensors, which you seem to have OK, and you send them to the SD just like you send them anywhere else.

a snippet.....

  Serial.print(OutTemp);
  Serial.print(" ,  ");
  Serial.print(DrainTemp);
  Serial.println(" ,  ");

  lcd.setCursor(49,1);
  lcd.print (OutTemp);
  lcd.setCursor(49,2);
  lcd.print(DrainTemp);
      
      myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN

  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");

       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE

There are minor formatting differences, appropriate for each type of output, but the game is essentially the same for all.

All you need do is avoid anything to do with strings, and even anything to do with the reasons why, for that matter.

No. strings are fine. Strings are not.

Little letter; Big difference.

No. strings are fine. Strings are not.
Little letter; Big difference.

Pedantic claptrap that isn't funny, and does nothing for a newbie.

and does nothing for a newbie.

I suppose that advising them to not use strings, while the rest of us are advising them TO use them is?