MCP2515 not working on Arduino Nano clone

Hey y'all!

I'm pretty new to this Arduino stuff. I'm working on what I thought would be a simple project. My car is lacking a screen for displaying tyre pressure. It has a TPMS, but there's only a LED signaling that the tyre pressure is too low. No information on which tyre or exact pressures. My idea was to utiliize the CAN bus to get these information and then show it on a simple monochrome display, but I can't seem to get two Arduino Nano clones to communicate over a MCP2515.

This is my scematic (I know it's a different Nano, but the pinout is identical and I haven't found a high res image of mine):

Both MCUs are powered via their USB connector.

I'm using the arduino-mcp2515 library in version 1.2.1. The sketches are taken straight from the examples of said library, with only few adjustments described in the comments.

This is the sketch on the sending MCU:

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);


void setup() {
  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x8E;
  canMsg1.data[1] = 0x87;
  canMsg1.data[2] = 0x32;
  canMsg1.data[3] = 0xFA;
  canMsg1.data[4] = 0x26;
  canMsg1.data[5] = 0x8E;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 0x0E;
  canMsg2.data[1] = 0x00;
  canMsg2.data[2] = 0x00;
  canMsg2.data[3] = 0x08;
  canMsg2.data[4] = 0x01;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;
  
  while (!Serial);
  Serial.begin(115200);
  
  mcp2515.reset();
  // The crystal on my MCP2515 reads "8.000",
  // so I set the oscillator frequency to 8 MHz.
  mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}

void loop() {
  mcp2515.sendMessage(&canMsg1);
  mcp2515.sendMessage(&canMsg2);

  Serial.println("Messages sent");
  
  delay(100);
}

This is the sketch on the receiving MCU:

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  
  mcp2515.reset();
  // Same as above: Frequency set to 8 MHz.
  mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();      
  }
}

The sending sketch seems to send CAN frames. Return value of MCP2515::sendMessage(...) is 0/MCP2515::ERROR_OK the first two or three times, then it's 2/MCP2515::ERROR_ALLTXBUSY. After setting the jumper to use the 120 Ω resistor, it seems to always be 0/MCP2515::ERROR_OK.

The receiving sketch receives nothing. Return value of MCP2515::readMessage(...) is always 5/MCP2515::ERROR_NOMSG.

I have multiple Arduino Nano MCUs and MCP2515 modules lying around. I tried all of them in different combinations, so I can rule out broken hardware.

As the two MCP2515 modules are the ends of the bus, both are having the jumper for the 120 Ω termination resistor set. I tried it without the jumper, but to no avail.

Does anyone know what I'm doing wrong?

this 10 is an address of slave, listener device, it answers on this Id, afaik
120 Ω needed on long wires

Is the clone board otherwise working OK? Can you upload basic codes to it?

Could you elaborate? According to the library docs this is the pin where SPI CS is connected, so 10 on both MCUs.

Yep, they're working fine! I even connected a I2C display to it and printed some fake tire pressure data.

Maybe 120 ohm terminating resistors at each device would help?

I thought a 120 Ohm resistor is already featured on the board and that jumper is just to enable it, like I did. Isn't that correct?

It appears if you short J1, it should activate a 120 ohm resistor.
It was just a thought.

I have the same setup and it works fine, have about 20 working nodes. I never tried short wires, I have at least a meter/yard in the CAN bus. My china Nano work just fine but they do not look like your picture. they look like this:
image
Do both of the CAN modules have the same value of crystal? does it work with the samples in Cory's library? The best thing is to get that working first. The jumpers, J1 in your picture which you show jumped need to be that way or it will not work. That is the termination jumper which needs to be at each physical end. Do not add any more termination. Using your scope's differential prob you should see the data on the bus. If you do not have a scope you can get an inexpensive USB analyzer for less then $10.00 US.

Yep, both are 8 MHz. Both have "8.000" printed on them.

I guess you mean coryjfowler/MCP_CAN_lib? I just tried the CAN_send.ino and CAN_receive.ino examples and only changed the crystal frequency to MCP_8MHZ. The following is the output on the sender side:

Entering Configuration Mode Failure...
Error Initializing MCP2515...
Message Sent Successfully!
Message Sent Successfully!
Message Sent Successfully!
[Repeat every 100 ms...]

And this is the receiving side output:

Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
MCP2515 Library Receive Example...

That how I understood it, too, yes. Tbh I'm kinda surprised how simple CAN really is. :slight_smile:

Will do!

Jesus Christ, I feel so ashamed. One of the wires was broken. I replaced them one by one and it's working!

Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
MCP2515 Library Receive Example...
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
Standard ID: 0x100       DLC: 8  Data: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
1 Like