Problem with CAN BUS Receive Data

Hello everyone,

i am trying to test Recieve and send signal using Seed-Arduino CAN Shield V2.0 using arduino Mega 2560 the sending signal working good actualy. but i have issue with recieve data:

The problem is that the code cannot get the data length and always get it zero so i cannot got the write signal as i need it to programm some digital stuff.

Aslo the Loop didnot want to work until i put (CAN_MSGAVAIL != CAN.checkReceive())
insteadt (CAN_MSGAVAIL = CAN.checkReceive()) . I have no idea why !

What i understand from the syntax example code that:
1- len=0 is a defined var will be filled as soon as the data length will delivered then gonna use this data to read it or control something else.

2- [[ CAN_MSGAVAIL == CAN.checkReceive() ]] this line is for checking if data is comming and will not be equal until data is comming, howerver if i send data throw CAN.
Actually (CAN_MSGAVAIL != CAN.checkReceive()) working and allow the loop to continiue. but the further Loop (for Loop) didn’t work as “len” always equal zero cause it didn’t get the new value.

I am testing the send and recieve using CANoe Software and i only can see the data that i send, but i cannot read this data back to my Arduino Program.


Is there anyway to check/Solve that or should i add something in the code.

I hope my attached code lead u to find the issue.



#include <mcp2518fd_can_dfs.h>
#include <can-serial.h>
#include <mcp2518fd_can.h>
#include “mcp2518fd_can.h”
#include <mcp_can.h>
#include <SPI.h>
#include “mcp2515_can.h”
#include <mcp2515_can.h>
#include <mcp2515_can_dfs.h>

#define CAN_2515

const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin

// Define Digital Signal for recieving Signal fromm CAN
const int LED = 13; // LED Pin Number
boolean ledON = 1;

// Define Analoge for Send data to CAN
int sensorPin = A0;
int sensorValue = 0;
int cantxValue = 0;

//-----------------------------------S E T U P--------------------------------------------

void setup() {
pinMode(LED, OUTPUT); // Output Signal delaration for the LED

while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println(“CAN init fail, retry…”);
SERIAL_PORT_MONITOR.println(“CAN init ok!”);

//-----------------------------------L O O P-----------------------------------------------

void loop() {

unsigned char len = 0;
unsigned char buf[8];
unsigned long canId = CAN.getCanId();

if (CAN_MSGAVAIL == CAN.checkReceive()) {
CAN.readMsgBufID(&canId, &len, buf);
Serial.print("data len = ");
Serial.print(“get data from ID: 0xA”);
Serial.print(canId, HEX);
Serial.print("data buf = ");

  for (int i = 0; i < len; i++) {                                                    // print the data
    if (ledON && i == 0) {
      digitalWrite(LED, buf[i]);
      ledON = 0;
      Serial.println("LED OFF!");
    else if ((!(ledON)) && i == 4) {
      digitalWrite(LED, buf[i]);
      ledON = 1;
      Serial.println("LED ON!");
  Serial.println("for Loop not running");
else Serial.println("CAN READ CODE Not Working");

// send data

sensorValue = analogRead(sensorPin); //Each CAN bus byte can store a value between 0-255.
cantxValue = sensorValue / 4; //Dividing sensorValue by 4 puts us in that range.
Serial.print(“cantxValue: “);

//Create data packet for CAN message
unsigned char canMsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, cantxValue};

// send data: id = 0xA, standrad flame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0xA, 0, 8, canMsg);