Try SdFat, a Library for SD Cards


@jeremywmccarter, try what I suggested to see if it works :wink:


I have glanced at previous posts and may have missed something.

First, when you share SPI, you need to disable all devices before initializing any device.

For all devices except the first one to be initialized set CS high with code like this.

  pinMode(CS_OTHER, OUTPUT);
  digitalWrite(CS_OTHER, HIGH);

SdFat sets SPI speed, mode, and bit order each time it accesses the SPI bus.

Many older libraries do not restore SPI setting so you may need to set speed and mode before calls to these libraries.


@whg This is a really well written library, so much content. Thank you.
I cannot however get the remove() function to work, can you give an example of removing or deleting a file. If I use the followint

File Foo;
the compiler throws an error, I have tried other combinations but this seems to be what the documentation requires?
I can open, write, read, truncate but not delete it.


Sorted. I had the file opened as O_READ it needs to be first opened as O_WRITE then erased, the following works

File Foo;“myFile.txt”,O_WRITE);



May I also ask question about using this library. I have a photon with shield shield and Arduino Ethernet Shield with microSD card reader.

I tried Ethernet shield with regular arduino and it works well and read microSd card… ChipSelect pin for microsd reader is 4 on shield

But when I try example trymefirst without changing any other code then SPI configuration I only receive
Can’t access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0X0

I try with SPI configuration 1 like below

// Pick an SPI configuration.
// See SPI configuration section below (comments are for photon).
// Setup SPI configuration.
// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFat sd;
const uint8_t chipSelect = SS;
// Secondary SPI with DMA
// SCK => D4, MISO => D3, MOSI => D2, SS => D1
SdFat sd(1);
const uint8_t chipSelect = D6;
// Primary SPI with Arduino SPI library style byte I/O.
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFatLibSpi sd;
const uint8_t chipSelect = SS;
// Software SPI.  Use any digital pins.
// MISO => D3, MOSI => D2, SCK => D4, SS => D6
SdFatSoftSpi<D3, D2, D4> sd;
const uint8_t chipSelect = D5;

Since I use shield shield I use D6 as CS which is pin 4 on Ethernet shield. Anyone who can help me understand why I it doesn’t work?

EthernetS         S_SHIELD     PHOTON
SCLK              13           D4         // SPI_3 SCK
MISO              12           D3         // SPI_3 MISO
MOSI              11           D2         // SPI_3 MOSI
CARD_CS            4           D6


I was trying to use the SdFatSoftSpi library and can not get the code to recognize the device:.

Can’t access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0XFF

We have the photon/electron mounted on another board that has the SDcard and none of the SPI mappings work as we used all of the pins for other functions, so I resorted to using the softwareSPI mode… Have used an older library: with software SPI and it works, .but I wanted to upgrade to a better library with long file names and what looks like a more active community for the Particle devices.

Anyone had any luck using softwarespi with this library? The example (SoftwareSpi) referenced in the html help file is not present in what I downloaded from git. I probably have some configuration file issue, but I have not found it yet. I am using the particle desktop IDE for building.

Any pointers appreciated


Hello, everyone.

I want to ask about Particle’s library.
I have my code ready for upload in the photon.
But, it stuck at verifying. Here is the error. /workspace/test_button_semi.cpp:2:16: fatal error: SD.h: No such file or directory

How can I insert a library in Particle? Is it necessary?

I have bought two books about the particle photon by Simon Monk and Christopher Rush. But non-telling about how to insert a library in particle’s library. I guess it’s same with the Arduino, right?
Please help me to understand more about programming the particle photon. As for now, I just copy and paste every code that I’ve done in the Arduino IDE into Particle web IDE.

Here I attach my code on the above error.
**Noted that I used ( ) in # because to make it visible to you.

#include <SPI.h>
#include <SD.h>
#include <Wire.h> //I2C Library
#include <EEPROM.h>

const int CS_pin = 53;
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState5 = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

double ReadEEPROM; //Variable to store in EEPROM.
int C = 0; //Location we want the increment to be put.
File myFile;

void setup() {
Serial.println("Initializing Card");
//CS Pin is an output
pinMode(CS_pin, OUTPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
//button will Draw Power from Pin 5, so set it high
pinMode(buttonPin, INPUT);
//digitalWrite(buttonPin, HIGH);
while (!Serial);
Serial.print("Read the last number of button push in EEPROM : ");
EEPROM.get(C, buttonPushCounter);

//Initialize Card
if (!SD.begin(CS_pin))
Serial.println("Card Failure");
Serial.println("Card Ready");


void loop() {
// read the pushbutton up input pin:
char fileName[10];
buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == HIGH)

  sprintf(fileName, "%d.txt", buttonPushCounter);

  myFile =, FILE_WRITE);
  Serial.println("File that will open  :");
  myFile.println("testing 1,2,3...");
  digitalWrite(ledPin, HIGH);      
  digitalWrite(ledPin, LOW);
  // close the file:
  myFile =;
  Serial.println("File open  :");
// save the current number of button pushed as the last number value
lastButtonState = buttonState5;
EEPROM.put(C, buttonPushCounter);

I hope that you can help me to understand about Photon better.
Thank you.


You can add the library following this guide:


Thank you…

What about this library, TryMeFirst?
Shouldn’t it be in the library?


Where’s that from?

You might need to give more information like which guide you are following and what libraries are required to remove the guesswork. :wink:


From this forum…


They are all listed under the Library examples:


Sorry, I’m still trying to go there but I don’t know why I’m stuck here…
When, I search in the library, there is no info like you give above.


Where can I find it? Is there any other library in the IDE?


Search for SDFat


In your right side screenshot it does tell

Showing the 10 most popular libraries,
search to find more

and at the top you have a textbox

[ Type to search ______ ]

where you can enter SDFat :wink:

SDfat library not working on particle electron

Hi all,

I flashed the bench.cpp firware into my electron. However, i did not get any output when i ran particle serial monitor command in node.js command prompt. The electron went into safe mode right after bootloading. Did i miss out anything required to change or is it just a plug and play feature from the example in the library? I have provided a snapshot below regarding the output and the setup.



Hi all,
first time poster, long time user. I’m trying to use the Adafruit Arduino SD Card shield with the photon. My goal is to sample analog signals (all 8 or as many as possible) at 100,000 sps and record the data onto the SD Card.

thoughts? sample codes? any help is greatly appreciated.




I’m not sure the Shield Shield will work well with the SD card shield as you’ll have two sets of level shifters 3.3V Photon <-> 5V shields <-> 3.3V SD card.
I’d just go for a bare SD card holder as the voltage levels already fit together.

And sampling 8 channels at 100kHz will also proof difficult.


I try to search SDFat, yes it shows the library.
I just don’t get it why I can’t get like @kennethlimcp give in his screenshot.
Because I try to learn about particle from an e-book.
@kennethlimcp maybe you can show me how you do it, like in the screenshot.

Thank you.