Go Down

Topic: Yet another CAN library for J1939, CANopen and CAN support (Read 21452 times) previous topic - next topic


May 08, 2014, 05:18 pm Last Edit: May 08, 2014, 05:20 pm by 43DR0 Reason: 1
Hey everyone,

I just finished putting together a CAN library to use with MCP2515 CAN controller. It's a mix of most of the MCP2515 libraries out there and hopefully more user "Arduino" friendly.  I was very frustrated with most of the libraries out there as they were made either hardware/shield specific or they mixed in OBD2 protocols to read codes, GPS location, etc... and there was very little documentation/examples.

What I tried to do is put together the best features of most of these libraries and use  known functions like read(), send(), begin(). I have also added message structures for  CAN (standard, extended), J1939 and CANopen with the correct message format (see the examples).

Its not shield specific so you can use it with Sparkfun, Seedstudio or your own shield.  SPI CS is selectable through the sketch and you can control several MCP2515 controllers with one arduino as long as they don't have the same CS (I have done up to 4). The main goal was to have this used in home automation, car/truck/boat applications or simply to send messages between two or more Arduinos.

I have added some examples to the gitHub but I will have more coming, I will also be putting together a document with each function available so there is no need to go back and fort between sketch and header file.

Since I have taken features from so many libraries I can't tell what came from where by now, so in order not to violate any GPL, LGPL or any other possible license out there I am trying to give credit where credit is due, see the libraries for all the acknowledgments. I am only a contributor to this library so some functions/work there might not be mine. I can only take credit for putting it together and releasing back to the public to make any variation as needed.

If you guys see any features you would like added, removed and modified let me know or send them through gitHub. I will be adding end(), clear(), and available().

I am also working on a CANboard with the Atmega328p, MCP2515 CAN controller and MCP2561/62 transceiver all in one so a CAN shield will not be required and you will have all the Arduino and MCP2515 pins/fucntionality available so you can use any shields you want like wifi, lcd screens, etc (SPI pins will be shared but CS will be selectable between pins 6,7 or 8 ) .

I haven't decided what connector to use yet, the db9  is a little bit of an overkill but it will work with any CAN cable out there (since I will make sure pin 2 is CANL and pin 7 is CAN H not like the odd sparkfun and seedstudio shields...). My prototype currently uses Molex Mini fit Jr 4 pin (CANh, CANl, V and GND) since they are cheap and can make my own cable. If anybody has some input I can look and see what can fit (RJ45 is an option) or just screw in connectors.  If there is enough interest I can make a larger batch and make it available to you guys through Tindie.

Let me know what you guys think about the library!  https://github.com/DRO/CAN-Library



May 08, 2014, 05:20 pm Last Edit: May 18, 2014, 09:10 pm by 43DR0 Reason: 1
Added functions description document to Github



on all your example codes im getting an error
I've tried rewritting it simplified but always end up with this error
Im writing an extended frame message also, with the standard frame removed and also removed from loop

can.h:472: error: 'ID' does not name a type


Never mind i fixed it in can.h   

unsigned long ID   // Full Identifier.

   unsigned long ID;   // Full Identifier


Thanks for catching that seanyt, I must have hit delete without noticing.

Library has been updated.

Let me know your thoughts and if you need any help.




Many thanks to DRO for starting this off.

In cooperation with DRO, I've forked the library and started incorporating changes to support three things:

  • Making the library more "generic" (where write() and read() don't depend on specific hardware implementations at the highest level)

  • Ensuring the library follows the Arduino 1.5 library standard

  • Supporting a wider variety of hardware

For the first, I'm attempting to follow the example laid out by the Adafruit_Sensor libraries (https://github.com/adafruit/Adafruit_Sensor). Their implementation of a unified set of sensor libraries across a wide variety of chips is simple and elegant.

Since the library specification isn't set in stone just yet, the second point is a bit of a moving target. However, I have followed the specification page at https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification and am continuing to work on improving both the documentation and the formatting.

For the third example, I've started "roughing in" the SAM3X8E CAN libraries, first from the Arduino CAN branch (https://github.com/arduino/Arduino/tree/can) as well as newer changes from both Collin Kidder (https://github.com/collin80/due_can) and Daniel Kasamis (https://github.com/togglebit/ArduinoDUE_OBD_FreeRunningCAN). Also on the list is incorporating the latest changes by Fabian Greif to his avr-can-lib (https://github.com/dergraaf/avr-can-lib) to also support AT90CAN and SJA1000.



The example sketches all return the error "error: 'MCP' does not name a type".  Any help would be appreciated.



I've been working on making this library work across three different CAN controllers with two different versions of the Arduino IDE, so I might have broken something in the meantime. I'm not quite done yet, but I do have the code in my repository in a tested and usable state. This will eventually be pulled into the main branch, but take a look at https://github.com/McNeight/CAN-Library/



Yep, the code in your repository works perfectly with my MCP2515. Thanks to you and 43DRO for doing this. :)



