Unable to connect to connect 2 SPI devices

Hi All,

I am having a problem where I am unable to connect 2 SPI devices to my MkrZero. One device is a CAN shield and the other is an Ethernet Shield .

Interesting thing is that if I disconnect the SPI pins from one the other will connect just fine. All the code in my sketch works, just not at the same time. I don’t even have to remove all the pins, just disconnect the MISO. My code is set up with the initialize functions in a loop so that the chip will keep trying to initialize the connection until it does. If the connection doesn’t initialize the program will keep going in a loop.

If I leave the MISO pin for one disconnected then the other will boot up. And when I connect the pin again while the other is in it’s initialization loop the connection will boot up, but it seems to destroy the other connection.

With both connections fully connected Ethernet tries to boot up first but it’s CS pin is held high while the CAN cs pin is low. It doesn’t initialize. When I pull out the MISO pin of the CAN Ethernet boots up and the sketch tries to boot up CAN. It can’t because it’s pin is disconnected, and both CS pins are held high. Plugging the MISO pin back in allows the CAN to connect and after this connection happens both CS are held high. I know this destroys the Ethernet connection because the sketch tries to initialize losant through Ethernet next but is unable to do so.

I’ve also tried controlling the CS pins manually but it doesn’t work.

I’m trying to figure out what is causing this behavior. One guess is that a library isn’t setting the CS pin high when it’s done with the connection. Another is that a shield isn’t obeying the SPI protocol completely such as not relinquishing control when it should.

Does anybody have any ideas of how I can fix this or what else I can look into?

Here is the relevant code, my program never gets to the loop so I didn’t include that.
I do have a Losant device ID, key, and secret, but have removed them here for security reasons.

#include <CAN.h>              //https://github.com/sandeepmistry/arduino-CAN
#include <MCP2515.h>          //https://github.com/sandeepmistry/arduino-CAN Changes CS and Int pins from Uno to MkrZero configuration
#include <Ethernet.h>
#include <ArduinoJson.hpp>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <Losant.h>
#include <SPI.h>

//Ethernet setup
#define MQTT_MAX_TRANSFER_SIZE 80  //this was done as a result of troubleshooting
byte mac[] = {  0x2C, 0xF7, 0xF1, 0x08, 0x19, 0x2C };
IPAddress ip (192, 168, 0, 1);
EthernetClient client;

// ** Configure the Losant credentials to enable comms over mqtt **
const char* LOSANT_DEVICE_ID = "";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";
LosantDevice device(LOSANT_DEVICE_ID);

// The setup() function runs once each time the micro-controller starts
void setup()
{
  Serial.begin(9600);
  while(!Serial) {  }
  Serial.println("started UART");
  //pinMode(BUTTON_PIN, INPUT); 
  
  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  //starting ethernet
  Ethernet.init(6); //changing Ethernet shield software select pin to 6 since default is an SPI transfer pin
  Serial.println("ethernet about to start");
  while(!Ethernet.begin(mac))  {}
  Serial.println("ethernet started");

  while (!CAN.begin(200E3)) {
    Serial.println("Starting CAN failed! ");
    delay(250);
  }
  Serial.println("CAN started");
  delay(100);   

  Serial.println("Connecting to Losant");
  connect(); //connecting to losant
  Serial.println("Connected to Losant");
}

Does anybody have any ideas of how I can fix this or what else I can look into?

The CAN shield is made for 5V boards and will probably cause the problems. You might try to use a level converter to fix that but be sure to have one that can be activated by the low level of the CS line.
A 74HC125 might do the job.

Thanks for the reply,
I am aware of the difference and am using a 74HC125 chip to match the 5V to the 3v3.

I am aware of the difference and am using a 74HC125 chip to match the 5V to the 3v3.

Something you forgot to tell us?

Post a complete wiring diagram of your setup.