Update : Latest Raspbian supports CAN Controller MCP251x, you don’t have to add modules like described here.
Please read the new post on how to use CAN Bus with Raspberry Pi.
After several hours trying to find a complete solution to add CAN bus support to my Raspberry Pi, I’m sharing with you what worked for me, by combining different sources and information that I get on the web.
This method uses MCP251x Microchip’s stand-alone controller, connected to SPI bus of the Raspberry Pi, but it should work also for any other CAN controller.
If you meet any problem, please let me know it by comment below or email.
This method is tested with latest Raspbian (3.18.7+), I recommend you to have the same version before proceeding.
I’ll skip Raspbian first installation, if you are not familiar with it, you can use NOOBS setup (version 1.4.0) and follow instructions.
For next steps, you will need :
- Raspberry Pi (I’m using Model B) + SD Card Raspbian.
- Screen/Keyboard/Mouse/Internet Access
After installing Raspbian, start by updating/upgrading, and rebooting your Raspberry Pi.
sudo apt-get update sudo apt-get upgrade sudo apt-get autoremove sudo reboot
Then download CAN bus modules for latest Raspbian (Thanks to Ifinterface.com!) http://ifinterface.com/page/page3.php?langid=1
If you are using older Raspbian (e.g. 3.12) , try to find compatible CAN bus sources here : http://lnxpps.de/rpie/ or here : http://ifinterface.com/page/page3.php?langid=1
Untar in root level (to avoid any permission error), or in /tmp if you want to remove it after rebooting :
cd / sudo tar -xjf ~/rpi-can-3.18.7+.tar.bz2
Register new modules, and reboot :
sudo depmod -a sudo reboot
There is two ways to configure and load your CAN modules :
1 – Doing it directly by command-line :
Start by configuring SPI (copy it as it is, with no spaces) :
sudo modprobe spi-config devices=bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_release
Explanation : 10MHz SPI clock GPIO 25 for MCP2515 /INT and 16Mhz clock (crystal) – 0x2002 -> IRQF_ONESHOT
Then, load MCP module :
sudo modprobe mcp251x
2 – Doing it in autoload file :
Open your modules config :
sudo nano /etc/modules
Add the following lines :
spi_bcm2835
spi-config devices=\
bus=0:cs=0:modalias=mcp2515:speed=10000000:gpioirq=25:pd=20:pds32-0=16000000:pdu32-4=0x2002:force_releasemcp251x
Press CTRL+O to save changes, then press ENTER to confirm file name. Press CTRL+X to quit nano.
Don’t forget to reboot to load your new modules :
sudo reboot
Normally, I say, normally, you are now able to set up a CAN bus link with bitrate value, for example :
ip link set can0 up type can bitrate 250000
If it is not working, here main mistakes I did, and eventually you are doing :
1- SPI module is not installed/enabled
Firstly, check if your SPI is well installed in Raspbian (There is no reason that it isn’t, but check it anyway)
Check your SPI device and module availability with these commands :
ls /sys/bus/spi/devices/ ls /sys/module/spidev/
You need to have (spi0.0 spi0.1), and see also (parameters uevents)
The easy way to enable SPI module it by using Raspi-config interface :
sudo raspi-config
Go to Advanced Options and enable SPI kernel module, and reboot.
Another method is by enabling SPI in Raspi-blacklist, open it :
sudo nano /etc/modprobe.d/raspi-blacklist.conf
Then withelist SPI and MCP with # comment :
#blacklist spi-bcm2835
#blacklist mcp251x
CTRL+O, ENTER, CTRL+X.
2- SPI module reference is different from above commands
Shame on me, but some times I used spi-bcm2708 instead of spi-bcm2835…
Take a look to your SPI reference in modules list (search for spi_bcmXXXX) :
ls /sys/module/
3- CAN bus is not connected
You need to connect your Raspberry Pi to physical CAN bus in order to set up can0 interface, otherwise you can’t see it.
Alternative solution is to use virtual CAN bus, it means that you will have internal CAN node in your Raspberry Pi, very helpful for desktop testing.
sudo modprobe vcan sudo ip link add dev vcan0 type vcan sudo ip link set up vcan0
You can now see vcan0 when you display your ifconfig :
ifconfig
Send data (for example send byte “10” to device with identifier 100) :
cansend vcan0 100#01
Helpful Links :
CAN + Raspberry Pi
Raspberry Pi SPI Documentation
http://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md
Raspberry Pi BCM2835 Documentation
http://www.airspayce.com/mikem/bcm2835/
Linux CAN-Utils Documentation
https://github.com/linux-can/can-utils/
HI Tiqni,
I have tried to install on my raspberry pi 2 without success. What is wrong? I think, I have another kernel version as you.
uname -r
3.18.7-v7+
If I have tried to install
sudo insmod /lib/modules/3.18.7-v7+/extra/mcp2515a.ko
and I get this error
Invalid module format
Have you got any idea, what is wrong?
Regards, Frank
Hi Frank,
Did you try to install modules like explained above ? You will find all what you need like :
/lib/modules/3.18.7+/kernel/drivers/net/can/spi/mcp251x.ko
/lib/modules/3.18.7+/extra/mcp2515.ko
Good luck.
Regards
The IFI download site (http://ifinterface.com/page/page3.php?langid=1) for the modules states that the modules are for the ARMV6. The Raspberry Pi 2 is not ARMV6, so the modules will not work. You may be able to get the modules from github and recompile the kernel.
Thanks Gary for clarification.
The virtual can works, but I cannot get my can-bus shield (Seeed studio CAN-BUS shield) to work. I cannot get a can0 device. Can you explain how to connect a can controller? Are there any other steps to get a can0 device?
I have OBDII Bluetooh and Raspberry Pi with Kodi XBMC. I dont know if I can use canbus to control xbmc?
Hi Ngochoangimsat,
I’m not sure that Kodi XBMC supports GPIO controls except IR receiver (http://kodi.wiki/view/Raspberry_Pi#Remote_controls)
I was able to get socketcan to work on a Raspberry Pi 2 Model B. The link to the instructions:
http://skpang.co.uk/blog/archives/1165
I have followed these steps and at the end of i got an answer –> cannot find device “can0”
Where is my mistake?
Hi Gul,
Most of time this is a hardware issue, check again your schematics and connections.
Now, I can configure module. I’m sending “sudo candump can0” command but I receive nothing. Maybe this is easy peasy question but i attached this section.
Can you show me your hardware schematic ?
I can suggest some issues :
Terminator resistor missed.
Low voltage if power source is Raspberry Pi.
CAN controller not enabled.
Hi Tiqni,
I’m using PICAN CAN-Bus board(sk pang electronics) . Board is directly wired to Raspberry pi. Also I’m using peak-system pican usb for send canbus data to my raspi. CAN-H and CAN-L pin of board are wired with pican usb.
What is your power source for Raspberry Pi ?
Did you try to turn on JP3 of PiCAN to enable 120Ohms terminator ?
Did you check that you use the same CAN speed in Raspberry Pi and CAN USB ?
Can you try with a real CAN Bus (e.g. Car OBDII) ?
– Adaptor turns main voltage to 10.5VA this is my power source i guess.
– I did not try to turn on/off JP3 of PiCAN to enable 120Ohms terminator.
– I checked and these are same.
– Actually i did not try real CAN Bus
I check for your CAN USB, there is no termination resistor, so you need to enable JP3 (see page 13)
http://www.peak-system.com/produktcd/Pdf/English/PCAN-USB_UserMan_eng.pdf
This may be the solution.
Hi Tiqni,
I solved my problem. My mistake is wrong vehicle target bitrate. I change it to 50kbps and everything goes right. Thank you for your all help!