Saturday, June 27, 2015

Arduino Uno R3 and MCP2515 CAN breadboard proto type

I have created a  simple prototype of CAN network using  the following setup
1. Arduino Uno R3 x1
2. CAN bus shield  from seeedstudio.com  x1
3. CAN Shield circuit wired on breadboard using MCP2515 & MCP2551   x1


One node is configured to send the CAN message over CAN id 0x01. the first byte of CAN frame is incremented by one to use it is a running counter. The other node is configured to receive the CAN message and print it on serial console.




Result observed on Serial Monitor




Arduino sketch attached for your refernce

// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13


#include <SPI.h>
#include "mcp_can.h"


// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
const int SPI_CS_PIN_CAN_BREADBOARD = 9;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin
MCP_CAN CAN2(SPI_CS_PIN_CAN_BREADBOARD);                   

void setup()
{
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN-1 BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN-1 BUS Shield init fail");
        Serial.println("Init-1 CAN BUS Shield again");
        delay(1000);
        goto START_INIT;
    }
    if(CAN_OK == CAN2.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN-2 BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN-2 BUS Shield init fail");
        Serial.println("Init-2 CAN2 BUS Shield again");
        delay(1000);
        goto START_INIT;
    }
}