64 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

  21. jwdsoft at | | Reply

    Why does Arduino voltage matters ? Since the the mcp2551 of the raspberry pi is hooked to 5v so it has 5v voltage levels like the Arduino not 3.3v
    So i think both CAN nodes have 5v voltage levels, am I right ?

  22. Rainer Müller-Knoche at | | Reply

    Hi, Pin 8 of the MCP2551 ist not reset, see datasheet.
    “Pin 8 (Rs) allows three different modes of operation to be selected: High-speed, Slope control and Standby.
    For high-speed operation, the transmitter output transistors are simply switched on and off as fast as possible. In this mode, no measures are taken to limit the rise and fall slopes. A shielded cable is recommended to avoid RFI problems. High-speed mode is selected by connecting pin 8 to ground.
    Slope control mode allows the use of an unshielded twisted pair or a parallel pair of wires as bus lines. To reduce RFI, the rise and fall slopes should be limited. The rise and fall slopes can be programmed with a resistor connected from pin 8 to ground. The slope is proportional to the current output at pin 8.
    If a HIGH level is applied to pin 8, the circuit enters a low-current Standby mode. In this mode, the transmitter is switched off and the receiver is switched to a low current. If dominant bits are detected (differential bus voltage >0.9 V), RXD will be switched to a LOW level. The microcontroller should react to this condition by switching the transceiver back to normal operation (via pin 8). Because the receiver is slower in Standby mode, the first message will be lost at higher bit rates.”

    So connecting it to GND will not harm. Just for clarification.

  23. Fangming at | | Reply

    Hi,
    I used a Pi 3B+ and a CAN-BUS shield for Arduino. I used method described here. The only mistake that I might do was providing a 5V to mcp2515 in my first day connecting. If I typed : uname -a, it returned:
    Linux raspberry 4.19.75-v7+ #1270 SMP Tue sep 24 18:45:11 BST 2019 armv7 GNU/Linux
    I also revised config.txt as the author wrote here. Now I changed 5V to 3.3v. However, when I typed : dmesg | grep -i spi, it returned:
    mcp251x spi0.0: cannot initialize Mcp2515. Wrong wiring?
    mcp251x spi0.0: Prob failed, error=19.

    Then I typed ‘ls /dev/spidev0.*’ It returned :
    /dev/spidev0.1
    How can I check the spi work or not? Thanks.

  24. Fangming at | | Reply

    I used a board designed by myself which applied MCP2515 and NXP A1050. I provided 5V to them. Initialization is success, both of MCP2515 and can0. However, when I type ‘sudo ip link set can0 up type can bitrate 500000’, there is no ‘$’ in the next row. How to solve this new problem? Thank you.

  25. Arpitha N V at | | Reply

    How do i make this work with kernel 4.19?
    It gives me an error “cannot find device can0”

  26. Nigel at | | Reply

    Hi Youness

    I am using the SK Pang board on board my boat and am getting NMEA 2000 data using the software supplied by SKP. I now need to retrieve from this, for example, depth and process this using for preference Python. Can you point me towards the software required to do this? Your help would be appreciated.

  27. Daniel Martinez at | | Reply

    Question: why on photos you put capacitador on parallel connection ??

    it should not be in series connection like the diagrams shows?

    Thanks for the answer.

  28. Daniel Martinez at | | Reply

    First of all, thanks for the quick reply.

    Ok, to ensure that we understand each other… it is the connection like this? (http://www.electronoobs.com/images/Arduino/tut_43/atmega8_2.png).

    On the other hand, when carrying out the project, I realized that this is the breadboard model, right? (Protoboard model Full, no Full + http://wiki.sunfounder.cc/index.php?title=Breadboard_Basics_–_Types)

    Thanks for the answer 🙂

  29. Daniel Martinez at | | Reply

    again, thanks for the quick reply 🙂

    I am understanding everything little by little, thanks for the patience.

    I have other question, related to the voltage level. In the image attached to the link (https://ibb.co/WBrPpyc uploaded by me) I have only mounted the voltage divider to ask you if it would be well connected.

    Blue resistor: 22k
    White resistor: 10k

  30. Gustavo Ruiz at | | Reply

    Hi !

    I have a problem in the software point.

    I put every electronic connection like the post and i think it is ok but when I configure the software part of the raspberry, I get the following comments:

    dmesg | grep -i spi
    mcp 251x spi0.0 : MCP251x didn’t enter in conf moder after reset
    mcp251x spi0.0: Probe failed, err = 16
    mcp251x; probe of spi0.0: Probe failed with error -16

    More information in this image: https://ibb.co/4PvbLYC

    Do you know what kind of error can be treated and how could it be solved?

    Thanks my friendo

    1. Gustavo Ruiz at | | Reply

      Something?

  31. Jack Kersey at | | Reply

    Hi friend,

    I got a question for you:

    when i put this

    • dmesg | grep -i spi
    • dmesg | grep -i can
    • sudo ip link set can0 up type can bitrate 500000

    And then ifconfig to check the connections I see that in the two RP i receive two different answers. One of the two, i think it is OK:

    can0: flags=193 mtu 16…

    but in the other i recieve the next message:

    can0: flags=128 mtu 16…

    why i recieve two different message??

  32. Alexandre Ribault at | | Reply

    Hi,

    I hope you still answer to comment.
    You post is really awesome. It’s clear !
    I need to communicate on CAN Bus at work, and I will higly probably use your diagrams, and your informations to understand what I’m doing !

    Do you know if we can dynamically change CAN Speed ?
    When I start my system, every components communicate at 250kb/s. After an authentification step, a kind of master on BUS, says to all slaves the new speed ( It can be 500kb/s or 1mb/s).

  33. Nickolos at | | Reply

    Hello,

    I have 2 Raspi having HAT controller on each of them. I assume that MCP2512 is already in-built in the controller and so I use a CAN bus wire to connect the H and L ports of both setup. I used candump and cansend commands but I don’t get the data displayed. However the commands work when the loopback is on. Could you let me know what could be the issue?

  34. Daniel at | | Reply

    Hi, I spotted another error in your schematic (unfortunately only after I designed a custom PCB according to the schematic and was wondering why I have too low voltage on the RxD pin at the MCP2515):
    The 10kOhm resistor for the voltage divider between MCP2515 and MCP2551 RxD pins needs to be at the MCP2515-side such that the current passes the 10kOhm resistor first when travelling out of the MCP2551 and then split up between direct trace to MCP2551 RxD pin and GND via 22kOhm.

    I hope this helps one or the other here!

    Otherwise: Thanks for the write-up!

  35. Daniel at | | Reply

    Sorry, too late, too tired:
    I meant “The 10kOhm resistor needs to be at the MCP2551(!)-side …”

  36. Keith at | | Reply

    THANK YOU for the effort on this Youness and everyone else.
    My interest is more in using the Pi as a main ecu with many canbus nodes.
    I like the car stuff, I have done that before with PC Tactrix 2 and excellent help
    from the romraider community.
    I hope I can contribute a little here in the near future.

Please comment with your real name using good manners.

Leave a Reply