35 Responses

  1. […] on how to connect Raspberry Pi to CAN Bus. http://youness.net/raspberry-pi/raspberry-pi-can-bus. […]

  2. Michael J. Kidd at | | Reply

    Great write up.

    While I’m sure many appreciate the details of the circuits involved, I’d bet others would appreciate knowing a Pi hat exists for this purpose.

    SKPang had the PiCAN2 board which I’ve used extensively for the past 3 years in various projects. He has single and dual bus board as well as a variety which includes a 12v to 5v SMPS that works great for vehicle projects.

    Note: I’m not affiliated with SKPang other that being a customer.

  3. Michael J. Kidd at | | Reply

    Youness, thanks! That’s what I get for not browsing your blog more… 😉

  4. Yahya EL Gaoual at | | Reply

    Thank you so much, such a great blog

  5. ERAT #36 A talking shield – ZL2KEJ at |

    […] How to Connect Raspberry Pi to CAN Bus […]

  6. Matheus Penido at | | Reply

    Thanks for that great tutorial! I did build the circuit and when plug it over a CAN BUS network based on two Arduinos and MCP2515/TJA1050 raspberry pi read data flow appropriately. But I have one question: Why loopback mode didnt works for me? There are some configurations or modifications on to circuit to get it working? Maybe someone can help me! Thanks…

  7. Martin at | | Reply

    Thanx for the great HowTo.

    Just a remark for a tiny mistake:
    The drawing in chapter 1.1.5 has a bug. The Node B must be twisted. The CAN Transceiver should be connected to the bus and being on the left side, the CAN controller should be on the right side.

  8. Donald Fast at | | Reply

    Hi,

    Should this same process work for the microchip CAN bus analyzer (USB) ?? I followed these steps and after “ip link set can0….” i get
    cannot find device “can0”

    Product details: https://www.microchip.com/Developmenttools/ProductDetails/APGDT002

    I have this device and it works when running under windows. I am trying to monitor some canbus industrial equipment (not a car, data not encrypted).

    Thanks for any help.

  9. Donald Fast at | | Reply

    Built and working.

    Small correction: the schematic in 1.4 is wrong (but the images are correct) Raspberry pin 20 is actually pin23 (Sclk)
    ( Pin 20 is ground. )

  10. Sneh at | | Reply

    I’m having some issues after following this guide.

    can0 interface shows as state UNKNOWN on both pi’s after booting.

    can0: mtu 16 qdisc pfifo_fast state UNKNOWN group default qlen 10 link/can.

    If I set both to down and bring them back up, one usually always comes UP, sometimes both come UP. At the moment, neither are coming up, just staying as UNKNOWN.

    dmesg | grep -i can and dmesg | grep -i spi on both pi’s show the following:

    mcp251x spi0.0 can0: MCP2515 successfully initialized.

    cansend and candump show nothing whatsoever.

    Wireshark shows the following on the pi I am sending from:

    1 0.000000000 CAN 32 STD: 0x00000111 ff
    2 0.000296441 CAN 32 ERR: 0x00000004 00 20 00 00 00 00 00 00
    3 0.001137634 CAN 32 ERR: 0x00000040 00 00 00 00 00 00 00 00

    If I do the same on the other pi, I get this:

    1 0.000000000 CAN 32 STD: 0x00000111 ff
    2 0.001802927 CAN 32 ERR: 0x00000004 00 10 00 00 00 00 00 00
    3 0.002334128 CAN 32 ERR: 0x00000004 00 20 00 00 00 00 00 00
    4 0.004644923 CAN 32 ERR: 0x00000040 00 00 00 00 00 00 00 00

    After trying wireshark, checking dmesg again, I end up with bus-off error:

    mcp251x spi0.0 can0: bus-off

    Any pointers on what to check next??

  11. sneh at | | Reply

    It looks like my issue was a bad connection somewhere.

    I bought some better quality breadboards with better gripping sockets, as I thought the first pair I was using let some of the leads flop around a bit too much for my liking.

    I rebuilt the pair of modules on the new breadboards and it all worked first go.

    Just goes to show, when something doesn’t work, start with the basics first.

    Thanks for the write up.

  12. ben at | | Reply

    Is there a way to read the MCP2515 registers? Not sure how much low level access linux’s socket interface provides! I’m interested in seeing what the values are, especially the timing configuration bits.

  13. Andy Ahn at | | Reply

    This has been great help.
    At first I got some errors but turned out the wiring problems.
    Thank you so much.

  14. Augusto Hernández at | | Reply

    Hey, nice job! But why 130 Ohms? Can BUS standards recommend resistance at ends of 120 Ohms…

    And the clock, datasheet recommends two capacitors of 22 pF for CERAMIC RESONATORS (Not for CRYSTAL OSCILATORS). Anyway they are insignificant details, your work is very well documented.

    Congratulations!

  15. Augusto Hernández at | | Reply

    Hi again!

    Do you have any idea to implement CAN BUS FD (MCP2517FD, MCP2558)?

    Thank you very much in advance!

  16. Hari krishna munagala at | | Reply

    Hi,
    Thanks alot.
    I connected mcp2515 to raspberry pi3,but dont know what libraries i need to include in program.can you please send any demo program to blink led or control fan.Please send asap…..

  17. Lordloki at | | Reply

    I’m looking to attach a pican2, and then create a service / constantly running script that read from it and writes output to the rpi as specific keyboard strokes that can be picked up by the rpi. Anyone have any direction they can point me to?

  18. reddy at | | Reply

    Hi team,

    can we load AUTOSAR Stack into the board using debugger?
    if it is Yes
    can some give help on that
    it will be helpfull for me

  19. wanek at | | Reply

    hello!

    nice article, thanks for the info!

    i think there is a bug in the schematic, with the voltage divider:
    the 22k should be placed between the 10k and the mcp2515.

    best regards,

  20. Jwdsoft at | | Reply

    Thanks youness for your great tutorial
    I’m trying to make the MCP2515 work like on this tutorial but it is not working and can’t figure out what is the problem, since the MCP2515 is successfully initialized so the wiring is correct and can0 is is up and everything is good but can’t see any traffic ( sending nor receving: i hooked an arduino to it which sends messages in a loop)

    pi@raspberrypi:~ $ dmesg | grep -i spi
    [ 6.301058] mcp251x spi0.0 can0: MCP2515 successfully initialized.
    pi@raspberrypi:~ $ sudo ip link set can0 up type can bitrate 500000
    pi@raspberrypi:~ $ candump can0
    ^Cpi@raspberrypi:~ $cansend can0 456#43414e2054657374

    I was suspecting the the MCP2515 is bad but I tried it on arduino and it works fine both receiving and sending ( two arduinos one sending and one receiving, the one sending is also the one I connected to RPI to send messages to it also tried receving but nothing works.)
    I want to use RPI so I can use Linux can-utils but unfortunetly this didn’t work for me

    Can you figure out what is the problem or how to debug this to find out what is the problem.
    Thanks

Please comment with your real name using good manners.

Leave a Reply