Introduction to CAN BUS and How to use it with Arduino

Ever heard of CAN BUS but do not understand or know what it means? No worries, as through this article, we will cover all about CAN BUS from

  • Introduction to CAN BUS
    • CAN BUS wires involved
    • CAN BUS protocol speed and range
    • CAN BUS message
  • Why use CAN BUS protocol
  • CAN BUS vs OBD2
  • Seeed CAN BUS products
  • Getting started with CAN BUS with Arduino
  • CAN BUS Project Ideas

Introduction to CAN BUS

  • CAN which stands for Controller Area Network is used to allow microcontrollers and devices to communicate with each other within a vehicle without a host computer
    which allow for control and data acquisition. These devices are also called Electronic Control Units (ECU) and they enable communication between all parts of a vehicle.
    • ECU in an automotive CAN BUS system can be an energy control unit, airbag, audio system, etc. Today, you can find up to 70 ECUs in a modern car.
    • In layman term, the CAN BUS involves a bunch of linked ECUs within a vehicle that communicate with each other based on a broadcast. Every ECU intercepts every broadcast, but individually decide whether or not to react to it.
  • CAN is a serial communication bus designed for industrial and automotive applications. For example, they are found in vehicles, farming equipment, industrial environments, etc
    • Why not use other communication peripherals like UART, SPI and I2C?
    • Well, compared to other communication protocols like UART, SPI and I2C, using CAN BUS communication protocol are much more reliable as they are standard automotive communication protocols that are used to transmit vital data like a throttle position in a vehicle. If miscommunication or loss of data occurs, it could lead to critical failures.
Ref: Volkspage
  • Without CAN BUS protocol, electronic modules in vehicles will have to communicate with each other using direct, point-to-point analog signal lines. With each module requiring a direct line connected for communication, not only is it time-consuming, it will be messy with all the excessive amount of wiring as seen on the picture above
Ref: Volkspage
  • With CAN BUS protocol, it eliminates the need of all these wiring by enabling electronic devices to communicate with each other with a single multiplex wire that connects each node in the network to the main dashboard as seen on the picture above.
    • The multiplex wire and architecture enable signals to be combined and transmitted over the entire network with just a single wire while ensuring each electronic module in vehicles receives data from sensors and actuators. This allows the user to be able to connect any number of ECUs in your vehicle through the two-wire bus.
    • It also allows for several features to be added via just software. Furthermore, an ECU is able to use data from another ECU which eliminates the need to install the same sensors in multiple devices.
  • With a CAN BUS system, you can now network electronic modules such as control units or intelligent sensors such as the wheel angle sensor.

CAN BUS wires involved

  • The CAN BUS protocol consists of two wires for bi-directional data transmission as shown above which are
    • CAN_H (CAN High)
    • CAN_L (CAN Low)
  • The wires act as a differential line which means the CAN signal either 0 or 1, can be represented by the potential difference between the two wires
    • For example, if the difference is positive and large than a certain minimum voltage = 1. If the difference is negative = 0
  • For CAN termination, as you can see from the picture above, a single 120 Ohm is generally used at the 2 ends of the CAN network.

CAN BUS protocol speed and range

  • Communication speeds of the CAN BUS protocol ranges from 10kpbs to 1Mbps.
    • The speed also depends on the length of wire used. The shorter it is, the faster the communication speed and the longer it is, the slower the communication speed.
    • For example, at 40 meters, the speed will be at 1Mbps. At 1000 meters, the speed can be at 50kpbs.
  • The node distance is generally advised to be no more than 0.3 meters / 1 foot.

CAN Message

  • To fully understand how the CAN BUS protocol works, let us look at the frames sent over the network.
  • The CAN message will contain many segments but 2 main segments would be the identifier and data which helps to transmit messages in the CAN BUS.
  • The identifier is used to identify CAN devices in a CAN network while data will be the sensor or control data that have to be sent from one device to another.
  • The identifier or CAN ID is either 11 or 29 bits in length depending on the type of CAN protocol used.
    • Standard CAN = 11 bit
    • Extended CAN = 29 CAN
  • While the data can be anywhere from 0 to 8 bytes.

Why use CAN BUS protocol?

Using the CAN BUS protocol has several advantages and benefits from:

Low Cost

  • ECUs communicates via a single CAN interface compared to, for example, direct analog signal lines which reduce errors, weight, and costs.
  • With its multiplex wiring that combines analog and digital signals and their transmission over a shared medium, it reduces the amount of wiring needed.

