How to achieve faster sampling rate when writing data to SD Card?

Hi everyone! I am very new to Arduino and am currently working on a data logging project.

Currently, I need to obtain 8000 samples from an ADC Conversion and store it to an SD card. I’m using a class 4 8GB SD card. The values will then be used to plot a wave. I have used the smallest prescaler in my code which is 16 and the maximum sampling rate I can achieve is 3600 per second.

What else can I do to obtain at least a 4000 per second sampling rate? The optimum would be 8000. Thank you! :slight_smile:

Here is my code:

///*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file  
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10
     
 */
// Define various ADC prescaler
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

#include <SD.h>

File myFile; 
unsigned long data;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
 pinMode(2, INPUT);
 
 // set up the ADC
  ADCSRA &= ~PS_128;  // remove bits set by Arduino library

  // you can choose a prescaler from above.
  // PS_16, PS_32, PS_64 or PS_128
  ADCSRA |= PS_16; // 16 prescaler

 
unsigned long i;

  Serial.print("Initializing SD card...");
 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("hooyy.csv", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    
     // capture the values to memory
  for(i=0;i<100;i++) {
    //start_times[i] = micros();
    data = analogRead(2);
    myFile.println(data);
    //stop_times[i] = micros();
  }
  // print out the results
 // Serial.println("\n\n--- Results ---"); 
  //for(i=0;i<8000;i++) {
    //Serial.println(data);
    //myFile.println(data);
    //Serial.println(" elapse = ");
    //Serial.print(stop_times[i] - start_times[i]);
    //Serial.print(" us\n");
    
  //}
  
    // close the file:
    myFile.close();
    Serial.println("done.");
   
  } 

  else {
    // if the file didn't open, print an error:
    Serial.println("error opening final.csv");
  }
}
  // re-open the file for reading:
 // myFile = SD.open("momo.csv");
 // if (myFile) {
 //   Serial.println("momo.csv:");
    
    // read from the file until there's nothing else in it:
  //  while (myFile.available()) {
  //      Serial.write(myFile.read());
  //  }
    // close the file:
  //  myFile.close();
 // } 
 // else {
    // if the file didn't open, print an error:
  //  Serial.println("error opening boo.csv");
 // }
//}

void loop()
{
    // nothing happens after setup
}

I'm sampled at 40,000 samples per second and stored it on an SD card as a bin file several times.

http://forum.arduino.cc/index.php?topic=228549.0

Thank you, I will try the code! :grin: :grin: :grin:

Don't try to use it until you understand about the bintocsv program. When you unpack the whole library to your My Documents\libraries folder, it create a folder called "bintocsv" in your libraries folder if you try to compile the analogbinlogger sketch without removing that folder you will get compiler errors. You need to "cut" & "paste" that "bintocsv" folder to somewhere else outside the libraries folder. When you run the program the menu appears in the serial monitor. If you don't open that you won't be able to use the program. Once you start recording , if you stop the recording by pressing any key , if you don't select convert bin to csv right then and there, then you will be forced to do it later using the command line (dos window) and cmd line commands explained in the readme file. It may take a while to convert it but it is much more convenient to do it right after stopping the recording.