Introduction to CAN-BUS and How to use it with Arduino[2024]

Updated on the 03/01/2024

Ever heard of CAN-BUS but don’t exactly know what it does? Fret not! We will be discussing everything about CAN-BUS today. I hope that you’ll get a better understanding of it through this blog:

  • Introduction to CAN-BUS
  • Why use the CAN protocol
  • CAN-BUS vs OBD2
  • Seeed’s CAN-BUS products (Updated)
  • CAN-BUS Projects (Updated)
  • Getting started with CAN-BUS with Arduino

Introduction to CAN-BUS

What is CAN-BUS?

CAN stands for Controller Area Network, it is used to allow microcontrollers and devices to communicate with each other within a vehicle without a host computer which allows for control and data acquisition. These devices are also called Electronic Control Units (ECU) and they enable communication between all parts of a vehicle.

Today, you can find up to 70 ECUs in a modern car, e.g. the engine control unit, airbags, audio system, etc. CAN is a serial communication bus designed for industrial and automotive applications. For example, they are found in vehicles, farming equipment, industrial environments, etc.

How does CAN-BUS work?

The fuel level, door sensors, odometer, and many more parts of a car have to communicate with each other somehow, and CAN BUS is what they used to do. These CAN-compatible components, which are called “nodes” are connected with a 3-string copper wire, with no central router to govern the flow of data. Every node can hear the messages of every other node.

Every node has an ID, where the ones with the higher priority ID can have the priority to “talk” first while the others “listen”. This is to ensure that there are never two nodes talking at the same time. The biggest benefit of CAN-BUS is to be able to just connect components without having to worry about signal routing.

Why use the CAN protocol rather than UART, SPI, and I2C?

Compared to other communication protocols like UART, SPI, and I2C, using the CAN protocol is 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.

In a vehicle, safety and reliability are the two most valued features. CAN-BUS is therefore the ideal protocol for vehicular usage.

The CAN bus standard is used in practically all vehicles and many machines due to below key benefits:

Low Cost

  • With its multiplex wiring that combines analog and digital signals and their transmission over a shared medium, it reduces the amount of wiring needed, errors, and weight.
  • When adding or reducing some equipment, it can be easily operated, and there is no need to carry out a large-scale transformation of the system, which saves a lot of manpower and costs.

Centralized

  • As CAN-BUS supports centralized control over electronic devices that are connected to the network, it enables central diagnostics, data logging, and configuration.
  • Error handling is also built into the CAN protocol where nodes can check for errors in transmission while maintaining their 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 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 removed 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-ideal for safety-critical applications (e.g. vehicles).

Efficient

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

The role of CAN

What will happen if there is no CAN in the car?

Ref: Volkspage

Without the CAN protocol, electronic modules in vehicles will have to communicate with each other using direct, point-to-point analog signal lines. It is not only time-consuming but also a messy excessive amount of wiring as seen in the picture above if each module requires a direct line connected for communication. Besides, there may be unreliable communication between devices. Excessive wires may require additional equipment, which also creates cost issues.

What will happen if there is a CAN in the car?

Ref: Volkspage

With CAN protocol, it eliminates the need for all these wirings 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 in 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.


CAN-BUS Wiring Sequence

  • The CAN 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
  • When the CAN bus is in idle mode, both lines carry 2.5V. When data bits are being transmitted, the CAN high line goes to 3.75V, and the CAN low drops to 1.25V, thereby generating a 2.5V differential between the lines.
  • 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 Protocol Speed and Range

  • Communication speeds of the CAN protocol range 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 protocol works, let us look at the frames sent over the network.
  • The CAN message contains many segments. The 2 main segments, identifier, and data will be the ones transmitting the data.
  • 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.

CAN-BUS vs OBD2

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

  • The CAN standard 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’s 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 the 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.

Explore Seeed’s CAN-BUS products!

Here at Seeed, we offer a variety of CAN-BUS products for your project needs! As shown below:

