0

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

///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(",");
            for(int i = 0; i<8; i++){
              myFile.print(TEMP[i]);
              myFile.write(",");
            }
          myFile.println();
          myFile.close();
          digitalWrite(sdCard, HIGH);
          SPI.setDataMode(SPI_MODE1);
          SPI.setClockDivider(SPI_CLOCK_DIV16);
          SPI.setBitOrder(MSBFIRST);
          delay(200);
    }

}
14
  • what is your question?
    – jsotola
    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
    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
    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
    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
    Nov 11, 2020 at 20:22

1 Answer 1

2

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 that you have read and understand our privacy policy and code of conduct.

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