It is possible to work with the CAN protocol used in the automotive industry on Raspberry Pi using a CAN HAT. This allows yorto plug in and decode messages from the on-board computer, retrieving information such as fuel consumption, speed or engine rpm.

Installing libraries

To use CAN Hat with Raspberry Pi, only one python package is needed can-utils

sudo apt-get install can-utils

This package handles the CAN communication protocol commonly used in the automotive industry.

  • read and decode CAN messages
  • save messages in a CANlog file
  • replay the CANlog file on a real or virtual CAN bus to simulate a device

N.B.: you can also install pandas and matplotlib to process and trace the data received.

CAN bus configuration

sudo ip link set can0 up type can bitrate 50000
sudo ip link set can0 down

For a virtual can bus, yorcan enter the following commands

sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

To check that the can bus is in place, enter the command

ifconfig can


ip addr | grep can

Sending and receiving CAN messages

Enter the transmission command in a terminal

cansend can0 001#1122334455667788

Then, in another terminal, enter the receive command

candump -tz can0

the -tz flag is optional and allows the time to be displayed

(1668600101.854788) can0 0B2#0000000000000000
(1668600101.855011) can0 116#04045CAE092596FF
(1668600101.855285) can0 11E#6B04000000000000
(1668600101.855949) can0 107#0000FE46411404E7
(1668600101.856174) can0 0A7#CDD35F7FFDF5173F
(1668600101.856408) can0 0A8#C1D35FC98C643C0F
(1668600101.856652) can0 32C#0003000098240810
(1668600101.856904) can0 557#0000000000000000
(1668600101.857743) can0 086#1703C82100000000
(1668600101.858067) can0 23D#0000F8FFFFFF3F00
(1668600101.859665) can0 3D0#000000A380008000
(1668600101.859913) can0 33C#0000012080000000
(1668600101.860199) can0 16A954C2#006B000000000000
(1668600101.863567) can0 464#0000000000000000
(1668600101.863814) can0 3DE#FE00000000000000
(1668600101.864582) can0 0B2#0000000000000000
(1668600101.864826) can0 0FD#E5E53F8000000000
(1668600101.865070) can0 101#B4057E0582030000
(1668600101.865308) can0 106#B985B3040000AF20
(1668600101.865631) can0 120#860200020008FF01
(1668600101.865866) can0 121#4602004039627EFE
(1668600101.866109) can0 15A#760E000001E60000
(1668600101.866339) can0 0A8#71D45FC98C643E0F
(1668600101.866574) can0 0A7#F7D45F7FFDF5173F

Recording and replaying a CANlog file

To save a log file of CAN messages, enter the command:

candump -l can0

this command creates a candump–.log file

The file can be replayed on the same can bus or on a different bus.

canplayer -I candump-2022-11-16_120141.log #play can on same canbus as in the file
canplayer vcan0=can1 -v -I candump-2022-11-16_120141.log #play saved canbus (can1) on vcan0
canplayer -l 2 -I candump-2022-11-16_120141.log #play can data twice
canplayer -l i -I candump-2022-11-16_120141.log #play can data inifinitely

Decode CAN bus messages

To decode messages on a CAN bus, it is necessary to have the definition of the messages on the CAN bus. This definition is described in a .dbc file, which is the CAN bus database. This document contains the message identifier and the names of the signals it contains.


Once you’ve mastered the protocol, you can use the CAN hat board with Raspberry Pi to develop embedded automotive devices.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?