Problem with CAN BUS and SD Card Shield on the UNO

Hi there,

i want to use my Arduino UNO to receive data via this CAN USB Shield and save the incoming data via this SD Card Shield. Using either of the shields seperately works fine, but i can't get them to work together on the same UNO.

I boiled it down to this very simple code:

#include <SPI.h>
#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <SD.h>

#define SPI_CS_PIN 9                                  //Define CS pin
MCP_CAN CAN(SPI_CS_PIN);                         //Sets the CS pin for CAN communication, I think...

void setup() {
  Serial.begin(115200);                                //Starts serial communication with Baudrate = 115200
  while (!Serial) {
                                                  // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Serial initialized");               //This is just for debugging purposes

  //SD.begin();
  if (!SD.begin(4)) {
      Serial.println("SD initialization failed!");    //To see which initialization fails
      return;
  }
  
  if (CAN_OK == CAN.begin(CAN_500KBPS)){
    Serial.println("CAN initialization failed!");     //To see which initialization fails
    return;
  }
  
  
Serial.println("It works!");                          //This is just for debugging purposes
}

void loop() {
  
//Nothing here

}

If I run this it shows me "CAN initialization failed". If I put

if (CAN_OK == CAN.begin(CAN_500KBPS)){
    Serial.println("CAN initialization failed!");     //To see which initialization fails
    return;
  }

before

//SD.begin();
  if (!SD.begin(4)) {
      Serial.println("SD initialization failed!");    //To see which initialization fails
      return;
  }

, it still shows, that CAN couldn't be initialized. In a more complex code I had before, it was always the SD Card Shield, which didn't seem to work.

As far as I can see, it is propably because of the UNO only having one serial connection, but I don't understand why that is a problem. I defined different CS pins (4 for SD and 9 for CAN) and none of the shields actively tries to use (block?) the serial. I am just checking if the shield is ready for use.

Can someone pinpoint me in the right direction? I have read several other posts here, but it was never really the same problem. I read about SoftwareSerial, but I don't know how to make that work. I tried to wire the SD Card Shield with jumper wires and just connected pin 0 and 1 of the SD Shield to the SoftwareSerial pins I defined in the code, but that didn't seem to work.

I am out of ideas as of right now and any help is appreciated,

thank you!

Okay, first of all, there was a mistake in if (CAN_OK == CAN.begin(CAN_500KBPS)), which of course is supposed to say '!=' instead of '=='. Secondly,I don't know what happened, but it just works now! I worked for days on this and just started doint it all again and it seems like i missed out on a mistake i did before :smiley:

I can read data from the CAN BUS Shield and save it on the SD card.

This can be closed then, i guess!