Example Code for Arduino-CAN Data Logging to SD Card
The purpose of this experiment is as follows: the receiving node receives ten pieces of data transmitted from the transmitting node, then writes the data into the SD card of the CAN-BUS Shield, and finally reads the data out and prints it via the serial port.
Hardware Preparation
| Item | SKU / Model | Qty | Link |
|---|---|---|---|
| Arduino UNO | Arduino UNO | 2 | Purchase |
| CAN-BUS Shield V2 | DFR0370 | 2 | Purchase |
| Dupont Cable | — | 4 | — |
| Micro SD Card | — | 1 | — |
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 |
plus SD Library (included in Arduino IDE).
Wiring Diagram

Other Preparation Work
- Insert a formatted Micro SD card into the shield's SD card slot.
- Ensure the SD card is properly seated and the SPI CS pin for SD is set to 4.
Sample Code
Receiver Code
/**************************************************************************************************************
*demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
* when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
* Jansion, 2015-5-27
****************************************************************************************************************/
#include <SPI.h>
#include "df_can.h"
#include <SD.h>
const int SPI_CS_PIN =10;
MCPCAN CAN(SPI_CS_PIN);
unsigned char flagRecv=0;
unsigned char len=0;
unsigned char buf[8];
char str[20];
char sd_cspin=4;
File myFile;
void setup()
{
Serial.begin(115200);
int count=50;
Serial.print("Initializing can controlor...");
do {
CAN.init();
CAN.init_Mask(MCP_RXM0,0,0x3ff);
CAN.init_Mask(MCP_RXM1,0,0x3ff);
CAN.init_Filter(MCP_RXF0,0,0x04);
CAN.init_Filter(MCP_RXF1,0,0x05);
CAN.init_Filter(MCP_RXF2,0,0x60);
CAN.init_Filter(MCP_RXF3,0,0x07);
CAN.init_Filter(MCP_RXF4,0,0x08);
CAN.init_Filter(MCP_RXF5,0,0x09);
if(CAN_OK == CAN.begin(CAN_500KBPS))
{
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--);
attachInterrupt(0, MCP2515_ISR, FALLING);
Serial.print("Initializing SD card...");
if(!SD.begin(sd_cspin))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization success!");
myFile = SD.open("Node0x60.txt", FILE_WRITE);
if(!myFile)
{
Serial.println("open Node0x60.text failed!");
}
else
{
Serial.println("open Node0x60.text success!");
}
}
void MCP2515_ISR()
{
flagRecv=1;
}
char filewrite=1, fileread=0;
int i=0,j=0;
void loop()
{
if(flagRecv)
{
flagRecv=0;
CAN.readMsgBuf(&len, buf);
if(filewrite)
{
if(i++ <1)
{
myFile.write(buf, len);
}
else
{
myFile.close();
filewrite=0;
myFile=SD.open("Node0x60.txt", FILE_WRITE);
if(SD.exists("Node0x60.txt"))
{
Serial.println("example.txt exists.");
fileread=1;
}
else
{
Serial.println("example.txt doesn't exist.");
}
}
}
if(fileread)
{
Serial.println("printf the data that myFile has saved! ");
myFile.read(buf, len);
Serial.println((char *)buf);
Serial.println("");
myFile.close();
Serial.println("myFile closed!!!!");
fileread=0;
}
}
}
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);
void setup()
{
Serial.begin(115200);
int count=50;
do {
CAN.init();
if(CAN_OK == CAN.begin(CAN_500KBPS))
{
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()
{
CAN.sendMsgBuf(0x60,0,8,data);
delay(1000);
}
Result
Receiver serial port output shows the saved data from the SD card:

Additional Information
This example uses the shield's integrated SD card slot to log CAN data for later analysis.
Was this article helpful?
