Try SdFat, a Library for SD Cards


Click on SDFat and scroll to the bottom and you will see the examples.


Nice… That is what I’m looking for.

Thank you @kennethlimcp and thank you all.:smiley:

Sorry for any inconvenience questions.:grin:


Hi looking for a solution to all of this, Has anyone gotten the SD to work with the electron? Read all the post and just keep getting lost. Any help would be great just want to use the SD to store string of data.


I’m using SDFAT, and I think it’s a great library for the particle devices. Big thanks to whg.
I am using it to log low frequency data (1 point per second or 1 point per 5 seconds). I would like to be able to allow my logger to run for several weeks before retrieving the SD card.

I have it working, except that my logger stops writing data to the file after some time. Last night it stopped at about 1:30 AM. This morning, I checked my serial monitor and everything seemed fine, except the file is not getting any additional data “put” into it.

Every 5 seconds I’m opening the file, writing one line of data, and then closing the file.

It stopped after 376,446 bytes.

I’m concerned that I need to do something more complex with the “blocks” in order to write files that are very large. I have glanced over the LowLatencyLogger.cpp example, but it seems overly complicated for what I need. (but maybe not…)

Does anyone have any experience with this type of problem or can point me in the correct direction for appending data to very large files?

Here is the code that I’m using to append my sensor data to the file:

void writeDataSD()
  if (!, O_RDWR | O_CREAT | O_AT_END)) {
    //sd.errorHalt("opening test.txt for write failed");
    Serial.println("...opening Logfile: <" + curLogFile + "> for write at end FAILED");
    // if the file opened okay, write to it:
    //Serial.print("Writing to test.txt...");
    myFile.printlnf("%d,%d,%d",, global_var1, global_var2);
  // close the file:

curLogFile is just a string containing the name of the *.csv to log to. Example:

As you can see, I just ripped the File write code straight out of one of the examples.

Thanks for your help.


So I’ve been looking in many place, and I think this is the best thread.

I’m working on a project with a SD card and the electron is going to deep sleep regularly. The SD card is supposed to use 100µA when idle, but it looks more like 6.6mA actually. Not sure why, but this seems to be the same value as mentioned by @peekay123 last year… Any explication or change I should make?

For info, I’m using the SD photon compatible Library and the card is using SPI (hardware) on A2-A5 (electron)

Help, anyone?



@peergum, I find some microSD cards don’t sleep as expected. I’ve had to use high-side power control to the SD was necessary using a MOSFET. I bias the fate of the FET with a pullup or down resistor so the SD remains off during deep sleep.


Hello All.
Need help here! I have an SD card hooked up to my Electron. Everything works, except that I never seem to reach end of file.
Below is the relevant part of my code.


  if (sd.begin(chipSelect, SPI_HALF_SPEED)) {
    Serial.println("SD opened successfully");
  } else     sd.initErrorHalt();

  // re-open the file for reading:
  if ("data.dat", O_READ)) {
      Serial.println("File opened successfully");
  } else   sd.errorHalt("opening test.txt for read failed");

  char c;
  String line="";
  int bytes=0;

  while ((>=0)
  Serial.println("File read done. Bytes="+bytes);

This loop prints all the data on the card well, but does not terminate.
So I printed the data in HEX format. At first, I saw that the last byte was 0xDA, which is equivalent to “Ú” and the while loop did not print anything further nor terminate.
Now, several trials later, it prints a series of "F"s continuously after reaching the end of the file. This happens for 2 micro-sd cards I have. One is 1GB and the other 2GB.
Yes, there is a possibility that these are not reliable SD cards, they were quite cheap (about 2 USD each). They have no issue however on Windows. I can write to them and read from them well. They are formatted to FAT32 (using the default Windows formatter). I also used SD Card Formatter, same story.

I later realized that the TryMeFirst example used int and not char to hold the characters while reading.
Using that approach, the file does reach the end and does NOT continue to print the "F"s. But the code outside the loop doesn’t get executed.
So i modified the code to look like below, to print the bytes counted after each new line.

int c;
  String line="";
  int bytes=0;
  while ((>=0)
  Serial.println("FILE READ DONE. Bytes="+bytes);

The result, in PUTTY, is in the snapshot below.

The “problem?” and “andler” are NOT part of my file contents and the Serial.println("BYTES="+bytes); statement is not executed as should be.
The Particle Dev monitor prints something slightly different. Look at the snapshot below:

I seem to have extra gibberish.
Any takes? Any ideas?
Worst case scenario, Since I do print all my data, I need to at least detect that there is no more printable data.
I am using SPI Configuration 0 and everything is wired to the hardware SPI and the default SS.
I need to also mention that the TryMeFirst example, unmodified, works perfectly well!


I haven’t checked the implementation of SDFat, but the -1 you expect is meant to be an int but your char c can never become -1.

0xFF would be -1 with int8_t but char is unsigned.

That’s also why TryMeFirst - which uses int c - works.

Do you always get the same “extra” words? Or are these random?
Have you double checked whether these are in your file after all?
Maybe your SD write code is putting them there by mistake.