New Release! Seeed Studio CAN Bus Breakout Board for XIAO and QT Py ($9.95)

  • Seamless XIAO Compatibility: Designed to work seamlessly with the Seeed Studio Xiao development board.
  • Efficient Communication: The onboard MCP2515 chip provides reliable control and handling of the CAN Bus communication. The integrated SN65HVD230 chip ensures accurate signal conversion and robust communication over the CAN Bus.
  • Terminal Connection: The CANH and CANL lines are conveniently accessible through a 3-pin terminal, allowing easy connection to the CAN Bus.
  • Compact Design: The expansion board has been designed with a compact form factor, making it suitable for various applications.

Wio ESP32 CAN Dev Kit($29.90)

  • Outstanding RF performance:  Powerful ESP32-C3 SoC and U.FL antenna that supports WiFi/Bluetooth connection over 100m. It also supports a Global Long Range frequency plan with a long-distance transmission range of 10km(ideal value in open areas)
  • 2 Independent CAN FD Interface: Supports CAN FD, CAN2.0 communication
  • High extendability with various sensors: full GPIOs led out to rich interfaces, including Grove modules for GPS, environmental detection, etc
  • Support industrial standards: a wide working temperature at -40 ℃ ~ 85℃, high sensitivity between -116.5 dBm ~ -136 dBm, and power output up to +20.8dBm at 3.3V

Grove – CAN BUS Module based on GD32E103($4.90)

This Grove – CAN BUS Module based on GD32E103 adopts a brand-new design, uses the cost-effective and high-performance GD32E103 microcontroller as the main control and cooperates with a firmware we wrote to complete the function of the serial port to CAN FD.

  • Support CAN communication: Implements CAN FD at up to 5Mb/s
  • Easy to program: Support AT command which enables simple serial port programming
  • Grove ecosystem: 20mm x40mm x10mm small size, 4pin Grove connector to plug and play,  Arduino compatible

CANBed DUAL($14.90)

  • High Performance: Powered by RP2040 with 264KB of SRAM, and 2MB of onboard Flash memory
  • Support two CAN protocols: CAN-FD and CAN 2.0
  • Robust CAN controller: GD32E103CBT6, dual-core ARM Cortex M0+ processor, flexible clock running up to 133 MHz
  • Multiple interfaces: One I2C, One UART, One 2×3 Pin SPI Interface

Wio-E5 CAN Development Kit($39.90)

  • Support various communication methods: CAN FD, CAN 2.0, and RS485
  • High extendability with various sensors: full GPIOs led out to rich interfaces, including RS-485, Grove modules for GPS, environmental detection, etc
  • High compatibility: support Global Long Range frequency plan with long-distance transmission range to 10km(ideal value in open area)
  • Easy to use: User-friendly waterproof case & solar charging interface provided for better outdoor deployment and enable temporary use when lacking an external power source
  • Support industrial standards: a wide working temperature at -40 ℃ ~ 85℃, high sensitivity between -116.5 dBm ~ -136 dBm, and power output up to +20.8dBm at 3.3V

CAN-BUS Shield V2 ($24.50)

  • High-performance ICs: stand-alone MCP2515 CAN-BUS controller and high-speed MCP2551 CAN transceiver
  • Supports industrial standards: provide 9-pin sub-D connector
  • Medium communication speed: implements CAN V2.0B at up to 1 Mb/s
  • Long travel distance: 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) ($24.90)

  • Enhanced MCU Performance: ATmega32U4 with Arduino Leonardo bootloader on the board
  • Compatible with Arduino: Combines CAN-BUS shield and Arduino development board together on a single board
  • High Speed: Implements CAN V2.0B at up to 1 Mb/s
  • Rich Resources in Pins: 18 pins that include digital pins, analog pins, UART, and I2C interface
  • Easy to Use: Requires no other MCU to control and is compatible with Arduino IDE

