Write at high speed on a SD card

Hello,

I am using an Arduino Nano BLE Sense to collect datas from several sensors. My final goal is to make possible that these datas are saved at different sampling frequency (4 Hz, 32 Hz or 64 Hz depending on the data) on a SD card and are sent by BLE. I am currently working on the part of the program to save on SD card. Here is the basic program for recording one data.

#include <Arduino_HTS221.h> // temperature sensor
#include <SPI.h>
#include <SD.h>

File myFile_T;

float tSensor=0;
float t=0;//time initialisation
const float f_temp=20; //sampling rate of temperature (Hz)
const float delta_temp=1/f_temp*1000; //period between 2 saving (ms)
float last_t_temp =0;

void setup() {

  HTS.begin(); //initialisation of the temperature sensor
      
  // Open serial communications and wait for port to open:  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.print("delta_temp \t");
  Serial.println(delta_temp);
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

}

void loop() {
  t=millis();
  //Serial.println(t-last_t_temp);
  
  if ((t-last_t_temp)>delta_temp){
    Serial.print(t);
    Serial.print("\t");
    Serial.print(last_t_temp);
    Serial.print("\t");
    Serial.print(t-last_t_temp);
    Serial.print("\t");
    tSensor=HTS.readTemperature();
    Serial.println(tSensor);
  
   // open the temperature file and write inside
    myFile_T = SD.open("temp.txt", FILE_WRITE);
  
    if (myFile_T) {   // if the file opened okay, write to it:
      Serial.print("Writing to test.txt...");
      myFile_T.print(t);
      myFile_T.print("\t");
      myFile_T.println(tSensor);
      
      myFile_T.close();// close the file:
      Serial.println("done.");
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening temp.txt");
    }
    last_t_temp=t;
  }
//delay (1000);

}

I have changed sampling rate to check until which value I can go. It seems that it is difficult to go higher than 20 Hz. To overcome that, I tried keep the file opened (so the file is never closed). However, this method leads to empty files. Does anyone would have a solution?

#include <Arduino_HTS221.h> // temperature sensor
#include <SPI.h>
#include <SD.h>

File myFile_T;

float tSensor=0;
float t=0;//initialisation du temps
const float f_temp=4; //fréquence d'enregistrement de la température (en Hz)
const float delta_temp=1/f_temp*1000; //écart entre 2 enregistrements de température (en ms)
float last_t_temp =0;

void setup() {

  HTS.begin(); //initialisation of the temperature sensor
      
  // Open serial communications and wait for port to open:  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.print("delta_temp \t");
  Serial.println(delta_temp);
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  
  // open the temperature file and write inside
  myFile_T = SD.open("temp.txt", FILE_WRITE);
  Serial.println("file opened");
}

void loop() {
  t=millis();
  //Serial.println(t-last_t_temp);
  
  if ((t-last_t_temp)>delta_temp){
    Serial.print(t);
    Serial.print("\t");
    Serial.print(last_t_temp);
    Serial.print("\t");
    Serial.print(t-last_t_temp);
    Serial.print("\t");
    tSensor=HTS.readTemperature();
    Serial.println(tSensor);
  

  
    if (myFile_T) {   // if the file opened okay, write to it:
      Serial.print("Writing to test.txt...");
      myFile_T.print(t);
      myFile_T.print("\t");
      myFile_T.println(tSensor);
    }      
//      myFile_T.close();// close the file:
//      Serial.println("done.");
//    } else {
//      // if the file didn't open, print an error:
//      Serial.println("error opening temp.txt");

    last_t_temp=t;
  }
//delay (1000);

}

I use the SdFat ExFatLogger to store data off an Adafruit LSM6DS33 3-axis Accelerometer and 3-axis gyroscope. I can log at 250Hz sample rate using a Mega2650 and a seeed SD shield. You should be OK for performance and memory with the Nano BLE M4 is you choose to go this route.

1 Like

generate preformatted string und send it to file.

1 Like

Thanks for the reply, I will check this library

Hello, would it be possible to see your code? Indeed using SDFat library I can reach a delay of 53 ms between 2 measurements writing (eg around 20Hz) but I don't manage to go faster

SdFat SD;
#define SD_CS_PIN SS

File myFile_T;

float tSensor=0;
float t=0;//initialisation du temps
const float f_temp=32; //fréquence d'enregistrement de la température (en Hz)
const float delta_temp=1/f_temp*1000; //écart entre 2 enregistrements de température (en ms)
float last_t_temp =0;

void setup() {

  HTS.begin(); //initialisation of the temperature sensor
      
  // Open serial communications and wait for port to open:  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.print("delta_temp \t");
  Serial.println(delta_temp);
  Serial.print("Initializing SD card...");

  //if (!SD.begin(4)) {
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

}

void loop() {
  t=millis();
  //Serial.println(t-last_t_temp);
  
  if ((t-last_t_temp)>delta_temp){
    Serial.print(t);
    Serial.print("\t");
    Serial.print(last_t_temp);
    Serial.print("\t");
    Serial.print(t-last_t_temp);
    Serial.print("\t");
    tSensor=HTS.readTemperature();
    Serial.println(tSensor);
  
   // open the temperature file and write inside
    myFile_T = SD.open("temp.txt", FILE_WRITE);
  
    if (myFile_T) {   // if the file opened okay, write to it:
      Serial.print("Writing to temp.txt...");
      myFile_T.print(t);
      myFile_T.print("\t");
      myFile_T.println(tSensor);
      
      myFile_T.close();// close the file:
      Serial.println("done.");
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening temp.txt");
    }
    last_t_temp=t;
  }
//delay (1000);

}

Just get ExFatLogger.ino from here

My code only incorporated the Adafruit LSM6DS33 sensor into the EFL.ino You need to modify>

//==============================================================================
// Replace logRecord(), printRecord(), and ExFatLogger.h for your sensors.

starting about line 86

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.