Increase sample rate in project

Hi, am new to arduino and i am working on a project where i log 160 sensors from a pressure sentitive mat with an arduino mega. I also log magnetometer data from a magnetometer shield. Both communicating through the I2C bus.

The pressure mat with the arduino compatible controller is seen in the picture:

Maximum baudrade for the controller is 1,497,600 bps. The controller is stacked on top of a arduino mega

I log the data to a SD-card with a spark fun SD-card shield in a CSV file through the SPI interface. The loop starts by:

  1. Get magnetometer data (3-axis)
  2. Creates an array of size 160 type byte and fills it with pressure sensor data
  3. Creates a string and fills it with data
  4. Writes the string to the SD-card.

By running this code i can log at approximately 13 Hz (13 samples pr second). Is there any way to speed up the sampling rate? What is the bottleneck?

//-------------------------------------------------------------------------------------------------------------------
void loop()
{
    mySensor.updateMag(); //collects data from magnetometer
    
    byte arraydata[160]; 
    snowforce.read(arraydata); // fills the array with data from the pressure sensitive mat
    
     // make a string for assembling the data to log:
     String buffer_string = "";
     buffer_string += String(millis()); 
     buffer_string += "," + String(mySensor.readMagX());
     buffer_string += "," + String(mySensor.readMagY());
     buffer_string += "," + String(mySensor.readMagZ());
     sum = 0;
     for(int i=0;i<160;i++){
      buffer_string += String(",");
      buffer_string += String(arraydata[i]);
      sum = sum + int(arraydata[i]); 
    }
   

// open the file. 
  File dataFile = SD.open(filename, FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
   dataFile.println(buffer_string);
   dataFile.close();
 
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening: " + filename);
  }

  
}

Don't use the String type! You can either have a fixed size array of char instead, or write each value separately to the SD card.

I wonder if writing to the SD Card is slow.

It would be interesting to see what repeat-rate you could get without the SD Card code (just lose the data for the test).

You need to post a complete program, including links to any specialized libraries.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Another tip would be not to open and close the SD card file each time you write to it. You can open it in setup() and maybe never close it. But after writing, perform "datafile.flush()" to ensure the data gets written to the card.

Thanks for all the tips! I will test out the different approaches and come back with the results.