SDFat usage on Particle Photon

I have been trying for a few days to get a MicroSD card to work with my Particle Photon. With no success. I am currently almost to the point of bashing my head through a wall or something… But that would be expensive to fix. So I will opt to post on here for help.

Anywho, I purchased a MicroSD shield and am using the First SPI configuration with the Pins as such:
SCK - A3
MISO - A4
MOSI - A5
SS - A2
Gnd - Gnd (Obviously…)
VCC - 3.3v (I tried Vin As well.)

I am also using Particle.publish as a way to debug.

All that I get as published is:
Starting
Fail

I used the code from the TryMeFirst.cc code on Github:

#include "SdFat.h"

// Pick an SPI configuration.
// See SPI configuration section below (comments are for photon).
#define SPI_CONFIGURATION 0
//------------------------------------------------------------------------------
// Setup SPI configuration.
#if SPI_CONFIGURATION == 0
// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFat sd;
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 1
// Secondary SPI with DMA
// SCK => D4, MISO => D3, MOSI => D2, SS => D1
SdFat sd(1);
const uint8_t chipSelect = D1;
#elif SPI_CONFIGURATION == 2
// 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;
#elif SPI_CONFIGURATION == 3
// Software SPI.  Use any digital pins.
// MISO => D5, MOSI => D6, SCK => D7, SS => D0
SdFatSoftSpi<D5, D6, D7> sd;
const uint8_t chipSelect = D0;
#endif  // SPI_CONFIGURATION
//------------------------------------------------------------------------------

File myFile;

void setup() {
  Serial.begin(9600);

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // Change to SPI_FULL_SPEED for more performance.
  Particle.publish("print", "Starting");
  delay(2000);
  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
  Particle.publish("print", "Fail");
    sd.initErrorHalt();
  }
  Particle.publish("print", "SD Card Works!!");

  // open the file for write at end like the "Native SD library"
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  myFile.println("testing 1, 2, 3.");
  myFile.printf("fileSize: %d\n", myFile.fileSize());
  
  // close the file:
  myFile.close();

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) {
    Particle.publish("print", data);
  }
  // close the file:
  myFile.close();
  Particle.publish("print", "Made it to the end");
}

void loop() {
  // nothing happens after setup
}


Thanks for the Help in advance!

What exact MicoSD shield is it?
What SD are you using?
Have you got a photo of your setup?

The SD Shield I am using is this one: https://www.amazon.com/gp/product/B07BJ2P6X6/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1

I am using a 32 gb MicroSD Card.

And as for a photo… I am in bed already. That will have to wait till morning.

How is that SD formatted?
Ext and NTFS are obviouls not supported via SdFat.

AFAICT this board requires a min. 4.5V supply for the voltage regulator which supplies the level shifter.
So you should definetly power the board off of Vin.

The SD card formatting is FAT.
I have switched it to the Vin with no luck.
I have had success with using the SD card with my Arduino Uno.

Image of Setup is here:

That looks fine, so I don’t know what else to suggest since I usually don’t use level shifting SD readers but go for the simple, straight forward ones as the Particle devices already are 3.3V based.
like this


While @rickkas7 often uses the level shifting ones and never had an issue I still can’t vouche for this specific board to properly work with 3.3V SPI levels.

If you are certain the wiring is as per the pin assignment in the code and the SD module is powered off Vin. Then if the same board works with Arduino, it suggests that the Photon has an issue.

Have you tested your SPI pins are working on the photon - install tinker and use a led with 330ohm resistor to GND?

Just looked at your code - the use of SPI_FULL_SPEED requires a weak pullup on the SPI bus lines (100K ohms) - I don’t see that on this board. You could try putting pull-up resistors on the MISO, MOSI pins of 10K to 3V3. Also, I would try SPI_HALF_SPEED.