CAN-BUS Error 5 and solid INT LED

I’ve had good success with the shield and the provided examples, however I’ve hit a problem that has me stumped.

About 50% of the time it works and 50% it doesn’t

When it works the Int LED flashes and the code receives and/or sends perfectly.

When it doesn’t work the Int LED goes immediately solid on start-up and I know that it has stalled.

If I do the following it works 100% of the time:
Disconnect one of the CAN wires.
Then power up the Arduino
Then attach the CAN wire that was disconnected

Adding some debug code it appears that when it stalls I get 5 in response to CAN.checkError()

Additional information is that the CAN.begin always seems to return OK, but if the stall occurs the error is present after a few ms of the begin.

Any ideas or suggestions?

There are two way to check if the canbus shield receive data, interrupt or check by a a function: CAN.checkReceive() . When there is some data coming, this will return CAN_MSGAVAIL(no data coming is CAN_NOMSG ).

I found that when the data is too fast, such as less than 10ms, the interrupt way will cause some problem. But you can use the other way, checkReceive(). you can try the following test code:

[code]//send.ino
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>

void setup()
{
Serial.begin(115200);
// init can bus, baudrate: 500k
if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print(“can init ok!!\r\n”);
else Serial.print(“Can init fail!!\r\n”);
}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad flame, data len = 8, stmp: data buf
for(int i=0; i<10000; i++)
{
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(1);
}

Serial.println(“send 10000 over”);
while(1);
}[/code]

//recv.ino
// demo: CAN-BUS Shield, receive data
#include "mcp_can.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char
 
INT8U Flag_Recv = 0;
INT8U len = 0;
INT8U buf[8];
char str[20];
void setup()
{
    Serial.begin(115200);
    CAN.begin(CAN_500KBPS);                   // init can bus : baudrate = 500k
    //attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
  
}
 
/*
void MCP2515_ISR()
{
     Flag_Recv = 1;
}*/
 
long sum=0;
void loop()
{
    //if(Flag_Recv)                   // check if get data
    if(MCP_STAT_RXIF_MASK == CAN.checkReceive())
    {
       sum++;
       Serial.println(sum);
      //Flag_Recv = 0;                // clear flag
      CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
     // Serial.println("CAN_BUS GET DATA!");
      //Serial.print("data len = ");
      //Serial.println(len);
     /* for(int i = 0; i<len; i++)    // print the data
      {
        Serial.print(buf[i]);Serial.print("\t");
      }*/
      //Serial.println();
    }
}

I had uploaded the can bus code, you can download the latest code here: github.com/Seeed-Studio/CAN_BUS … ree/master

Thank you, the different approach appears to be working without the problem.

Hello guys i have the same problem i have a can bus shield v1.2 and a opel astra car, if i only want to receive data it works fine, when i try to send data the checkerror() it show 0 0 then 5 CAN_CTRLERROR. Can someone please help me? Almost forgot i use a arduino uno and the SPI_CS_PIN = 9;