Centralized

  • As CAN BUS supports centralized control over electronic devices that are connected to the network, it allows for central error diagnosis and configuration across all ECUs
  • Error handling is also built into the CAN protocol where nodes can check for errors in transmission while maintaining its own error counter. For example, the protocol supports different error detection capabilities such as bit error, ack error, form error, CRC error, etc.

Flexible

  • As each CAN connected ECU can receive all transmitted messages, it can also decide whether it is relevant and act accordingly.
  • The CAN BUS protocol is also a message-based communication protocol where nodes on the bus have no identifying information.
  • With the above features, nodes can easily be added or remove and modified.
  • For beginners, it will be easy to integrate new electronic devices into the CAN BUS network without any significant programming overhead.

Robust

  • When choosing a communication protocol, durability and reliability are very important. You would want your communication protocol to be self-sustaining and durable for a long period of time without maintenance.
  • With the CAN BUS, the system is robust towards electric disturbances and electromagnetic interference which makes it ideal for vehicles.

Efficient

  • CAN messages frames are prioritized by ID where the top priority will get bus access and yet frames would not be interrupted.
  • Due to flash programming, it also saves time together with less and simple wiring.

CAN BUS vs OBD2

When it comes to CAN BUS, I am pretty sure you will come across other “higher-layer protocols” that are related to CAN BUS like OBD2 and they are not the same!

  • This is as for CAN standard, it does not specify how to handle messages larger than 8 bytes or how to decode the raw data. Therefore, a set of standardized protocols are developed to further specify how data is communicated between ECUs of a given network and OBD2 is one of them.
  • OBD which stands for on-board diagnostics is your vehicle built-in self-diagnostic system. OBD2 can use one of (many) different bus systems to transfer diagnostic data from and to your car.
  • One simple analogy I would use to differentiate between two of them will be OBD2 is like a language that we speak where we use CAN as a communication device like a phone to talk to someone which in this case a vehicle and its state of health.
  • To read more about OBD2, you can check out the Wiki Page on OBD.

Seeed CAN BUS products

Here at Seeed, we offer a variety of CAN BUS products for your project needs! They are:

CAN-BUS Shield V2 ($24.50)

  • This CAN-BUS Shield adopts MCP2515 CAN Bus controller with SPI interface and MCP2551 CAN transceiver to give your Arduino/Seeeduino CAN-BUS capability. With an OBD-II converter cable added on and the OBD-II library imported, you are ready to build an onboard diagnostic device or data logger.
  • To make this CAN BUS shield convenient to interact with cars, it has an MCP2515 CAN-BUS controller with SPI interface and MCP2551 CAN transceiver to give you Arduino/Seeeduino CAN-BUS capability. \
  • Features of this CAN BUS shield includes:
    • Implements CAN V2.0B at up to 1 Mb/s  
    • Industrial standard 9 pin sub-D connector
    • OBD-II and CAN standard pinout selectable.
    • Changeable chip select pin
    • Changeable CS pin for TF card slot
    • Changeable INT pin
    • Screw terminal that easily to connect CAN_H and CAN_L
    • Arduino Uno pin headers 
    • 2 Grove connectors (I2C and UART)
    • SPI Interface up to 10 MHz    
    • Standard (11 bit) and extended (29 bit) data and remote frames
    • Two receive buffers with prioritized message storage

CANBed – Arduino CAN-BUS Development Kit (Atmega32U4 with MCP2515 and MCP2551) ($14.90)

  • CANBed – Arduino CAN-BUS Development Kit carries an Atmega32U4 chip and MCP2515, MCP2551 CAN-BUS controller and transceiver to realize the CAN-BUS communication protocol on a single board without other MCU to control, it is a CAN-BUS Development Board itself!
  • Compared to the above CAN BUS shield, this CAN BED carries an Arduino chip – Atmega32U4 which means it is a combination of the CAN-BUS Shield and Arduino Development Board together on one single board.
  • Because of the Atmega32U4 onboard chip, this board has rich resources in pins. As a matter of fact, there are 18 pins based on core chip set up on the board, which include digital pins, analog pins, UART, and I2C interface.
  • Furthermore, this CANBed adopts MCP2515 CAN Bus controller with SPI interface and MCP2551 to achieve the CAN-Bus capability. There are also two kinds of CAN Bus interface for various demands which are sub-D9 connector and terminal block interface. They would fit all your needs in the connecting method.
  • In addition, This CAN-Bus development board is perfectly compatible with Arduino IDE. With the help of the Arduino CAN-Bus library, you will save plenty of time for your CAN project. 
  • Features of the CANBED includes:
    • Atmega32U4 with Arduino Leonardo bootloader on the board
    • MCP2515 CAN Bus controller and MCP2551 CAN Bus transceiver
    • BD-II and CAN standard pinout selectable at sub-D connector

