2 SPI Slaves with 2 different SPI modes on one Arduino Mega 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] = &quot;test.txt&quot;;
}

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

///THEN THIS

  //lets confirm SD card is still in socket before writing
  CardDetect = digitalRead(37);
  if(CardDetect == HIGH){
    
     SPI.setDataMode(SPI_MODE0);
     SPI.setClockDivider(SPI_CLOCK_DIV4);
     SPI.setBitOrder(LSBFIRST);
     digitalWrite(sdCard, LOW);
     sd.begin(sdCard, SD_SCK_MHZ(1));
    //LETS START SAVING DATA
          myFile.open(filename, O_WRONLY | O_CREAT | O_EXCL);
          myFile.write(now.unixtime());
          myFile.write(&quot;,&quot;);
            for(int i = 0; i&lt;8; i++){
              myFile.print(TEMP[i]);
              myFile.write(&quot;,&quot;);
            }
          myFile.println();
          myFile.close();
          digitalWrite(sdCard, HIGH);
          SPI.setDataMode(SPI_MODE1);
          SPI.setClockDivider(SPI_CLOCK_DIV16);
          SPI.setBitOrder(MSBFIRST);
          delay(200);
    }

}
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.