This Article is to make readers to explore about how to interface CAN Bus with Arduino which is used in the Communication of Sensors, Actuators and controllers in a car.
This project helps in understanding the insights of CAN protocol interfacing with Arduino. Controller Area Network or CAN protocol is a methodology of communication between various electronic devices like engine management systems, gear control, active suspension, ABS, lighting control, air conditioning, airbags, central locking etc. embedded in an automobile. For further learnings refer this article.
Here you will get idea about the programming of Arduino to interface with CAN Controller (MCP2515) to act as a transceiver.
DESCRIPTION:
Prerequisites & Equipment:
You will need the following:
-
Two Arduino Board or Arduino clone(Here is a guide if you need)
-
A 5v TTL -UART Bluetooth module.
-
Arduino IDE for the programming.
-
Two MCP2515 CAN Tranciever.
Features MCP2515 CAN Tranciever:
-
Implements CAN V2.0B at up to 1 Mb/s
-
SPI Interface up to 10 MHz
-
Standard (11 bit) and extended (29 bit) data and remote frames
-
Two receive buffers with prioritized message storage
-
Two LED indicators
Demonstration:
Fig. 1: Prototype of Arduino based Transmitter and Receiver Circuits for communication over CAN Interface
1. Download the CAN-BUS Library code file for Arduino 1.0 and unzip it under the libraries directory of the Arduino IDE folder. For my computer’s setup, it’s in this directory
XXXarduino-1.0.1libraries
After copying files across, the directory
XXXarduino-1.0.1librariesDHT
2. Open the Arduino-1.0, and navigate to Examples you will find examples: receive_blink, receive_check, receive_interrupt, Send, Send_blink. Here we’ll use send and receive_check to check our CAN Bus.
Fig. 2: Screenshot of navigating to sample code for CAN Receiver in Arduino IDE
3. Upload Send examples to one Arduino Board. Choose the board by navigating to: Tools –>Serial Port–>COMX.
Note: Remember which board you select as a “send” node and which board you select as a “receive” node.
4. Open the “Serial Monitor” by selecting the Com Port which you selected for the Receiver, you will get the preset message “0 1 2 3 4 5 6 7” which is sent from the “send” node. This can be seen in the following picture.
Fig. 3: Screenshot of Arduino Serial Port receiving preset messages over CAN Interface
Functions in CAN Library:
1. Set the Baud Rate
To initialize the baud rate of the CAN Bus system use the following Functions.
The available baud rates are:
CAN_5KBPS, CAN_10KBPS, CAN_20KBPS, CAN_40KBPS, CAN_50KBPS, CAN_80KBPS, CAN_100KBPS, CAN_125KBPS, CAN_200KBPS, CAN_250KBPS, CAN_500KBPS and CAN_1000KBPS
2. Check Receive
The MCP2515 has two modes of Receive Check, one is Polling where the code checks the received frame and the other mode is Interrupt mode where, Separate Interrupt pin is used to notify the received frame.
The following is the Polling method to get the received frame.
INT8U MCP_CAN::checkReceive(void);
The function will return 1 if a frame is received, and 0 if nothing received.
3. Get CAN ID
If you want know the CAN ID of the Sending node when a message is received you vcan use the following Function.
INT32U MCP_CAN::getCanId(void)
4. Send Data
The below function is to send data onto the bus:
CAN.sendMsgBuf(INT8U id, INT8U ext, INT8U len, data_buf);
Where,
“id” gives CAN ID of the sending nodes.
“ext” ‘0’ means standard frame. ‘1’ means extended frame.
“len” gives the length of this frame.
“data_buf” is the main content of this message.
For example, In the ‘send’ example, we have:
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
CAN.sendMsgBuf(0x00, 0, 8, stmp); //send out the message ‘stmp’ to the bus and tell other devices this is a standard frame from 0x00.
5. Receive Data
The following function is used to receive data from the bus:
CAN.readMsgBuf(unsigned char len, unsigned char buf);
“len” gives the data length.
“buf” is the main content of this message.
Fig. 4: Prototype of Arduino based CAN Transmitter Circuit designed on a breadboard
Fig. 5: Prototype of Arduino based CAN Receiver Circuit designed on a breadboard
You may also like:
Circuit Diagrams
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.