CAN Bus with Arduino

Communication protocols like UART (Serial), I2C and SPI are very popular because several peripherals can be interfaced with Arduino using these protocols. CAN (Controller Area Network) is another such protocol, which isn't very widely popular in general, but find several applications in the automotive domain.

While going into the details of CAN bus is beyond the scope of this article, you can find the relevant information here. However, here are a few things you should know −

CAN is a message-based protocol (i.e., the message and content are more important than the sender). A message transmitted by one device is received by all devices,including the transmitting device itself.

If multiple devices are transmitting at the same time, the device with the highest priority continues transmission, while others back off. Note that since CAN is a message based protocol, IDs are assigned to messages and not the devices.

It uses two lines for data transmission CAN_H and CAN_L. The differential voltage between these lines determines the signal. A positive difference above a threshold indicates a 1, while a negative voltage indicates a 0

The devices in the network are called nodes. CAN is very flexible in the sense that newer nodes can be added to the network, and nodes can be removed as well. All the nodes in the network only share two lines.

Data transmission happens in frames. Each data frame contains an 11 (base frame format) or 29 (extended frame format) identifier bits and 0 to 8 data bytes.

Now, Arduino Uno doesn't support CAN directly like it supports UART, SPI and I2C. Therefore, we will use external module, MCP2515 with TJA1050 transceiver, that interfaces with Arduino via SPI, and the transmits the message using CAN.

Circuit Diagram

The Circuit Diagram for one node (transmitter) is shown below.

As you can see above, the Vcc line of the module is connected to 5V of Arduino, GND to GND,CS to pin 10, SO to pin 12 (MISO), SI to pin 11 (MOSI) and SCK to pin 13 (SCK).

On the receiving side, the connections are similar, except that the INT pin of the module is connected to pin 2 of Arduino.

The two (transmitter and receiver) are be joined together by CAN_H and CAN_L lines (CAN_H to CAN_H and CAN_L to CAN_L).

Library Required

We will use this library from Seeed Studio. This library won't be found in the Library Manager of Arduino. The procedure for downloading a third-party library is given here − https://www.tutorialspoint.com/using-a-third-party-library-in-arduino

Code Walkthrough

Once you have the library installed, you can find the send and receive_interrupt examples in File — Examples — CAN_BUS Shield.

We will be using slightly simplified versions of these examples.

The code for SEND is given below −

Much of the code is self-explanatory. We will go through the implementation in brief.

We initialize CAN using the Chip Select pin (Pin 10 in our case).

We set the baud rate to 500 kbps in the Setup and check if CAN began properly.

We are transmitting 8 bytes in each frame to the received. The bytes are all 0 to begin with.Within loop, we keep incrementing the last byte, till it reaches 100, then add one to the second last byte and again keep incrementing the last byte till it reaches 100, and so on. We increment the third-last byte if the second-last byte reaches 100. This gives us several iterations to go on.

The important function is −

The first argument is the ID of the message (0x00), the second argument represents whether we are using the base format or the extended format (0 for base, and 1 for extended), the third argument is the length of the data (8), and the fourth is the data buffer.

This will keep sending 8-byte data frames to the receiver.

The code for RECEIVE is shown below −

As you can see the initial variable declarations and the setup is similar to SEND. The only difference is that an interrupt is attached to digital pin 2. Remember that the INT pin of the module goes LOW whenever a message is received. Therefore, a FALLING EDGE interrupt is attached to pin 2, which is connected to the INT pin of the module.

Within the MCP2515_ISR function, we simply set the flagRecv to 1, which is checked inside the loop.

When the flagRecv value is 1, the available data in the CAN buffer is checked (using CAN.checkReceive()) and read and printed on the Serial Monitor. The function to read the data is −

The first argument indicates the length of data available, and the second argument is the buffer to store the incoming data.

I hope you enjoyed this article. You are encouraged to go through the other examples that come in with this library.


Источник: www.tutorialspoint.com