Thanks for the hard work as well. Looks like you have been doing a lot! Sorry I haven't done much with the library (new job, baby and other projects are getting in the way). I will wait until all your changes are merged and see where we are at. I was thinking about adding Sleep() or off() for those projects that need low power consumption and still trying to come up with a better way for the mask/filter. On the mean time I have been working on a board using MCP25625 and was looking at Teensy 3.1 since it has native CAN but the library they have is FlexCAN only (Not too familiar with it, seems fairly straight forward), I am thinking about ordering a teensy 3.1 and start playing with it.   I noticed you tested the library with it, how was it?

I placed an order with Atmel for a sample of their Atmega32C1 and At90CAN32 but I haven't received them yet. Eventually I want to test those as well but that is a bigger task as it will involve some custom bootloaders and I haven't done any of that but you gotta start somewhere right

Thanks for the hard work Neil!!! I am hoping Massimo and the other guys from Arduino take notice of this effort and maybe give us a hand or try to incorporate CAN as part of their future libraries. Hopefully this will standardize the use of it no matter what CAN controller is used.


If you whant to play with the AT90CAN you should go for the olimex boards, they have on header only and one that has a can transcriver included, the firstone can be programmed with the USBtiny the second one iwth a JTAG ice.
I have one and i'm curious to see if your library is compatible and i'm happy to do some tests, i'm a bit newbie so i'm not sure i can be helpfoul.


Sep 03, 2014, 11:11 pm Last Edit: Sep 03, 2014, 11:40 pm by Sonnyo Reason: 1
So, forgive my ignorance here.

I am very familiar with CAN, and have been programming Arduinos for a couple years now. I have never tried to use other people's libraries though and my projects have been somewhat simple.

I am using the MCP2515 CAN Controller. I downloaded the files in Mcneight's repository, including one of the examples, and put them all in a folder. I tried to compile the sketch and am getting several errors:

ReceiveAnyMessages.ino: In function 'void setup()':
ReceiveAnyMessages:45: error: 'CAN' was not declared in this scope
ReceiveAnyMessages:45: error: 'CAN_BPS_500K' was not declared in this scope
ReceiveAnyMessages.ino: In function 'void readMessage()':
ReceiveAnyMessages:61: error: 'CAN' was not declared in this scope

Could somebody give me some quick guidance on what needs to be done to use this library? Do I need to tell the IDE to use the files other than using #include?



Coding Badly

"A folder"?  Seems rather arbitrary.

Libraries belong in a specific location...


I've imported the library into the Arduino 1.0 IDE where is setup the lincomatic work to compile for the AT90CAN.
Lincomatic example compiles right and uploades ok.
I've tryed the provided example from McNeight repository but i got this error:

In file included from ReceiveCANMessages.ino:21:
C:\Program Files\arduino-1.0\libraries\CANLibrarymaster/CAN.h:126:2: error: #error "Your CAN controller is currently unsupported."
In file included from ReceiveCANMessages.ino:22:
C:\Program Files\arduino-1.0\libraries\CANLibrarymaster/CAN_AT90CAN.h:40:1: error: unterminated #if
ReceiveCANMessages.ino: In function 'void setup()':
ReceiveCANMessages:34: error: 'CAN' was not declared in this scope
ReceiveCANMessages.ino: In function 'void readMessage()':
ReceiveCANMessages:50: error: 'CAN' was not declared in this scope


If i try to compile the sketch on the 1.5 ide i got this error:

Arduino:1.5.7 (Windows 7), Scheda:"AT90CAN-based board"

ReceiveCANMessages.ino:20:21: fatal error: Arduino.h: No such file or directory
compilation terminated.

  Questo report potrebbe essere piĆ¹ ricco
di informazioni con
  "Mostra un output dettagliato durante la compilazione"
  abilitato in "File > Impostazioni"

Go Up