Try SdFat, a Library for SD Cards


@jeremywmccarter, the SDFat library keeps resetting the SPI mode settings whereas other libraries don’t. I have opened an issue to implement the arduino beginTransaction() and endTransaction() functions. Recently, I had to reset the SPI clock frequency prior to using SPI in my SharpMemory display library. It did not work at the high frequency that SDFat set.


@peekay123, Yeah I’m setting SPI settings each time I switch devices, which is annoying because it happens a lot:

SPI1.setDataMode(SPI_MODE0);          // Enable SPI1*/
SPI1.setBitOrder(MSBFIRST);           // Enable SPI1
SPI1.setClockDivider(SPI_CLOCK_DIV8); // Enable SPI1
SPI1.begin(SS_PIN);                         // Start SPI1

…but rather safe than sorry, right?

Any general advice to get the SD working in my configuration? Maybe an off-board (non-Shield) SD card slot? It would have to use I2C, because my SPI (not SPI1) pins are taken by some of the TFT Shield pins. I don’t really want to cut pins off the Shield and re-route… Just brainstorming here…


@jeremywmccarter, you may not need to set the SPI mode or bitorder and you shouldn’t call begin() every time. All you may need is to set the clock divider. I’ll have to look at the libraries to make sure.


@peekay123 Ok, I’ll try to pare it down a bit. Thank you for that!


@jeremywmccarter, all devices run SPI_MODE0 and MSBFIRST. However, the clock settings differ. For the ili9341, the library calls for SPI_CLOCK_DIV2 (18MHz on older Core), and for the touch library, it calls for SPI_CLOCK_DIV16 (1MHz on arduino). I suggest adding a line of code to each library’s SPI read and write functions (eg. readRegister()/writeRegister() or spiread()/spiwrite()) of each library to set the correct SPI speed. That should be all you need.

ili9341 SPI.setClockSpeed(18, MHZ); // You could also try 30 MHZ!
STMPE610 SPI.setClockSpeed(1, MHZ);



@peekay123 Here is the post from @ScruffR that caused me to add those begin() calls…but perhaps I misunderstood his meaning…


@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?