Serial CAN-BUS Module based on MCP2551 and MCP2515 ($14.90)

  • If your project is space limited, here is a Serial CAN-BUS module which has the full features of CAN Bus. 
  • The Serial CAN-BUS provides your Arduino or other MCU with the capability to communication to CAN Bus, such as hacking your vehicle. 
  • This Grove CAN-BUS module is controlled by UART, which means if your MCU has a UART interface, this serial CAN BUS is available. 
  • Furthermore, this Serial CAN BUS module only costs $14.90
  • Features of this Serial CAN BUS module includes:
    • UART to CAN-BUS communication
    • AT command
    • Up to 115200 UART baud rate (default 9600)
    • Up to 1Mb/s CAN-BUS baud rate
    • TX and RX led indicator
    • 4pin Grove connector
    • 3.3V working voltage
    • Easy-to-use Arduino library
    • Small size

OBD-II CAN Bus GPS Development Kit ($29.90)

  • If you want to record the GPS log at the same time, this OBD-II CAN Bus GPS Development Kit is highly recommended! The board can be plugged directly into the car via the ODB-II interface and supports Arduino programming.
  • The baseboard of the development kit is integrated with an Atmega32U4 microprocessor. 
  • The CAN-Bus library is available to write sketches using Arduino IDE to send and receive messages from the CAN bus network and also allows you to fetch useful data from the messages.
  • The output data can be taken through the USB Type-C port or you can store them to a microSD card (TF card) by inserting to the microSD slot.   
  • The mainboard in the kit is based on MCP2551 CAN transceiver and MCP2515 CAN receiver, which provides baud rate from 5kb/s to 1Mb/s.   
  • A NEO-6 GPS breakout is seated on the base board allows you to track your vehicle with this fantastic small module by logging the GPS data to a microSD card. 
  • This is an example of the results in Google Earth!
Picture of View the Result

Without further ado, let us jump right in on how to get started with CAN BUS with Arduino!

Getting started with CAN BUS with Arduino

What do you need?

Instructions

Step 1: Hardware Connection

  • Firstly, connect each CAN-BUS shield into Seeeduino V4.2 and connect the 2 CAN-BUS shield together via 2 jumper wires as shown below. (CAN_H to CAN_H and CAN_L to CAN_L)

Step 2: Setting up Software

  • Download the library for CAN BUS here and install the library to Arduino IDE after download has completed.
  • If you do not know how to install an Arduino library, you can follow the instructions here.
  • One of the node (a node means Seeeduino + CAN_BUS Shield) acts as master, the other acts as slaver. The master will send data to slaver constantly.
  • Open the send example (File > Examples > CAN_BUS_Shield-master >send) and upload to the master as shown below:
  • After that, Open the receive_check example (File > Examples > CAN_BUS_Shield-master > receive_check) and upload to the slaver as shown below

Step 3: View Results

  • Open the Serial Monitor of Arduino IDE(slaver), you will get the data sent from the master. Below is a photo of what it will look like:

Step 4: APIs (Application Program Interface)

For the APIs of this software, you can:

Set the Baud Rate
  • This function is used to initialize the baud rate of the CAN Bus system. The available baud rates are listed as follows:
#define CAN_5KBPS    1
#define CAN_10KBPS   2
#define CAN_20KBPS   3
#define CAN_25KBPS   4
#define CAN_31K25BPS 5
#define CAN_33KBPS   6
#define CAN_40KBPS   7
#define CAN_50KBPS   8
#define CAN_80KBPS   9
#define CAN_83K3BPS  10
#define CAN_95KBPS   11
#define CAN_100KBPS  12
#define CAN_125KBPS  13
#define CAN_200KBPS  14
#define CAN_250KBPS  15
#define CAN_500KBPS  16
#define CAN_666kbps  17
#define CAN_1000KBPS 18
Set Receive Mask and Filter
  • There are 2 receive mask registers and 5 filter registers on the controller chip that guarantee you getting data from the target device. They are useful especially in a large network consisting of numerous nodes.
  • We provide two functions for you to utilize these mask and filter registers. They are:

Mask

init_Mask(unsigned char num, unsigned char ext, unsigned char ulData);

Filter

init_Filt(unsigned char num, unsigned char ext, unsigned char ulData);
  • num represents which register to use. You can fill 0 or 1 for mask and 0 to 5 for filter.
  • ext represents the status of the frame. 0 means it’s a mask or filter for a standard frame. 1 means it’s for a extended frame.
  • ulData represents the content of the mask of filter.
Check Receive
  • The MCP2515 can operate in either a polled mode, where the software checks for a received frame, or using additional pins to signal that a frame has been received or transmit completed.
  • Use the following function to poll for received frames:
