Hi everyone, I started a project where I need to connect 2 Arduino with CAN-BUS protocol. For this I went to the simplest and tried to make ONLY the communication via CAN bus of the 2 Arduino with a simple example I found in the library that you can download directly from Arduino called: "CAN_BUS_Shield-master".
As known, there are 2 codes (1 for the transmitter and 1 for the receiver), the transmitter one works just fine (I think). The problem comes in the receiver code, when it checks if data is coming with sentences like:
if(CAN_MSGAVAIL == CAN.checkReceive())
I tried different codes and libraries but the problems repeat at the same point (when the receiver checks if data is coming) and I know the transmitter is sending...
Can someone explain what is wrong? I will attach the code and also a schematic of what and how i connected.
TRANSMITTER CODE:
#include <mcp_can.h>
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// 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 ledHIGH = 1;
const int ledLOW = 0;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
void loop()
{ SERIAL.println("In loop");
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70, 0, 8, stmp);
delay(1000); // send data once per second
}
RECEIVER CODE:
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// 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 LED = 8;
boolean ledON = 1;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
SERIAL.begin(115200);
pinMode(LED,OUTPUT);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");
}
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
unsigned long canId = CAN.getCanId();
SERIAL.println("-----------------------------");
SERIAL.println("get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
SERIAL.print(buf[i]);
SERIAL.print("\t");
if(ledON && i==0)
{
digitalWrite(LED, buf[i]);
ledON = 0;
delay(500);
}
else if((!(ledON)) && i==4)
{
digitalWrite(LED, buf[i]);
ledON = 1;
}
}
SERIAL.println();
}
}
SCHEMATIC (The connection between CANs is missing I am concerned. Sorry about that)