Thursday, September 25, 2014

Using the Seeed CAN-BUS shield with an Arduino Mega


The Seeed Studio CAN-BUS shield is designed specifically to be used with an Arduino Uno but with a simple modification you can use it with several other Arduino boards. I sourced this info from a few different forums and thought I would write up a complete post on exactly how to do this. The Seeed website had some info on how to use the shield with a Mega but their info was not complete and did not work for me. In this post I am going to show how to use the Seeed CAN-BUS shield with an Arduino Mega 2560.

The CAN-BUS shield incompatibility with Arduino boards other than the Uno arises from where Seeed chose to access the SPI pins. They used pins 11,12,13 to access SPI on the Uno. On the Mega the SPI pins are 50,51,52. A better design would have been to use the SPI pins on the ICSP header which is consistent on the Uno, Mega, Due and Leonardo but such is life and we have to work with what is available right now. Seeed's site does mention they are working on a 1.1 version of the board that moves SPI to the ICSP header.

Here is an Arduino Uno with the SPI pins labeled
Arduino Uno SPI pins


And here is an Arduino Mega 2560 with the SPI pins labeled
Arduino Mega 2560 SPI pins


Here is what the Seeed Studio CAN-BUS Shield looks like if you don't already own one
Seeed Studio CAN-BUS Shield v1.0


The mod

Here is how to modify the CAN-BUS Shield to work with a Mega. The basic overview of the mod is that we need to change the SPI pins that the CAN-BUS Shield is using. There are two ways to do this modification. One one is reversible and the other is more permanent. First the reversible method.

The reversible method is to bend three pins on the CAN-BUS Shield like this:


When you plug the shield into the Mega it will look like this:


With bent pins I was able to bend them back use the shield with an Uno again.

Now you need to use three jumper wires to connect the SPI pins on the shield to the SPI pins on the Arduino board. In my example I connected the wires to pins 50,51,52 on the Arduino because I only had male-male jumper wires. If you have male-female jumper wires you can connect to the ICSP header instead.

Jumper wire connections
Mega Pin Shield Pin SPI Desc
50 12 MISO
51 11 MOSI
52 13 SCK


Here is what it looks like with the jumper wires installed




I would recommend you use coryjfowler's MCP2515 library instead of the Seeed's because his fork of the library has a configureable SS pin but Seeed's version will work with this mod because they hard coded the SS pin as digital pin 10.

I then loaded up one of the examples and the shield started up correctly.


If you want to make this modification more permanent you can simply snip off the three pins instead of bending them.



I found some male-female jumper wires at Fry's Electronics yesterday. Here is what it looks like when using the ICSP headers


ICSP Pin Shield Pin SPI Desc
1 12 MISO
4 11 MOSI
3 13 SCK


Sources
http://arduino.cc/en/Reference/SPI
http://forum.arduino.cc/index.php?topic=123367.0
http://www.seeedstudio.com/forum/viewtopic.php?f=23&t=5172
http://www.seeedstudio.com/wiki/Talk:CAN-BUS_Shield
http://www.seeedstudio.com/wiki/CAN-BUS_Shield


[Updated 2014-09-29] - Added photos of shield with the pins snipped off. Also added photo showing the male-female jumpers with the ICSP header.

[Updated 2014-10-23] - Added table with pinouts for connecting the shield to the ICSP header.

6 comments:

  1. Hello,

    Thank you for this post, I currently working on CAN-BUS shield from seeed studio and Arduino Mega too.

    But unfortunately, even I have tried some tutorial from forum and also your post, I couldn't make mine working.

    Is there something that I need to change on the coryjfowler code?

    Thank you...

    ReplyDelete
    Replies
    1. His fork is a works a little differently than the my original post. His code includes send and receive examples which should work with your Mega. Take a look here:

      https://github.com/coryjfowler/MCP2515_lib/tree/master/examples

      Delete
  2. Great guide and very well written! I just have to ask, I only have access to a CAN-BUS Shield v1.2, does it require any sort of modification to function with the Arduino Mega 2560? Thanks in advance!

    ReplyDelete
    Replies
    1. I believe Seeed moved the SPI connection to the ICSP header on the 1.2 version of the shield. If that is correct then you would not need to modify the shield like I did.

      Delete
  3. Hi Matt, excellent guide.I am currently using sparkfun shield with an Arduino Mega 2560. I am still a bit confused about the coryjfowler library, do I need to adjust anything within the code? Cause in the other libraries I have attempted using, there were some software tweaking required due to the pin location of the MOSI, MISO, SCK and SS in the arduino Mega. Does the coryjfowler account for this? cause I am relatively a newbie and it can be difficult understanding everything within the code or is the code plug and play without any necessary software adjustments? I tried the example send message code in the coryjfowler library with my setup (without changing anything in the software) and I get

    "Entering Configuration Mode Failure...
    Error Initializing MCP2515...
    Message Sent Successfully!"

    Is this the behaviour expected or am I doing something wrong. Thanks in advance for your help.

    ReplyDelete
    Replies
    1. If you use the CAN_send.ino example included with coryjfowler library you shouldn't have to change anything. I looked at the library code and that error either has something to do with setting mask filters or using loopback mode. Do you have some way of testing if the messages were sent successfully? Like a CAN bus sniffer or a second Arduino with a CAN shield running the receive example.

      Delete

Please note all comments are moderated by me before they appear on the site. It may take a day or so for me to get to them. Thanks for your feedback.