Skip to content

Under the hood: Arduino SD card library & CSV data managment

AJ edited this page Feb 7, 2024 · 5 revisions

IN PROGRESS!

Let's take a look at a typical line of CSV from an SD card write: (i0-5 are indexes, in markdown mode I have to put a header to make a table)

i0 i1 i2 i3 i4 i5
1 2 3 A B C

Let's read this data from the SD card with the Arduino. We'll do that by:

  myFile = SD.open(fileName, FILE_READ);
  int byteCount = 0;

  while (myFile.available() > 0)
    {
      byteCount = myFile.readBytes(buff, MAXBYTES);
      
      Serial.print("Number of Bytes Copied: ");
      Serial.print(byteCount);
      Serial.println();
    }

  myFile.close();

  for (int i = 0; i < byteCount; i++)
    {
      Serial.print(buff[i]); 
      Serial.print(" ");
    }
  Serial.println();

Let's get a step by step breakdown.

myFile = SD.open(fileName, FILE_READ); -> To start at the beginning of the file, we need to use FILE_READ.

byteCount = myFile.readBytes(buff, MAXBYTES); -> Copy the bytes from myFile into buffer. readBytes returns with the amount of bytes read.

Clone this wiki locally