Go Down

Topic: Missing ADC data while saving data block in SD card (Read 2131 times) previous topic - next topic

csapsanis

Hi,

The goal of my current project is a simple data logging from an external ADC using a SPI interface to an SD card via an Arduino UNO. The system operates but I am losing adc data while saving.

For example, in order to verify if I am getting the data properly, I supply a sine wave in the ADC input. As you can observe from the attached fig., data is missing. As expected, the data are sent though chunks of 512bytes. As I can observe from the logic analyser, at the time of sending the data to SD card, the ADC is not operating.

Is any suggestion on how to mitigate this issue? I assume that I cannot completely overcome it, since they are sharing the same SPI with only different CS pins.

Thanks
Chris


wildbill

What frequency is the test sine wave? What about the real data?

Depending on those answers it might be possible to use a second Arduino to handle the writing and send the ADC numbers via serial. Or send it to a server via wifi or Ethernet.

csapsanis

#2
Mar 17, 2020, 12:05 am Last Edit: Mar 17, 2020, 01:20 am by csapsanis
The sine wave is at 1kHz. The spectrum that I am targeting should be up to that value. The sampling rate can be up to 10kHz.

The same data logging issue applies even when I use the internal ADC with the analogRead.

jremington

Have you thought about posting the code, using code tags, as described in "How to use this forum"?

csapsanis

#4
Mar 17, 2020, 02:51 pm Last Edit: Mar 17, 2020, 02:55 pm by csapsanis
Hi jremington

I think it is a more general problem but please find attached the code:

Code: [Select]

#include <SPI.h>
#include <SD.h>
#define FILE_BASE_NAME "DATA"

const int CS_SD = 10;
const int CS_ADC = 9;
unsigned long i;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.txt";
SPISettings settings1(1000000, MSBFIRST, SPI_MODE0);

void setup() {
  pinMode(CS_ADC, OUTPUT);
  Serial.begin(9600);
  delay(1000);
  while (!Serial) {
    ; // wait to connect
  }
   
  SPI.begin();
  SPI.beginTransaction(settings1);
  Serial.print("Initializing SD card...");

  // card present
  if (!SD.begin(CS_SD)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
  while (SD.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } else {
      Serial.println("Can't create file name");
      return;
    }
  }
  File dataFile = SD.open(fileName, O_CREAT | O_WRITE); // O_READ | O_WRITE | O_CREAT
  if (!dataFile) {
    Serial.println("open failed");
    return;
  }
  //time = micros();
  Serial.println("Start Recording");
  for (i = 0; i < 40000; i++){
   
    digitalWrite(CS_ADC, HIGH);
    uint16_t adc_val_H = SPI.transfer16(0x0110); // dummy transfer
    //delayMicroseconds(8);

     // Write data
     char cstr[5];
     dataFile.write(itoa(-adc_val_H, cstr, 10)); //itoa (int val, char *s, int radix)

    digitalWrite(CS_ADC, LOW);
    //delayMicroseconds(1);
  }
  dataFile.close();
  SPI.endTransaction();
  Serial.println("Data Saved");
  Serial.println(fileName);
}

void loop() {
}

ieee488

Hi,

The goal of my current project is a simple data logging from an external ADC using a SPI interface to an SD card via an Arduino UNO. The system operates but I am losing adc data while saving.

For example, in order to verify if I am getting the data properly, I supply a sine wave in the ADC input. As you can observe from the attached fig., data is missing. As expected, the data are sent though chunks of 512bytes. As I can observe from the logic analyser, at the time of sending the data to SD card, the ADC is not operating.

Is any suggestion on how to mitigate this issue? I assume that I cannot completely overcome it, since they are sharing the same SPI with only different CS pins.

Thanks
Chris


Write a simple sketch that generates sequential data to be written to the SD card.

Does it still work?


.

jremington

I imagine that datafile.write() is blocking in the event that a buffer needs to be written out. In any case, you can certainly expect problems trying to share the SPI port like that.

You won't encounter this problem if you use the Sparkfun OpenLog, which I highly recommend.

MarkT

There may be a way to do this using the ADC in auto-trigger mode (I've not actually done this yet myself),
allowing samples to be collected while the SDcard is being written.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up