"Error Initializing MCP2515..." using coryjfowler lib

Hi there,

I am trying to send J1939 messages using and Arduino Uno and an MCP2515 board. I have had some basic Arduino classes in school but I am no programmer and have some difficulties getting it to work.
When trying uploading the code to the board I get the "Error Initializing MCP2515..." message. My board is connected to the MCP2515 like this:
VCC -> 5V
GND -> GND
CS -> D10
SO -> D11
SI -> D12
SCK -> D13
INT -> D2
I guess it is a wiring problem but I received a setup on a nano, but I couldn't upload to the nano so I used the Uno I had laying around and copied the wiring to that one.
The code is the sent message template:

// CAN Send Example
//

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

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_250KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);   // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0xFF, 0xFF, 0x95, 0x19, 0x00, 0xFF, 0xFF, 0xFF};

void loop()
{
  // send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
  byte sndStat = CAN0.sendMsgBuf(0x0CF00400, 0, 8, data);
  if(sndStat == CAN_OK){
    Serial.println("Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }
  delay(100);   // send data per 100ms
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

While I'm at it I might as well add that I did not know how to code the correct PGN, I am trying to send a message in pgn61444 and as you can see I changed the message ID to 0x0CF00400 since when I did a bit by bit analysis this was what I came up with.

If any extra info is needed I will gladly provide it!

It looks like that's the case regarding no replies given.
Tray this link and see what You can add: How to get the best out of this forum - Projects / General Guidance - Arduino Forum

Based on your title that is telling me one of two things, it is wired wrong or it is bad. Post an annotated schematic showing exactly how YOU have wired it. Show all power, ground, power sources and any other ancillary hardware involved. If you follow @Railroader suggestion it will let you a schematic is generally needed, word salad does not work very well. There are several MCP2515 boards on the market, which one did you use? Without knowing that I cannot determine if your setup() code is correct.

Hi there,

Thanks for the reply!
As you suggested I have added a wiring diagram to this reply and in addition I have a picture of the MCP module. I think it as knockoff, I am not sure since I received it from someone in the company I am doing an internship at with a "this is all we have, figure it out" notion.
I had read that Chinese board usually have a 8MHz crystal but changing the it to 8 in the setup didn't solve the problem.
What I read as well is that this kind of setup doesn't work unless it receives acknowledgement from another ECU, but is there a way around this? Since at the moment I don't have any other components then the Uno, the MCP module and a Peak CAN analyser.

Thank you!


Swap the SI and SO lines.

2 Likes

It is wired wrong you need to swap MISO and MOSI as suggested by @coryjfowler . Also in your picture you show the crystal, what is the number written on the top of it? My guess is 8.000 which tells me you should change your can init statement from "MCP_ANY, CAN_250KBPS, MCP_16MHZ" to "MCP_ANY, CAN_250KBPS, MCP_8MHZ", If you do not change it will initialize to 125KBPS. In either case you should get "MCP2515 Initialized Successfully!". When that happens and it does not perform the problem most likely is in software or the CAN bus side of the hardware.

When swapping the SI and SO lines I do indeed get the MCP2515 initialized successfully! So this problem is solved. It is a 16MHz crystal so that was not a problem. However now I do get Errors when sending messages. Is this due to the fact that there is no acknowledgement? Or could there be something else?

Thank you guys for the help so far!

1 Like

Assuming it is not connected to the bus you have it correct. If it is connected to the bus do you have the termination jumper installed as your photo shows? Are you receiving messages (this confirms correct bit rate) and the other end is working.

Thank you. I just had the same issue and reversing the wires fixed it.

Thanks for the help!
Yes I have the jumper installed. When testing it on a machine I don't see the message in Busmaster. I use a Peak CanAlayser and can see other messages but not the one I am sending. I think my code is wrong and it doesn't send the correct message ID.
I have no idea how it should be done exactly for an extended frame.

I really appreciate all the help I got here. Got my Arduino part of the project to work thanks to some of you guys! In the end I changed changed the wiring as suggested and figured out that to send an extended frame you just have to change the 0 to 1 in the ID part of the code.
I ended up with the code below:

// CAN Send Example
//

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

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_250KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);   // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0xFF, 0xFF, 0x00, 0x95, 0x19, 0xFF, 0xFF, 0xFF};

void loop()
{
  // send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
  byte sndStat = CAN0.sendMsgBuf(0x0CF00400, 1, 8, data);
  if(sndStat == CAN_OK){
    Serial.println("Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }
  delay(100);   // send data per 100ms
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

I thought adding this reply might help future peeps with knowing what to do when encountering my problem.
With this code sending a J1939 message to a CAN bus works like a charm!
Big thanks to CoryJFowler for the lib.

Please read the section on this about thanking people who helped you.

And consider if you have done this correctly by marking your own work as a soloution.

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