CANBed FD – Arduino CAN-FD Development Kit ($17.90)

  • Compact size design: 56x41mm overall dimension
  • Support two CAN protocols: CAN-FD and CAN 2.0
  • High-performance CAN Bus chip: MCP2517FD as CAN controller and MCP2542 (MCP2557) as CAN receiver
  • Flexible CAN Interface: Industrial standard 9-pin sub-D connector or 4-pin terminal, 2 x 4-Pin Grove connectors Compatible with the Grove ecosystem
  • Wide power input range from 7-28V and stable 5V/1A output

CANBed M4 Development Board with ATSAME51G19A MCU and MCP2542FD CAN Transceiver($19.90)

  • Supports two CAN protocols: CAN-FD and CAN2.0
  • Powerful MCU: ATSAME51G19A 32bit Cortex M4 core with clock speed up to 120MHz
  • Industrial standard 9-pin sub-D connector or a 4-pin terminal connector
  • OBD-II and CAN standard pinout selectable at the sub-D connector
  • 2×4-pin connector compatible with Grove ecosystem

CANBed – Arduino CAN-Bus RP2040 development board($15.90)

  • Powerful MCU: Powered by RP2040 with 264KB of SRAM, and 2MB of onboard Flash memory
  • High-Performance: MCP2515 CAN controller and MCP2551 CAN receiver operating at CAN2.0 protocols in 133MHz. Implements CAN V2.0B at up to 1 Mb/s
  • Industrial standard 9 pin sub-D connector or 4PIN Terminal.
  • OBD-II and CAN standard pinout selectable at the sub-D connector
  • 2x4Pin Connector compatible with Grove system from Seeedstudio

CAN-BUS Projects

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 you’ll 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, it’s 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, locking the doors, switching 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!

Seeed Studio CAN-BUS V2.0 Hacking – Getting Started

This instructable is meant for beginners who already know their way around Arduino. In this instructable, you will learn how to return some CAN-BUS data from your vehicle.

What you’ll need:

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

CAN Bus Sniffing – Hacking With Arduino!

This video is going to connect to the Renault Megane 3 vehicle with the Arduino Uno + SeeedStudio CAN Bus Shield V2 and analyze the data/messages passing through the vehicle’s CAN bus (sniffing). If you use CAN Bus Protocol in your vehicles, you can listen to your vehicle with the same actions.

What you’ll need:

Interested? You can find the full tutorial by Bugra on Youtube!

Vehicle GUI Can Bus Display

This project utilizes Can Bus to read sensor values and communicate over serial to the display featuring customizable GUI.

What you’ll need:

Interested? You can find the full tutorial by Kenliao on Hackster!


Getting started with CAN-BUS with Arduino

Getting Started with CAN-BUS can be very fulfilling. It can be daunting as well, but this guide will provide you with a step-by-step guide to getting started!

What do you need?

Instructions

Step 1: Hardware Connection

  • Firstly, connect each CAN-BUS shield to Seeeduino V4.2 and connect the 2 CAN-BUS shields 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 the download has been completed.
  • If you do not know how to install an Arduino library, you can follow the instructions here.
  • One of the nodes (a node means Seeeduino + CAN_BUS Shield) acts as a master and the other acts as a slaver. The master will send data to the slaver constantly.
  • Open the sent example (File > Examples > CAN_BUS_Shield-master >send) and upload it to the master as shown below:
  • After that, Open the receive_check example (File > Examples > CAN_BUS_Shield-master > receive_check) and upload it 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 guarantees you get 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 the mask and 0 to 5 for the 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 an extended frame.
  • ulData represents the content of the mask of the filter.
Check Receive
  • The MCP2515 can operate in either a polled mode, where the software checks for a received frame, or uses 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 an 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 where 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 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, then do some simple settings, 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 the 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 the 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 learned how to interface CAN-BUS with the Arduino using the CAN-BUS shield, here are some CAN-BUS projects to get you started!


Summary

With the basic knowledge of CAN 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!

I hope you learn more about the CAN protocol in this guide! If you have any questions regarding CAN BUS, feel free to drop a comment in the comment section down below.

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

About Author

Calendar

November 2019
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930