Example Code for Arduino-Basic CAN BUS Polling Mode
In this section we will demonstrate basic CAN BUS receiving and sending functions. Receiving uses polling mode. You can accept any ID standard data frame or extended frame. The transmitting node sends a standard data frame which ID is 0x06 per 100ms.
Hardware Preparation
| Item | SKU / Model | Qty | Link |
|---|---|---|---|
| Arduino UNO | Arduino UNO | 2 | Purchase |
| CAN-BUS Shield V2 | DFR0370 | 2 | Purchase |
| Dupont Cable | — | 4 | — |
Software Preparation
| Item | Version / Description | Link |
|---|---|---|
| Arduino IDE | v1.6.5 | Download |
| CAN-BUS Shield Library | MCP2515 (v2.0) | GitHub |
| Installation Tutorial | Arduino IDE Library Installation | View Guide |
Wiring Diagram

Other Preparation Work
- Install the CAN-BUS Shield v2.0 Library in Arduino IDE.
- Ensure the power switch on the shield is set correctly (OFF for Arduino power, ON for DB9 power).
- Connect the CAN-H and CAN-L lines between the two shields using Dupont cables.
Sample Code
Receiver Code
/******************************************************************************
* demo: CAN-BUS Shield, receive data with check mode
* send data coming to fast, such as less than 10ms, you can use this way
* Jansion, 2015.5.27
******************************************************************************/
#include <SPI.h>
#include "df_can.h"
const int SPI_CS_PIN = 10;
MCPCAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
int count = 50; // the max numbers of initializint the CAN-BUS, if initialize failed first!.
do {
CAN.init(); //must initialize the Can interface here!
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("DFROBOT's CAN BUS Shield init ok!");
break;
}
else
{
Serial.println("DFROBOT's CAN BUS Shield init fail");
Serial.println("Please Init CAN BUS Shield again");
delay(100);
if (count <= 1)
Serial.println("Please give up trying!, trying is useless!");
}
}while(count--);
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}
Sender Code
// demo: CAN-BUS Shield, send data
#include <df_can.h>
#include <SPI.h>
const int SPI_CS_PIN = 10;
MCPCAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
int count = 50; // the max numbers of initializint the CAN-BUS, if initialize failed first!.
do {
CAN.init(); //must initialize the Can interface here!
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("DFROBOT's CAN BUS Shield init ok!");
break;
}
else
{
Serial.println("DFROBOT's CAN BUS Shield init fail");
Serial.println("Please Init CAN BUS Shield again");
delay(100);
if (count <= 1)
Serial.println("Please give up trying!, trying is useless!");
}
}while(count--);
}
unsigned char data[8] = {'D', 'F', 'R', 'O', 'B', 'O', 'T', '!'};
void loop()
{
// send data: id = 0x06, standrad flame, data len = 8, data: data buf
CAN.sendMsgBuf(0x06, 0, 8, data);
delay(100); // send data per 100ms
}
Result
Receiver serial port output shows the incoming data frame:

Was this article helpful?
