1

I tried searching codes for MCP2515 with Arduino, On Arduino Forums, Instructables, Electronics Hub, YouTube etc.

All of them were codes for a transmitter node and a receiver node. Sensors on one node and result on the other end.

Can we connect two Arduinos using MCP2515 and communicate TWO WAY?

2 Answers 2

0

I never used CAN bus myself, but what you could do is one of the following:

  • Letting the transmitter send a specific (short) message to the receiver. In case the receiver has something to transmit, it sends a specific reply back, so the transmitter can request the receiver to send the actual message to be sent. The disadvantage is that polling is needed.
  • If you want the receiver to be able to send its message as fast as possible, add a separate wire next to the CAN bus communication. If that line is LOW or HIGH (decide yourself), then the transmitter can 'immediately' send a message to have the receiver send its actual message.
4
  • 1
    Thanks! Tx Arduino runs only tx code, and Rx Arduino runs only rx code. So, it's not possible for Rx to transmit anything back. And, using CAN is for reducing no. of wires used, but that's a nice idea. Jun 24, 2021 at 9:27
  • 1
    By your first point, I'll try to merge the tx & rx commands, and temporarily make TX Arduino receive data, and vice versa. Jun 24, 2021 at 9:31
  • 1
    Let us know how you do!
    – Gil
    Jun 25, 2021 at 19:45
  • 1
    Per the Bosch specification, which is readily available, The CAN bus does arbitration automatically by a defined priority on a bit by bit basis. To send a message with the MPC2515 you load it and when the bus becomes available it will transmit it, if there is an error it will retry many times (internal counter) and if it fails you will know. When a message is sent it must receive an ACK in a specified part of the can frame. This can be generated by any node. Your internal protocol needs to determine if the right node got the message.
    – Gil
    Aug 19 at 17:44
0

---Yes--- It should work nicely. They operate only in simplex mode (sending or receiving) to the outside world. Internally they also monitor what they are sending for error checking etc. From the user point they would appear to be bidirectional because they can switch between send and receive messages rapidly, message by message. Of course the bus speed helps. You can get upto 1 Meg with these. I use Cory Fowler's "mcp_can.h". You can get it at: "https://github.com/coryjfowler/MCP_CAN_lib". The library has both send and receive .ino files, get them to work first. Since CAN is priority Based NDA Non Destructive Arbitration the higher priority message will prevail regardless of who else is on the bus or is sending a message.

Do not forget to put jumpers on the module to terminate the bus. Note the bus gets terminated only at the physical ends. The bus design impedance is 60 Ohm, the termination resistors are 120 ohms.

You simply combine the send and receive sections of code in your module, nothing special to do. You initialize both send and receive at the same time with basically the same information. You will find the information for this in the setup() section of the code. Be sure to check crystal frequencies, I have seen both 8 and 16 Mhz crystals on these boards.

Per the Bosch specification, which is readily available, The CAN bus does arbitration automatically by a defined priority on a bit by bit basis. To send a message with the MPC2515 you load it and when the bus becomes available it will transmit it, if there is an error it will retry many times (internal counter) and if it fails you will know. When a message is sent it must receive an ACK in a specified part of the can frame. This can be generated by any node. Your internal protocol needs to determine if the right node got the message. By the time your external logic can determine it wants to send a message and send a signal it will have already been sent in a standard CAN setup. Any number of modules can start transmitting but the arbitration will weed them out and the highest priority message will get through. Every node will receive every message, depending on its filtering etc it may accept it or ignore it. You can pole or take an interrupt any time it receives a message etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.