INT8U MCP_CAN::checkReceive(void);

The function will return 1 if a frame arrives, and 0 if nothing arrives.

Get CAN ID
  • When some data arrive, you can use the following function to get the CAN ID of the “send” node.
INT32U MCP_CAN::getCanId(void)
Send Data
CAN.sendMsgBuf(INT8U id, INT8U ext, INT8U len, data_buf);

It is a function to send data onto the bus. In which:

  • id represents where the data comes from.
  • ext represents the status of the frame. ‘0’ means standard frame. ‘1’ means extended frame.
  • len represents the length of this frame.
  • data_buf is the content of this message.

For example, In the ‘send’ example, we have:

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
CAN.sendMsgBuf(0x00, 0, 8, stmp); //send out the message 'stmp' to the bus and tell other devices this is a standard frame from 0x00.
Receive Data
  • The following function is used to receive data on the ‘receive’ node:
CAN.readMsgBuf(unsigned char len, unsigned char buf);

In conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters.

  • len represents the data length.
  • buf is where you store the data.
Generate a New BaudRate

We had provided many frequently-used baud rates, as shown below:

#define CAN_5KBPS    1
#define CAN_10KBPS   2
#define CAN_20KBPS   3
#define CAN_25KBPS   4
#define CAN_31KBPS   5
#define CAN_33KBPS   6
#define CAN_40KBPS   7
#define CAN_50KBPS   8
#define CAN_80KBPS   9
#define CAN_83KBPS   10
#define CAN_95KBPS   11
#define CAN_100KBPS  12
#define CAN_125KBPS  13
#define CAN_200KBPS  14
#define CAN_250KBPS  15
#define CAN_500KBPS  16
#define CAN_666KBPS  17
#define CAN_1000KBPS 18

Yet you may still can’t find the rate you want. Here we provide software to help you to calculate the baud rate you need.

Click here to download the software, it’s in Chinese, but it’s easy to use. Here is the interface translated for easier usage:

  • Open the software, what you need to do is to set the baud rate you want, and then do some simple setting, then click calculate.
  • Then you will get some data, cfg1, cfg2 and cfg3.
  • You need to add some code to the library.
  • Open mcp_can_dfs.h, you need to add below code at about line 272
#define MCP_16MHz_xxxkBPS_CFG1 (cfg1)    // xxx is the baud rate you need
#define MCP_16MHz_xxxkBPS_CFG2 (cfg2)
#define MCP_16MHz_xxxkBPS_CFG3 (cfg2)
  • Then let’s go to about line 390, add below code:
#define CAN_xxxKBPS NUM       // xxx is the baudrate you need, and NUM is a number, you need to get a different from the other rates.
  • Open mcp_can.cpp, goto the function mcp2515_configRate(at about line 190), then add below code:
case (CAN_xxxKBPS):
    cfg1 = MCP_16MHz_xxxkBPS_CFG1;
    cfg2 = MCP_16MHz_xxxkBPS_CFG2;
    cfg3 = MCP_16MHz_xxxkBPS_CFG3;
    break;

That’s all! Now, you can use the baud rate you need!

Now that you’ve learnt how to interface CAN BUS with the Arduino using the CAN BUS shield, here are some CAN BUS projects to get you started!

CAN BUS Project Ideas

Volkswagen CAN BUS Gaming

Inspired by Silas Parker

Ever wanted to play a car/truck simulator with a real dashboard on your PC? With the CAN BUS shield, you can! In this project, I’m trying to control a VW Polo 6R dashboard via CAN Bus with an Arduino Uno and a Seeed CAN Bus Shield.

What do you need?

Interested? You can check out the full tutorial at SeeedStudio Community!

Hacking your Vehicle

Modern Vehicles all come equipped with a CAN-BUS Controller Area Network, Instead of having a million wires running back and forth from various devices in your car to the battery, its making use of a more clever system. From each node (IE Switch pod that controls your windows or electric door locks) it broadcasts a message across the CAN. When the TIPM detects a valid message it will react accordingly like, lock the doors, switch on lights and so on. With the CAN BUS shield, you can now hack your vehicle!

What do you need?

Interested? You can find the full tutorial by mvilijoen2 on Instructables!

Summary

With the basic knowledge of CAN BUS protocol in your hands, one way that you can use it is to analyze the data/messages passing through your vehicle and also hack your vehicle!

If you have any questions regarding CAN BUS, feel free to drop a comment down in the comment section down below!

Interested in more CAN BUS products? You can check out all of our CAN BUS products here!

Please follow and like us:
Wordpress Social Share Plugin powered by Ultimatelysocial