It should be simple!

Hi guys I’m after a little help.

I have added this CAN BUS shield https://www.sparkfun.com/products/10039 into an Arduino UNO and have a simple piece of code, but I cannot see anything coming out of the CAN H and CAN L.

The CAN BUS module initializes ok, this is my code.

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

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

void setup()
{
  Serial.begin(115200);
  // init can bus, baudrate: 500k
  if(CAN0.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
  CAN0.sendMsgBuf(0x00, 0, 8, stmp);  
  delay(100);                       // send data per 100ms
}

i have attached a CAN BUS sniffer to the CAN H and CAN L to see if there is any traffic, see picture attached.

AFAIK the can bus should be terminated into something, meaning it should have a load on the end of both lines. I would start investigating this a bit deeper to ensure the CanBus hardware is connected as intended, before suspecting the code.

Anders53: AFAIK the can bus should be terminated into something, meaning it should have a load on the end of both lines. I would start investigating this a bit deeper to ensure the CanBus hardware is connected as intended, before suspecting the code.

Do you not think the CAN snifter would not be a load?

I dont know. Depends on its specifications, which I cant read :slight_smile:

Anders53: I dont know. Depends on its specifications, which I cant read :-)

I will try a 120 ohm resistor.

I added a 120 ohm resistor to both the High and Low but still that same.

Things are narrowing in then. Do you get your OK message from your CAN init in setup ?

Looking over your code again, I can not see where you set the CAN shield to enabled state ? You define the pin, but dont set it.

Anders53: Things are narrowing in then. Do you get your OK message from your CAN init in setup ?

Looking over your code again, I can not see where you set the CAN shield to enabled state ? You define the pin, but dont set it.

Ah, how do i enable the shield? Yes i get the Init message.

Look in the specs. My guess is the enable should have a HIGH input, but I could be wrong.

I think it must be enabled because i'm getting the init message.

epic question : what happens if you set it HIGH ? If that enable pin is not declared and initialised, it would be set as an input, after Arduino power up and reset.

I'd like to have things under control, not assuming, when there is a functionality problem.

I set it high, just the same, i pasted an image of my canbus software.

So now we need to set it LOW and see what happens :-)

Pls enclose your code with added chipselect.

Still the same on low.

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

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


void setup()
{
  Serial.begin(115200);
  digitalWrite(10, LOW);
  // init can bus, baudrate: 500k
  if(CAN0.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
  CAN0.sendMsgBuf(0x00, 0, 8, stmp);  
  delay(100);                       // send data per 100ms
}

I searched for some examples using the mcp_can library. I found 2 sketches covering send and receive on the CANbus here : mcp_can lib & sample sketches

Try download the 2 ino sketches and figure out what they are doing to send and receive on the bus.

I have copied your sketch and imported the mcp_can lib into my Arduino environment, and can not compile it without getting errors.

I got it working but i now have a new problem.

I moved the resistor from the terminals to the end of the circuit and it worked.

My new problem is that i really need this not to work with the UNO but the Linkit ONE board but the module just wont initilise.

This is where I have to get off the train :slight_smile:

Anders53: This is where I have to get off the train :)

lol, wish i could too.

I cheated, i put the CAN BUS module on the UNO board and just sent a digital signal to it from the Linkit ONE board to send the CAN BUS massage.

All works a treat,