Logging data at >50Hz

Hello!

I am having a bit of trouble lately logging accelerometer data on a microSD (2GB, FAT16).
The reason is, to capture vibrations/impacts/movements, the accelerometer has to run at least at 50Hz in order to obtain a nice resolution.
However, data is being logged at approximately 5-10Hz. I would appreciate any help or ideas on how to log data at higher rates.

Thank you.

code? Libraries used?

the SDFAT lib can get in the clock speed area of around 8Mhz, getting data logged at 5-10 Hz sounds like something else is up

Osgeld:
the SDFAT lib can get in the clock speed area of around 8Mhz, getting data logged at 5-10 Hz sounds like something else is up

I'm using SD library, maybe there is something in the code...

#include <SD.h>
#include <Wire.h>
#include <ADXL345.h>

const int chipSelect = 8;
ADXL345 accel;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  accel = ADXL345();
  pinMode(10, OUTPUT);  

  // Set the range of the accelerometer.
  accel.SetRange(16, true);
  accel.EnableMeasurements();
   if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized."); 
  //Write Log File Header
  File logFile = SD.open("ADXL345.txt", FILE_WRITE);
  if (logFile)
  {
    String header = "ADXL345 Initialized";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
}

void loop()
{
  if(accel.IsConnected) // If we are connected to the accelerometer.
  {
//    AccelerometerRaw raw = accel.ReadRawAxis();
       AccelerometerScaled scaled = accel.ReadScaledAxis();

 File dataFile = SD.open("ADXL345.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(scaled.XAxis);
    dataFile.print(",");
    dataFile.print(scaled.YAxis);
    dataFile.print(",");
    dataFile.println(scaled.ZAxis);

    dataFile.close();
    // print to the serial port too:
    Serial.print(scaled.XAxis);
    Serial.print(",");
    Serial.print(scaled.YAxis);
    Serial.print(",");
    Serial.println(scaled.ZAxis);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("Error opening file");
       } 
  }
  delay(10);
}
  Serial.begin(9600);
...
    // print to the serial port too:
    Serial.print(scaled.XAxis);
    Serial.print(",");
    Serial.print(scaled.YAxis);
    Serial.print(",");
    Serial.println(scaled.ZAxis);

Say you are printing 10 characters, eg.

12,34,56\r\n

At 9600 baud that will take 0.0104 seconds so you can only do 96 a second.

Comment out the serial prints, or bump up your baud rate to 115200.

I tried doing that but it didn't work. Actually just a few moments ago I realized that it works better if I send a single string; therefore I have to send the data using a buffer. I also just tested this method and it gave much much better results (almost 100Hz).

The only downside is that my raw accelerometer data is setup as floating point numbers, which cannot be send as strings (being read as "?").
I am trying to convert float to char at the moment, and hopefully that should solve the problem.

Thanks for your tip!

it gave much much better results (almost 100Hz)

96 Hz?

Do what you did, and get rid of the serial prints.

Hi,

Its a while since I used the SD Library, but do you need to open the file everytime, can't you open it in setup and then just use it in loop ?

i.e move this to setup -

File dataFile = SD.open("ADXL345.txt", FILE_WRITE);

Duane B

rcarduino.blogspot.com

How would you close the file before stopping/resetting the Arduino.

Brown out feature maybe ...

-Enjoy
fh : )_~

See this topic Where are my datas? - #8 by fat16lib - Storage - Arduino Forum.

I solved the problem today, here are some factors which helped in speeding up the data logging process:

  1. Wrote a single string at a time to SD card (most important)
  2. Removed all "Serial.print" which were used for debugging
  3. Removed any unnecessary variables and code (lowered sketch size)

Now I am logging data on the SD card at about ~10ms (~100Hz) which provides really nice resolution.

I also tried moving the file open command in the setup(), but that does not help as the file is being appended and has to be opened and closed within the loop.

Thank you everyone for your suggestions!