Example Code for Arduino-Three Node CAN Network

The doc post offers example code for a three-node CAN network using Arduino, detailing nodes' roles in data transmission and reception, enhancing understanding of network communication.

Hardware Preparation

Item SKU / Model Qty Link
Arduino UNO Arduino UNO 3 Purchase
CAN-BUS Shield V2 DFR0370 3 Purchase
Dupont Cable 6

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

dfr0370 three

Other Preparation Work

  1. Connect CAN-H and CAN-L lines between all three nodes.
  2. Configure each node's mask and filter registers as per the code.

Sample Code

Node1 Code

Same as Specified ID Data Frame Receiver Code.

Node2 Code

#include <SPI.h>
#include "df_can.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];
void setup()
{
    Serial.begin(115200);
    int count=50;
    do {
        CAN.init();
        CAN.init_Mask(MCP_RXM0,0,0x3ff);
        CAN.init_Mask(MCP_RXM1,0,0x3ff);
        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);
}

void MCP2515_ISR()
{
    flagRecv=1;
}

unsigned char data[] = "node 2";
void loop()
{
    if(flagRecv)
    {
        flagRecv=0;
        CAN.readMsgBuf(&len, buf);
        Serial.println("\r\n------------------------------------------------------------------");
        Serial.print("Get Data From id: ");
        Serial.println(CAN.getCanId());
        for(int i=0; i<len; i++)
        {
            Serial.print(buf[i]);
            Serial.print("\t");
        }
        Serial.println();
    }
    CAN.sendMsgBuf(0x08,0,sizeof(data),data);
    delay(1000);
}

Node3 Code

#include <SPI.h>
#include "df_can.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];
void setup()
{
    Serial.begin(115200);
    int count=50;
    do {
        CAN.init();
        CAN.init_Mask(MCP_RXM0,0,0x3ff);
        CAN.init_Mask(MCP_RXM1,0,0x3ff);
        CAN.init_Filter(MCP_RXF5,0,0x08);
        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);
}

void MCP2515_ISR()
{
    flagRecv=1;
}

unsigned char data[] = "node3";
void loop()
{
    if(flagRecv)
    {
        flagRecv=0;
        CAN.readMsgBuf(&len, buf);
        Serial.println("\r\n------------------------------------------------------------------");
        Serial.print("Get Data From id: ");
        Serial.println(CAN.getCanId());
        for(int i=0; i<len; i++)
        {
            Serial.print(buf[i]);
            Serial.print("\t");
        }
        Serial.println();
    }
    CAN.sendMsgBuf(0x09,0,sizeof(data),data);
    delay(1000);
}

Result

Each node's serial port output shows data from allowed IDs:

DFR0370

Additional Information

This example shows how to build a multi-node CAN network with selective data transmission and reception.

Was this article helpful?

TOP