Intermittend SPI issue (with mcp2515 CAN and SD card)


I have a larger project on a mega with multiple components on a custom PCB. Two of those components work with SPI; SD-Card and MCP2515 CAN bus.

The SD card is working for some time already and we are trying to get the CAN bus to work for the last two days... but no joy...

The strange thing is that it worked once the first time we started an example sketch yesterday using the coryjfolwer MCP_CAN_lib (in loopback mode) but after recompiling and uploading again we did not get it to work anymore.

Today evening we tried again, and again it only worked once and failed after compiling and uploading a second time.

We did not (actively) access the SD card in between.

We tried several things, using a MCP2515 different library, powering off for 15 mins, made sure CS pin of SD was high, checked all wiring thrice, set spi_mode to 3 explicitely in the sketch but it remains unresponsive.

Trying the same canbus shield on a seperate mega works fine.

Any idea's in which area we should search? Is there anything persistently stored/retained which requires longer power down time...

Any tips hints are welcome.


Is the SD card in the slot? Was it there when it worked?

What component are you using to reduce the Mega's 5V I/O down to the 3.3V required by the SD card?

What voltage is the MCP2515?

Can you give us a schematic or do we have to keep guessing?

Hi Morgan,

Yes SD card has been inserted all the time.

I use a Niren Can bus module and a SD shield which have an on-board voltage regulator and use 5V. Both are connected to the PCB using header connectors.

There are a lot of components/in-outputs connected to the mega. In below image I show only the SPI related components/connections and power supply.

The SPI lines that go to the 16 pin connector are routed to the outside of the encasing to be able to connect other external SPI devices or another arduino as slave but this is not used at the moment.
I did connect a second can module to it to rule out that reflections due to the open endings are causing the issue.

Hope this will remove some guesswork :slight_smile: Let me know if you need more details.


(PS While creating the image I got to wonder, would the fact that the can and SD module get 5V from a separate power supply and the mega using internal 5V possibly be causing the issue?)

just found this post:

Looks a lot like my issue, I am going to try this mod. Will post the result.

screwed up my SD shield trying to solder the very, very very small pins. I removed it and now MCP2515 works. Have to look for another SD shield and try the mod again. Will post if succesfull

the solution in the post (SOLVED. Nrf24 (Mirf lib) + Micro SD-card works OK together - Storage - Arduino Forum) worked for me. Now I can continue testing. The structural solution will be additional tri-state buffer(s) on the pcb.


I was battling to get the SD CARD to Work with MCP_CAN_LIB a library for the MCP2515 CANController my findings was that the SPI Settings was not the same for the two libraries the SD CARD begin function calls for half speed and the mcp lib calls for 1Mbps my solution was to call the sd.begin(cs_PIN) function whenever i needed to save data to the SD card this is also done in the mcp library i am using every transaction it gives the SPI settings this way the two can work well together

example of sending the data received via CAN BUS

void WriteToSD() {

//CALL The Following function to set the SPI bus with the settings that the SD CARD NEEDS
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
 // SD CARD Portion
 String dataString = "";
 dataString += "YAW = ";
 dataString += (float)YAW / 10;
 dataString += "\t";
 dataString += "Pitch = ";
 dataString += (float)PITCH / 10;
 dataString += "\t";
 dataString += "Roll = ";
 dataString += (float)ROLL / 10;
 dataString += "\t";

 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile ="datalog.txt", FILE_WRITE);

 // if the file is available, write to it:
 if (dataFile) {
   // print to the serial port too:
 else {
   Serial.println("error opening datalog.txt");