Interfacing ElecFreaks CAN Shield V1.2 With Arduino

Hello,

I am trying to interface Arduino and ElecFreaks CAN Sheild. Downloaded Sample Code From their https://www.elecfreaks.com/estore/can-bus-shield.html website.
I tried to verify the Receive CAN Message Sample Code but it shows this error.
exit status 1
‘CAN’ was not declared in this scope

Any Suggestion Would be helpful.
Thanks in Advance.

Code:
// demo: CAN-BUS Shield, receive data
#include <mcp_can.h>
#include <SPI.h>

unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];

void setup()
{
CAN.begin(CAN_500KBPS); // init can bus : baudrate = 500k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(115200);
}

void MCP2515_ISR()
{
Flag_Recv = 1;
}

void loop()
{
if(Flag_Recv) // check if get data
{
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*);Serial.print("\t");*

Doesn't that device require installation of library?

Please let us know which library you are using. Some libraries instantiate objects in the header files in most cases you need to do this in your code. For this you need to know what that CAN class is called.

Additionally, CAN always needs at least one other node on the bus, and it needs to be configured correctly Otherwise you will get send errors.

CAN is a differential bus and the bus needs to be terminated on both ends. Some modules have the termination resistor on the board and a jumper that needs to be set. But only for the two end nodes or the bus cable needs to have these at the end.

Installed all Libraries as per user guide instructions. Downloaded MCP Library and Uploaded CAN_receive code into Arduino. But I’m using ElecFreaks CAN-Bus Shield.

Serial Monitor Error:
Entering Configuration Mode Failure…
Error Initializing MCP2515…
MCP2515 Library Receive Example…

Code:

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string

#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10

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

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println(“MCP2515 Initialized Successfully!”);
else
Serial.println(“Error Initializing MCP2515…”);

CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.

pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input

Serial.println(“MCP2515 Library Receive Example…”);
}

Check My Connection File.

CAN-H and CAN-L From Shield is connected with the ECU CAN-H and CAN-L.
Termination Resistor-120ohm

I had a look into the library from the ElecFreaks website. The mcp_can.h instantiates an object called CAN. If the files are in the correct folder you should be able to compile the code without your own CAN0 object.

Make sure File -> Preferences -> "Sketchbook location" points to the correct folder. The structure should look like this. Note when you set the location the libraries folder will be created automatically by the IDE.

myClass in your case should be mcp_can

{sketchbook folder}
├── libraries
│ └── myClass
│ ├── examples
│ │ └── myClass
│ │ └── myClass.ino
│ ├── keywords.txt
│ ├── library.json
│ ├── library.properties
│ └── src
│ ├── myClass.cpp
│ └── myClass.h
└─── send
└── send.ino

Are you using a second library. The original one only has one CAN.begin function. You have two different versions.

Do you have an oscilloscope to check the SPI signals?

Thanks @Klaus_k

I found the error.It's due to Multiple CAN Libraries.Thanks for pointing it out.Finally Solved it.

What i learned:
Make Sure to add Suitable Libraries for your hardware.After Adding seeed can_Shield library my problem solved.(GitHub - Seeed-Studio/Seeed_Arduino_CAN: Seeed Arduino CAN-BUS library - MCP2518FD&MCP2515&MCP2551).
Hardware:Elecfreaks can shield
Select correct CS Pin.

Thank you all... :slight_smile: