Sparkfun CAN shield with UNO

Hi there,

I am trying to get the example code working with the following CAN shield: CAN-BUS Shield Hookup Guide - learn.sparkfun.com .

After getting the initialisation working with the Arduino UNO (actually not arduino but a knock off UNO), it doesn't give me anything on the CAN analyser tool when I try to run the example code below:

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600);
  Serial.println("CAN Write - Testing transmission of CAN Bus messages");
  delay(1000);
  
  if(Canbus.init(CANSPEED_500))  //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
    
  delay(1000);
}

//********************************Main Loop*********************************//

void loop() 
{
tCAN message;

        message.id = 0x631; //formatted in HEX
        message.header.rtr = 0;
        message.header.length = 8; //formatted in DEC
        message.data[0] = 0x40;
  message.data[1] = 0x05;
  message.data[2] = 0x30;
  message.data[3] = 0xFF; //formatted in HEX
  message.data[4] = 0x00;
  message.data[5] = 0x40;
  message.data[6] = 0x00;
  message.data[7] = 0x00;

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message);

delay(1000);
}

Before you say the CAN analyser tool may be broken, I also tried with this code (MCP_CAN_lib/CAN_send.ino at master · coryjfowler/MCP_CAN_lib · GitHub) from a different library and it gave me the error message in the arduino serial moniter "Error Sending Message...".

I am using a female DB9 to DB9 cable from the CAN shield to the CAN analyser tool. Is it maybe that this is the problem? Do I have to reconfigure the jumpers in 6c here: CAN-BUS Shield Hookup Guide - learn.sparkfun.com ? If so, how exactly do I do that, it says something about soldering but it's not quite clear to me how I should solder those pads.

Another idea I had was that the lack of termination resistors is causing the problem, because as far as I understand there are no termination resistors included in the shield, correct? If so, how do I add them if I want to use the DB9 cable to send receive?

Attached is a picture of my setup. There is an LCD attached, but it is not doing anything for the moment. Any help is greatly appreciated.

Is the CAN analyzer set to 500 kbps?
Is the CAN analyzer in "listen only" mode? If so, set it to "normal" or whatever setting generates ACKs. The sending device will flag an error if it doesn't see ACKs.
Make sure you have a 3 wire connection, ground is needed.

Check the DB9 cable wiring. CAN expects a one-to-one connection while serial cables have cross overs.

I created a CAN bus cable with many DB9 connectors using a flat ribbon cable and connectors that could be crimped. Make sure they are all the same way around. :slight_smile: You could add two 120Ohm resistors at the end of this type of cable or to two DB9 connectors with the resistors at the end.

And just confirming what mikb55 said. CAN always needs at least two active nodes because the protocol requires each message to be acknowledged. This is part of what makes the bus so robust.

Multiplier7:
Another idea I had was that the lack of termination resistors is causing the problem, because as far as I understand there are no termination resistors included in the shield, correct? If so, how do I add them if I want to use the DB9 cable to send receive?

Yes, the schematic does not show any termination resistor. You could add one to the CANL and CANH pin holes. I think that is the two wires you are holding.

If you are attaching your board later to a live CAN bus then you must remove the resistor again because the bus will already have them.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.