Help with increasing logging frequency

Hi All

I modified some code in order to collect data from 4 accelerometers.
At the moment the average logging frequency is 51 Hz BUT I wish to increase it as much as I can.
My hardware is an Arduino Mega 2560 + Wireless SD Shield + 4 Tinkerkit 3-axis accelerometers. The micro SD card I use is a class 10 card

Does anyone spot something on my code that could be done better and then increase the logging frequency? Attached and below my code
Thanks in advance


  SD card datalogger
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 created  24 Nov 2010 by Tom Igoe
 modified 1 June 2014 by G (
 This example code is in the public domain.

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.

const int chipSelect = 4;

int FL_power = 2; // This define the pins used to give power to the sensors
int FR_power = 3;
int RL_power = 5;
int RR_power = 6;

void setup()
   // Open serial communications and wait for port to open:
   while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
//Write Log File Header
  File dataFile ="datalog.txt", FILE_WRITE);
  if (dataFile)
    dataFile.println("New Log Session"); //Just a leading blank line, in case there was previous data
    String header = "";
    header += "Time";header += "\t";header += "FLx";header += "\t";header += "FLy";header += "\t";header += "FLz";header += "\t";
    header += "FRx";header += "\t";header += "FRy";header += "\t";header += "FRz";header += "\t";
    header += "RLx";header += "\t";header += "RLy";header += "\t";header += "RLz";header += "\t";
    header += "RRx";header += "\t";header += "RRy";header += "\t";header += "RRz";header += "\t";
    Serial.println("Couldn't open the datalog file");
pinMode(FL_power, OUTPUT); digitalWrite(FL_power, HIGH); // This is to power on the sensors
pinMode(FR_power, OUTPUT); digitalWrite(FR_power, HIGH);
pinMode(RL_power, OUTPUT); digitalWrite(RL_power, HIGH);
pinMode(RR_power, OUTPUT); digitalWrite(RR_power, HIGH);

void loop()
  // make a string for assembling the data to log:
  String dataString = "";
  dataString += millis();
  dataString += "\t";
    for (int analogPin = 0; analogPin < 12; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    dataString += "\t"; 

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile ="datalog.txt", FILE_WRITE);

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

DAS_1file_no_button_v2.ino (3.17 KB)

Wireless SD shield? How’s that work?

Wireless SD shield? How’s that work?

You can have a look to, I am only using it as it can allow you read/write data on the micro SD card
HOWEVER, I opened this thread because I need help with logging frequency. I do not think your question is appropriate in this contest?

Why are you trying to read 12 sensors ? That will waste time, even if nothing is connected to the analog inputs.

You could try opening the file once and keeping it open. I've found that often leads to sd card becoming corrupted about 10% of the time. However it would be worth trying as an experiment to see what difference it makes.

Ah, so the interface to the SD is not wireless, that would be interesting!

What if you reduce the data some? Looks like you write millis every time with a separator character. Maybe just store that once every 10 or 100 samples? SD cards seem to work best if the data is broken into 512 byte samples, maybe capture the time as part of a large block.

Stop using the String class. On top of all the stability issues it raises, heap allocation can be very expensive. Everything you're currently doing using the String class can be done just as easily using plain old c-strings (null-terminated char arrays).

I don't know whether the limiting factor in your case is reading from the sensors, or formatting the data, or writing to the SD card. I remember a post a couple of months ago describing a heavily optimised sketch which would capture multiple analog inputs and write the values to an SD card at tens of KHz; I think it was from the author of the SD library. I don't know how fast it's possible to read the sensors, but everything else should be able to go massively faster than you're achieving here.

michinyon: Why are you trying to read 12 sensors ? That will waste time, even if nothing is connected to the analog inputs.

This is what my application requires, I do not log data just for fun?