Fast Data Logging

Hi,

I am using a RedBoard Qwiic, with an OpenLog Qwiic, and an analog measurement device (ecg/emg). I am trying to store more than 200Hz data on the OpenLog. I am having troubles because of the latency of the logging.

I have tried two approaches:

The first one has no determined sampling frequency and just measures and logs in every loop. With this I get under 100Hz.

#include <compat/deprecated.h>
volatile unsigned char CurrentCh=0;         //Current channel being sampled.
volatile unsigned int ADC_Value = 0;    //ADC current value

//*** added these lines for logging
#include <Wire.h>
#include "SparkFun_Qwiic_OpenLog_Arduino_Library.h"
OpenLog myLog; //Create instance
char filename[11] = "ECGdata.txt";
//***

String output = "";

void setup() {

// Serial Port
Serial.begin(57600);
//*** added these lines for logging

  Wire.begin();
  myLog.begin(); //Open connection to OpenLog (no pun intended)
  delay(1000);
  myLog.append(filename); 
  
 // ***
}

void loop() {
        ADC_Value = analogRead(CurrentCh);
         Serial.print(millis());
         Serial.print(",");
         Serial.print(ADC_Value);
         Serial.println();
         myLog.append(filename);  
         myLog.print(millis());
         myLog.print(",");
         myLog.print(ADC_Value);
         myLog.println();
         myLog.syncFile();
}

In the second one I measure the data with 200Hz, add every new line to a string, and then store the data at the micro SD every 100 measurements. In that case, 200Hz work more or less well, but when it sends the data to the micro SD I lose around 0.4 seconds. This gets shorter, the less data I am storing in my string obviously, but than it happens more often.

#include <compat/deprecated.h>
#define SAMPFREQ 200                     // ADC sampling rate 256
volatile unsigned char CurrentCh=0;         //Current channel being sampled.
volatile unsigned int ADC_Value = 0;    //ADC current value
volatile unsigned int recentmillis = 0;
const int timeconstant = 1000/SAMPFREQ;
const int savelines = 100;
int counter = 0;

//*** added these lines for logging
#include <Wire.h>
#include "SparkFun_Qwiic_OpenLog_Arduino_Library.h"
OpenLog myLog; //Create instance
char filename[12] = "ECGdata2.txt";
//***

String output = "";

void setup() {

// Serial Port
Serial.begin(57600);
//*** added these lines for logging

  Wire.begin();
  myLog.begin(); //Open connection to OpenLog (no pun intended)
  delay(1000);
  myLog.append(filename); 
  
 // ***
}

void loop() {
  if (millis() >= recentmillis + timeconstant) 
  {     
        recentmillis = millis();
        ADC_Value = analogRead(CurrentCh);
        char string_one[1] = ",";
        output+= "\n" + String(millis()) + string_one + String(ADC_Value);
        //Serial.print(millis());
        //Serial.print(", ");
        //Serial.println(ADC_Value);
        counter++;

        if (counter == savelines)
        {
         Serial.print(output);
         myLog.append(filename);  
         myLog.println(output);
         myLog.syncFile();
         output = "";
         counter = 0;
        }  
  }
}

Does someone have a better idea on how I could approach that problem?

Best,

Patrick