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:
|TJA 1050 VCC||2 (5V)|
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  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.