CAN-Bus Implementation Issue

Hi! I have been having issues over the past few days with implementing a CAN-bus shield and would really appreciate help. I am using the MCP2515 module with an Arduino UNO. I have tried the following libraries and used the example code provided by those libraries:

  1. mcp_can by coryjfowler
  2. CAN by Sandeep Mistry

The ultimate goal is to read and send messages to and from a Victron Energy Battery Management System which is implementing CAN 2.0A.

I have tried communicating between two Arduino's, and have managed to do this successfully, however, I fail to receive any messages when I try connect the Arduino to CAN hi and CAN lo of the device. I will show the code I am using to receive messages below:

/* 
  MCP2515 Setup
  VCC - 5V
  GND - GND
  SCK - SCK (PIN13)
  SO - MISO (PIN12)
  SI - MOSI (PIN11)
  CS - PIN10
  INT - PIN2
*/

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];                         
char msgString[128];                        // Array to store serial string

#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(10);                               // Set CS to pin 10


void setup()
{
  Serial.begin(115200);
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  
  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
}

void loop()
{
  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
    else
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
  
    Serial.print(msgString);
  
    if((rxId & 0x40000000) == 0x40000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
        Serial.print(msgString);
      }
    }
        
    Serial.println();
  }
}

All it indicates is that the MCP2515 has successfully initialized but there are no interrupts and thus I believe no messages are being received. I am wondering if perhaps messages are being sent too fast for the MCP2515 to be able to register it, or if there's a synchronization issue. I currently have not implemented any masks or filters. I have read the datasheet and I know exactly the messages I am expected to receive but I am unable to receive any. If anyone knows what the issue may be or if there's any other place I can go to for further help on this issue I would appreciate the assistance! Thanks in advance.

Does your "MCP2515 module" have a 16 MHz crystal?

Yes it does

Either the BMS is using a baud rate other than 500k, or it's not sending any data.

Hire or buy an oscilloscope to investigate.

Alternatively, if you have good soldering skills, attach a wire to the transceiver Rx pin and use a spare pin on the Arduino to look for a digital signal by using pulseIn(). The shortest pulse that you detect can be used to calculate the baud rate. (4 micro sec resolution on the Uno R3)

Try reversing the CAN Hi and Low pins on your connection.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.