10 years, 6 months ago.

Issue in Data logging in SD Card

Hi,

I am developing data logging application using mbed which stores data in SD card using SPI. Here i am writing ADC data with UNIX_TIMESTAMP to test.csv file.

All i need is data with 1000 samples/sec. but i am getting around 24 samples per second that u can see in image attached. As far as i think this delay is mainly due to the sd card read & write delay.

So can u guide me how to achieve such higher sample rate.??

This is my code.

// example writing to SD card

#include "mbed.h"
#include "SDFileSystem.h"


SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
AnalogIn voltage(p15);
int main() {

    mkdir("/sd/time", 0777);
    set_time(1379940793);
    while (1) {
    time_t seconds = time(NULL);
    FILE *fp = fopen("/sd/time/test.csv", "a");
     if(fp == NULL) {
        error("Could not open file for write\n");
    }
 
        
    fprintf(fp, "%d,%f\r\n", seconds,3.3*voltage.read());
fclose(fp); 

}
    
}   

This is my output file.Left side column has values of timestamp and right side column has voltage values.

/media/uploads/nis8288/untitled.png

I think that isn't a trivial requirement on an SD card. One thing you can try is not opening and closing the file in the while loop, just keep it opened until you are finished writing.

posted by Erik - 23 Sep 2013

2 Answers

10 years, 6 months ago.

I also had the same slow SD card write problem to save 1000 samples/sec of CAN data.

My solution was to store the data in a ring buffer memory first, then save it to the SD card.
Please refer to following sample program.
http://mbed.org/users/ym1784/code/CAN_LOG_SD/

It works as expected.

10 years, 6 months ago.

Hey thanks that worked....keeping fclose and fopen out of while loop and storing the data in buffer is the solution .