Skip to content

CAN-Bus with Raspberry Pi: HowTo/Quickstart MCP2515 Kernel 4.4.x+

In this HowTo I will show you how to connect a MCP2515 CAN-Bus module to your Pi and get it running.
I created this HowTo, because almost all other HowTos were outdated and not working with newer kernels and I want to help others with this information.
I am using a recent (on the date of this post) Raspbian image on a Raspberry Pi 2.

First of all run the following command on your Raspbian to check which overlay will work for your kernel:

pi@raspberry:~$ uname -a
Linux raspberrypi 4.4.41-v7+ #942 SMP Mon Jan 9 15:00:25 GMT 2017 armv7l GNU/Linux

It’s necessary to modify the CAN-module from ebay, because it has only one VCC pin, but the MCP2515 needs to be powered from 3V3 and the TJA1050 CAN-transceiver needs to be powered from 5V. Powering both chips from 5V would work, but then a level-shifter for the SPI would be needed. The Pis GPIO pins are NOT 5V tolerant.

In my case I cut a trace on the PCB and soldered a pin onto the trace to deliver 5V only to the TJA1050. Be sure to cut the trace before the capacitor (otherwise if there is no capacitor, add one (about 100nF maybe)).

Here is a closeup (pin 3 is VCC on the TJA1050):

Next connect the module:

MCP2515 Raspberry Pi
VCC 1 (3V3)
TJA 1050 VCC 2 (5V)
GND 6 (GND)
CS 24 (CE0)
MISO 21 (MISO)
MOSI 19 (MOSI)
SCK 23 (SCK)
INT 22 (GPIO25)

First update software and install can-utils:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install can-utils

To activate the driver for the MCP2515 you have to add a kernel overlay, to do so edit the /boot/config.txt

sudo nano /boot/config.txt

And add the following lines (for kernel 4.4.x+) (then exit with Ctrl+X):

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi1-1cs

Or for older kernels:

dtparam=spi=on
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

In my case the MCP2515 CAN module has a 8MHz quartz, so the oscillator value has to be two times the frequency = 16000000. (This is one of the caveats I ran into, this was the only way I got my module working).

Now reboot, after the reboot try to setup the the can interface:

sudo ip link set can0 up type can bitrate 500000

If no errors occurred, the can interface should be ready now.
Try to send some messages:

cansend can0 127#DEADBEEF

You can also sniff the CAN-bus using candump:

pi@raspberrypi:~$ candump can0
  can0  120   [8]  01 C0 FF EE 00 00 00 00

To make the CAN-interface permanent, add the following lines to /etc/network/interfaces

auto can0
iface can0 can static
	bitrate 500000

If you find any errors please let me know in the comments.

Published inAllgemeinCANCAN-BusElektronikMikrocontroller

9 Comments

  1. Leonardo José Leonardo José

    Thanks very much for the detailed and up to date guide

  2. liangzhenjie liangzhenjie

    I have done all steps following this article,and no error occcur,when i write data using cansend ,everything is well,but i can receive noting on my can board, i also try modify the config.txt with :dtoverlay=spi0-hw-cs ,which i saw it from another article, not work as well.
    i’m working on raspberry pi 3 with linux raspberrypi 4.9.59-v7,help me please!

    • I have no experience with that kernel. Be sure that you are running the can transceiver not out of spec.

  3. liangzhenjie liangzhenjie

    when i turn loopback on ,i can receiver message using candump can0,but when i turn loopback off, i can not receive or send message any more,could you give me some sugesstions?

  4. Nicholas Nicholas

    Please check, if you are right about the crystal freq. In my setup I need to specify the same value read on the crystal.

    • Yes, it seems confusing. It may be a bug or something. But setting the same value on the crystal didn’t work.

  5. Salih Salih

    I do not understand what you’re doing on hardware. Do you give me some more deed information?

    • There is only one VCC pin for both ICs: CAN controller and CAN transceiver.
      The CAN controller can operate between 2.7 to 5.5V,
      but the CAN transceiver only between 4.75V to 5.25V.
      If you use the module with a Pi the VCC for the CAN controller has to be connected to 3V3 so the SPI interface is on the same level.
      But 3V3 is out of spec of the CAN transceiver, this is why I cut the trace and connected the VCC of the transceiver separate.

Leave a Reply

Your email address will not be published. Required fields are marked *