CAN Bus Programming With Arduino Uno, Mega 2560
About Controller Area Network (CAN)
Controller Area Network (CAN) is a serial network technology that was originally designed for the automotive industry, especially for European cars, but has also become a popular bus in industrial automation as well as other applications. The CAN bus is primarily used in embedded systems, and as its name implies, is a network technology that provides fast communication among microcontrollers up to real-time requirements, eliminating the need for the much more expensive and complex technology of a Dual-Ported RAM.
For more information on Controller Area Network see my post: A Brief Introduction to Controller Area Network
Arduino CAN Shields
Since Controller Area Network (CAN) is predominantly targeted at industrial solutions (versus the vastly more popular USB for non-industrial use such as home and lab), there aren’t too many choices available in the market.
Through some research (i.e. browsing) I found two very similar solutions, and they both work with the same CAN library (as explained in a later chapter). Both solutions use the Microchip MCP2515 CAN controller. Also, both solutions are distributed through worldwide online resources.
Microchip MCP2515 CAN Controller
Microchip Technology’s MCP2515 is a stand-alone Controller Area Network (CAN) controller that implements the CAN specification, version 2.0B. It is capable of transmitting and receiving both standard and extended data and remote frames. The MCP2515 has two acceptance masks and six acceptance filters that are used to filter out unwanted messages, thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via an industry standard Serial Peripheral Interface (SPI).
The features include two receive buffers with prioritized message storage, six 29-bit filters, two 29-bit masks, and three transmit buffers with prioritization and abort features. (Source: Microchip Data Sheet)
Note: CAN specification 2.0B refers to the capability of using standard CAN frames with 11-bit message identifier plus the extended format with a 29-bit message ID.
To download the full MCP2515 data sheet log on to: http://ww1.microchip.com/downloads/en/DeviceDoc/21...
Both CAN shields as described in the following chapters utilize the Microchip MCP2551 CAN transceiver, which converts the internal TTL signals to a differential voltage as demanded by the CAN standard.
CAN-BUS Shield by Seeed Studio
In terms of CAN capabilities, the shield by Seeek Studio provides the same functionality as the one by SK Pang electronics, however, it comes with a much lower price tag, because it does not have any additional components besides the CAN interface.
Over all, the device makes a solid impression, especially since the CAN connection is according to standard and in addition provides CAN connectivity through easily accessible terminals.
- Implements CAN V2.0B at up to 1 Mb/s
- SPI Interface up to 10 MHz
- Standard (11 bit) and extended (29 bit) data and remote frames
- Two receive buffers with prioritized message storage
- Industrial standard 9 pin sub-D connector
- Two LED indicators
- No cables included
To order the Seeed Studio CAN shield, you can use the following resources (or browse for “Arduino CAN-BUS Shield” for further options):
Within the United States and Canada:
- Copperhill Technologies - http://copperhilltech.com/can-bus-shield-for-arduino/
Outside the United States and Canada:
- Seeed Studio - http://www.seeedstudio.com/depot/CANBUS-shield-p-...
Arduino CAN-Bus Shield by SK Pang electronics
This shield by SK Pang electronics provides the Arduino CAN-Bus capability. As explained previously, it uses the Microchip MCP2515 CAN controller with MCP2551 CAN transceiver. The CAN connection is realized via a standard 9-way sub-D, and the pin assignment for CAN_H, CAN_L can be set for ODB-II compliance or the industry’s virtual standard for CAN analyzers (data loggers).
The shield also comes with a uSD card holder, a serial LCD connector, and a connector for an EM406 GPS module, making this shield suitable for data logging application.
- CAN v2.0B up to 1 Mb/s
- High speed SPI Interface (10 MHz)
- Standard and extended data and remote frames
- CAN connection via standard 9-pin sub-D connector
- As an option, power can be supplied to the Arduino by sub-D via resettable fuse and reverse polarity protection.
- Socket for EM406 GPS module
- Micro SD card holder
- Connector for serial LCD
- Reset button
- Joystick control menu navigation control
- Two LED indicator
- No cables included
- Header pins are not included; they must be ordered separately
- Pin assignment for CAN_H, CAN_L not according to standard
All technical information regarding the use of the CAN controller, uSD card holder, joystick, LEDs, etc. can be found on the company’s wiki website at: https://code.google.com/p/skpang/
To order the SK Pang electronics CAN shield, you can use the following resources (or browse for “Arduino CAN-BUS Shield” for further options):
- Sparkfun - https://www.sparkfun.com/products/10039
- SK Pang electronics - http://skpang.co.uk/catalog/arduino-canbus-shield...
Arduino CAN Sketches
The implementation of either one of the introduced CAN-BUS Shields and the corresponding CAN sketches went surprisingly smooth when paired with the right library software.
I found several source codes for accessing the MCP2515 CAN controller, but most of them didn’t even pass the initial quality control phase (I read the code first before I use it). One of the quality criteria was the support for 29-bit CAN message identifiers (CAN 2.0B Compatibility), which is mandatory when it comes to implementing, for instance, the SAE J1939 vehicle network protocol. Some software samples I found were just literally “samples” and they left ample room for guessing games.
I was most pleased by the MCP2515 Library by Cory Fowler, which can be found at https://github.com/coryjfowler/MCP2515_lib
This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller.
Controller Area Network Prototyping with Arduino
While the Arduino is not widely considered an industrial-strength solution, it provides, due to its low price and ease of programming, the perfect prototyping platform for all kinds of Controller Area Network (CAN) applications.
This book, written by a leading expert on CAN technologies, guides the reader through the process of acquiring all necessary hardware and software components, the implementation of the CAN driver, and the implementation of programs (Arduino Sketches) to read, send, process, and display data from and to a CAN network.The collection of programming examples cumulates into a full-fledged USB-to-CAN Gateway communicating with a Windows/Linux PC.
This book will enable you to achieve CAN functionality literally within only a few hours.