Logging data to SD card problems

Hi,

We are trying to use a SD-card to store data from an ADXL335 accelerometer. We want to control the sample rate (fs) of the accelerometer, and the runtime, as this determains how many values we obtain. We need to have a precise sample rate for our project, hence why we are asking for help. We are having problems with the fs, as it seems to work with about 30 Hz, but we want it to be at least 100 Hz. We tried creating the script so that it stored all the data in 3 arrays in a for-loop, before converting it to the right values and then storeing it on the SD-card. We would appreciate if someone would take the time to look through our script, and give some advice to how to improve it, and hopefully solve our problems. We are not that experienced with programming, and especially not c++. We have linked the code. Thanks in advance :slight_smile:

#include <SPI.h>
#include <SD.h>

const int xpin = A1;
const int ypin = A2;
const int zpin = A3;

File myFile;
//String fileName;

////////////////////////////////////////////////////////
// Input values
const String fileName = "File.txt";
const int runtime = 10;
const int fs = 100;
const int n = runtime * fs;
const float meanX = 1.0784;                         //These values are constant 
const float meanY = 1.0705;
const float meanZ = 1.3122;
int xRaw[n], yRaw[n], zRaw[n];                      //Creates 3 empty arrays for the data
//////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);

  while (!Serial){
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");
  
  //

  delay(2000);

  Serial.println("Data collecting starting now");

  for (int a = 0; a < n; a++) {                          
    xRaw[a] = analogRead(xpin);                           //Collects the data in the arrays
    yRaw[a] = analogRead(ypin);
    zRaw[a] = analogRead(zpin);

    Serial.print("Iteration "); Serial.print(a); Serial.println(" done");
      
    delay(1000/fs);                                       //The delay/time between each measurement
  }

  Serial.println("Data collection finished");             //Prints to serial monitor that collection of data is done

  //delay(1000);

  float xVal[n], yVal[n], zVal[n];                        //Creates 3 empty arrays for the float values

  for (int a = 0; a < n; a++) {
    xVal[a] = (((xRaw[a] * (3.3/1023.0)) - meanX) / 0.3) * 9.82;                     
    yVal[a] = (((yRaw[a] * (3.3/1023.0)) - meanY) / 0.3) * 9.82;
    zVal[a] = (((zRaw[a] * (3.3/1023.0)) - meanZ) / 0.3) * 9.82;

    delay(50);
  }

  Serial.println("Data conversion complete");               //Prints to serial monitor that conversion of data is done

  delay(2000);

  myFile = SD.open(fileName, FILE_WRITE);              //Creates and opens the file
  if (myFile) {
    for (int a = 0; a < n; a++) {
      myFile.print(xVal[a]);
      myFile.print(", ");
      myFile.print(yVal[a]);
      myFile.print(", ");
      myFile.println(zVal[a]);

      delay(50);
    }
    Serial.println("Logging sensor data to SD card finished.");   //Prints to the serial monitor that the measurements is done
  } else {
    Serial.println("Error opening file");
  }
  myFile.close();                                         //Closes the file (This is important to do)
}

void loop() {
  //Remains empty
}

Do not print to Serial in the data collection loop, or if you must print then significantly increase the baud rate. At 9600 baud, you can send 960 characters/second, or 9.6 characters per sample at 100Hz sampling rate. I would also get rid of the delay() and instead use millis() or micros() for timing the samples.

The serial print in the loop are not necessary, we just used it to watch where/when it went wrong, as it seems that when we increase fs, when the script is ran it jumps out of the loop at some point to initialize the as card again. Is this also because of the use of delay() instead of millis()?

Thank you for the response😊

Why all delays? That will kill the sample rate.

What type arduino are you using? The float arrays will take considerable amounts of memory.

Storing that much data to an SD card will take considerable time. For continuous high speed sampling I would store the raw data in binary form (no conversion to ASCII), and do the processing later.

We are using Arduino MEGA, but are in possession of the UNO too. I think that to save the raw data as binary data is a bit too complicated for our skills, but if you are able to give us some examples of how to do this it could be an idea.

Upgrading your skills will be the answer to your data storage problems. Save ALL the computations for the later program that processes the data from the SD card.

Once place to start is to remove all lines like this: