I am working on a project where I read 8 MAX31856 thermocouple sensors using the Adafruit Library. This works just fine and prints the read outs to serial.

I want to also save the readings to a microSD card. And this is where I hit a snag.

I am trying to use the SdFat.h library. I can loop through my MAX31856 thermocouples, and write once to my SD card, but after that it hangs the SPI bus.

the MAX31856 works on SPI_MODE1 (as per Adafruit Library) and the microSD card works on SPI_MODE0, I believe. I am trying to switch SPI bus modes before and after writing to the SD card but I think there is something wrong after first successful write to the SD card. After the first loop iteration, it seems like all data is basically noise on the SPI bus.

I am using the Adafruit ADA254 microSD breakout Board, which has a card detect pin, so I first check to see if an SD card is inserted, and then write to the file.

Question: Is there another or better way to switch SPI_MODES on each loop? is there anything more I need to consider so I end up back to SPI_MODE1 appropriately for the MAX31856 thermocouples?

Below is the pertinent code for what I have going on...

void setup(){
   int sdCard = 36;
   char filename[10] = "test.txt";

void loop(){
....code to read thermocouples ...


  //lets confirm SD card is still in socket before writing
  CardDetect = digitalRead(37);
  if(CardDetect == HIGH){
     digitalWrite(sdCard, LOW);
     sd.begin(sdCard, SD_SCK_MHZ(1));
          myFile.open(filename, O_WRONLY | O_CREAT | O_EXCL);
            for(int i = 0; i<8; i++){
          digitalWrite(sdCard, HIGH);

  • what is your question?
    – jsotola
    Commented Nov 11, 2020 at 20:00
  • if your question is how do I debug the code?, then remove all sensor code and see if file is written without crashing
    – jsotola
    Commented Nov 11, 2020 at 20:03
  • @jsotola - the file is writing no problem on first iteration. However, after the first loop of the code, the SPI bus 'glitches' and I does not read sensors correctly
    – NRav
    Commented Nov 11, 2020 at 20:06
  • 2
    This is more likely the SD card or level translator itself interfering with the SPI bus. I never share an SPI bus with an SD card - always have it on its own dedicated bus.
    – Majenko
    Commented Nov 11, 2020 at 20:19
  • @Majenko I feared this... I needed all the digital pins, hence why the project is built around the arduino Mega - any way to add a second SPI bus (software maybe)? Wait - are you the author of: github.com/MajenkoLibraries/SoftSPI ?
    – NRav
    Commented Nov 11, 2020 at 20:22

1 Answer 1


For anyone who has a similar issue with the SPI Bus, SdFat library has a Software SPI option built in (example here).

You need to modify your SdFat/SdFatConfig.h file, but that is minimal. Once you have the microSD card module on a separate bus, it will not corrupt the main SPI Bus.

Switching the SD Card module to it's own bus rectified the issue. Keep in mind that Software SPI comes with a speed penalty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.