I'm currently doing a project involving the use of CAN-bus, but i have problems getting my two MCP2515 to communicate (or something else). In short, i have one Controllino Mega (CM) that later in the project will act as a ECU that will send and receive CAN data to control 2 components. But before getting there i want to get to know the CAN protocol some more, and i tried to connect the CM to a Arduino Uno with two MCP2515 modules. So far i have used the example code provided with the MCP2515 library from Autowp, but i cant for the life of me get it to communicate. I've done some searching here on the forum, but all in vain.
Bellow are the schematics (please excuse the wire coloring) and the current code i have (modified example code). Since I'm all new to CAN i wouldn't really know what to tweak to get it to run. Starting to think i might have defect modules. All input/help are welcome and much appreciated.
if you use that, it assumes that your CAN sheilds use 16MHz crystals.
if your schematic is showing the ACTUAL boards your are using then in my experience those CAN sheilds have a 8MHz crystal on them. If that's the case, you should try using this instead:
As long as the modules and settings are identical it does not matter. The bitrate will be wrong on both modules and still work.
@dsgj Do you have access to an oscilloscope? Even an old analog one will be good enough.
I recommend you use one board first. Configure the MCP2515 and send one message. Because you have no other node on the bus the MCP2515 will continuously send the message giving you time to look at the CAN message with your oscilloscope. You should be able to:
see the CAN message
measure the bit length and get the bitrate from that
see a differential signal on the CAN bus
Have a look at the CAN bus Wikipedia page to see what this should look like.
Once this works, check the second board.
If both boards work and have the same bitrate, connect the bus and check again.
You can also try loopback mode and send the message to the same MCP2515. This should work with just one node. Check your library on how to enable Loopback mode. It should be the same as setting normal mode but setting another bit pattern.
Alternatively have a look at other libraries for the MCP2510 or 2515. There are a few out there and some seem to be easier to use than others.
If you rather try to fix the issue with the library you have and the above does not help, have a look at the SPI signals. Ensure the chip select line is set and released around the SPI frame, check the clock and data signals. ...
Thanks so much for fast and elaborate answers! Will have access to an oscilloscope when i get back to the Uni next week, and hopefully that will be to some help. Although I'm starting to think my Uno might have some problems since the serial monitor outputs "Message sent!" 2 loops and then enter the ELSE-state on the "Write code". With the CM it always enters the IF-state. And obviously i can't tell if the "Read code" works if only one of the boards works as it should. But I will get back with you when i get my hands on the oscilloscope. Once again thnx for the help!
The MCP2515 has 3 transmit buffers. If there is no other node on the bus you should see 3 "Message send" and then the TX Error. That means you filled all 3 TX buffers, but the messages could not be send and therefore the buffers never get available again. (This assumes the library makes use of all three TX buffers. It may keep a buffer for some high priority messages. Then the error would appear earlier. e.g., after 2 TX). So, this suggests that this Arduino can actually talk to the MCP2515.