CAN-BUS Shield noob question

Hello.

I am completely new to all this Arduino things, but really want to use it.

I have iteaduino Leonardo and Seeedstudio CAN-BUS shield. I used wires to connect shield to Leonardo. They are digital pins 2, 9-13, Vin and GND. Is that all pins wich the shield uses? Or I forgot something? I connected CAN terminal CAN H and CAN L wires to my Skoda Superb CAN H and CAN L wires and didn’t use the DB-connector.

I uploaded the following code to Leonardo. This is the receive example with the only change of the bitrate.

[code]// 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()
{
CAN.begin(CAN_100KBPS); // 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[i]);Serial.print(”\t”);
}
Serial.println();
}
}[/code]

When I did all this things, I don’t get any serial output. Did I miss something?

I tried to make some debuging.

When I make CAN.begin(CAN_100KBPS), it returns CAN_FAILINIT, not CAN_OK. So what can be the problem? How can I understan why it fails?

Also, should it return CAN_OK if the shield is not connected to can_l and can_h of other device?

yes, it should return can_ok…

I know, but it doesn’t.
I read that CAN Shield is not compatible with arduino leonardo SPI pinout.

I will try with UNO and test it.

I thought I posted before I left work…

First, you didn’t mention having power connected, just ground, but I will assume you have power (+5V) connected to the CAN shield.
Second, the Leonardo does not have SPI on its Digital 11-13 pins. SPI is only available on the ICSP header. You mentioned connecting the two with wires, can you clarify where you have the shield connected on the Leonardo? (Digital pin 10 is still used for the shield’s CS and digital pin 2 for the interrupt input.)
Third, confirm MOSI is connected to MOSI, MISO to MISO and CLK to CLK…

While its not crucial for operation, the Leonardo’s /RESET line should be tied to the shield’s /RESET line so everything starts in a known state.
Also, I would use the version of the library located on my GitHub. The receive example you posted is still using the Arduino’s interrupt handler which will cause the Arduino to hang during operation due to how the interrupts are used on the shield and the version of the library that example comes with has bugs.

First of all, thank you for you answer.

Of course, I connected power pin, just forget to mention it.

After I wrote all these posts I found that CAN shield is not compatible with Leonardo, so I took my UNO and tried a slightly modified example from your github library:

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10); // Set CS to pin 10

void setup()
{
delay(5000);
Serial.begin(115200);
if(CAN0.begin(CAN_100KBPS) == CAN_OK) Serial.print(“can init ok!!\r\n”);
else Serial.print(“Can init fail!!\r\n”); // init can bus : baudrate = 100k
pinMode(2, INPUT); // Setting pin 2 for /INT input
Serial.println(“MCP2515 Library Receive Example…”);
}

void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s)
rxId = CAN0.getCanId(); // Get message ID
Serial.print("ID: “);
Serial.print(rxId, HEX);
Serial.print(” Data: “);
for(int i = 0; i<len; i++) // Print each byte of the data
{
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print(“0”);
}
Serial.print(rxBuf[i], HEX);
Serial.print(” ");
}
Serial.println();
}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/[/code]

That gives me this output:

can init ok!! MCP2515 Library Receive Example...

So, as I see, the library itself works, but after that there’s complete silence. No RX blinking, no messages received. I have CAN wires connected to my car’s wires, and it is know to work because CAN adapter for my clarion stereo works fine.

What else could I check to receive something?

For what its worth, I only recently acquired the Seeed studio CAN-shield thanks to a generous forum user. I had been using an isolated version I made on a proto-shield. The first thing I did with the Seeed studio CAN-shield before I soldered on the stacking headers was remove the incorrect 60 ohm termination resistor, that is enabled by default, off my boards. That resistor should be 120-ohms and NOT enabled by default…

I recommend you try to either disable the termination by cutting the solder-blob-jumper trace located behind the wire-terminals or remove the resistor entirely. Both have equal difficulty, one just requires a nice hot soldering iron. I do remember reading that OBD2 diagnostic devices are not supposed to have a termination so I am not exactly sure why the CAN_shield has it enabled by default or where the 60 ohm resistance came from, but that’s all I can think of right now. I would expect the RX LED to blink if the bus wires were flipped as I know that will cause nothing to come across…

Here is a picture:

Well, my english is not so well. Should I remove the R3 resistor completely or cut the P1?

Also, what the OBD switch is for? What it does?

Oh, my apologies.

That is up to you, either will remove the termination.

The switch connects power from the DE9 connector to the Vin of the Arduino.

Thank you very much! I unsoldered the resistor and everything works now. The next step is to make the shield work with Leonardo.

Also, one more question.

There is a “next track” key on the steering wheel of my car. When I press it, i can get one or more than one message “5C3 01 00 00 00”. Sometimes I get one message, sometimes 2 or 3. I think that it is because of repetition period. Is there any way to know how many time passed since previous message?

Thats good!

Not with the MCP2515. I would set a variable to the current CPU time after receiving the message and check the difference between that variable and the current CPU time when the next message comes in. Then either ignore the message or goto the next track depending on the duration difference.

Just to let you know. Now CAN shield work with my Leonardo. I connected pins 11, 12 and 13 to ICSP pins on Leonardo according to tha table located here: arduino.cc/en/Reference/SPI

Everything works like a charm! Thank you so much one more time!