Arduino + CanBus Shield(No Data)

Hi
Now i’m making OBDuino.
so i connect Canbus shield to Arduino and have trouble in receiving data from ECU simulator.
Tx and Rx light turn on. Don’t even blink. still on.

this is result of serial monitor when the code is executed.

CAN_BUS GET DATA!
CAN ID: 12
data len = 0
0 0 0 0 0 0 0 0

plz check my code.

[code]#include <SPI.h>
#include “mcp_can.h”

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
INT8U Flag_Recv = 0;

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

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

START_INIT:

if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
{
    Serial.println("CAN BUS Shield init ok!");
}
else
{
    Serial.println("CAN BUS Shield init fail");
    Serial.println("Init CAN BUS Shield again");
    delay(100);
    goto START_INIT;
}

}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void loop()
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x0c, 0, 8, stmp);
delay(100); // send data per 100ms

  CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
  unsigned char canId = CAN.getCanId();
  //Print data to the serial console 
  //and the LCD display
  Serial.println("CAN_BUS GET DATA!");
  Serial.print("CAN ID: ");
  Serial.println(canId);
  Serial.print("data len = ");
  Serial.println(len);
  //This loops through each byte of data and prints it
  
  for(int i = 0; i<len; i++)    // print the data
  {
      Serial.print(buf[i]);
      Serial.print("\t"); 
  }
      Serial.println();
      delay(50);

}[/code]

I am having a very similar problem. Have you found any solutions?

Hello,
I work on a project is: putting the 3 sensors; temperature sensor, current, inductive, with ARDUINO CARD+BUS CAN , I made the 3 programs Arduino language, now I want to know how to connect the sensor with the Arduino card + BUS and what is the program that allows to do that?
please someone ansewer me , i’m blocked .
thank you in advance.

I don’t know whether Tx and Rx LEDs should be on or off when no data is transmitted. So in that case I assume that it’s ok when they’re always on.

But I have other suggestions…

First of all, make sure that your bus speed (in your code 500kbp/s) is the right one and the shield is connected to the right wires (CAN-H and CAN-L).

What is a ECU-Simulator? Your code is sending data permanently. Is that the simulator?

Sending might be ok but you won’t receive your own message because the shield doesn’t run in loopback mode. You will only receive data from another CAN-Bus-Member that is connected to your shield. CAN-Bus is like a network. You need at least one other member in your network which your shield can “talk” to.

Reading data from the bus can be done with polling or interrupts. For the beginning you should use polling, because it’s easier to understand and there are less “traps” to worry about.

Here is my example for reading data:

void loop() {
  // Every loop you're polling for data.
  if (CAN.checkReceive() == CAN_MSGAVAIL) {
    // Read message and save id where it came from.
    // You can do it in one step by using readMsgBufID().
    unsigned long canId = 0;
    byte dataLen = 0;
    byte data[MAX_CHAR_IN_MESSAGE];  // Constant defined in CAN-Bus-Library.
    CAN.readMsgBufID(&canId, &dataLen, data);
    // Do what you want with received data.
    // ...
  }
}

As an extra, here’s a compact example for initializing your shield. :wink:

void setup() {
  while (true) {
    if (CAN.begin(CAN_33KBPS) == CAN_OK) break;
    delay(100);
  };
}

I hope I could help a bit. :slight_smile:

Mike

Hello ,
thank you for your answer , it helped me a lot,but i’m also blocked on Arduino - Sending data over a CAN bus
can you explain every single line on the message in the picture below
thank youu very much for ur help , i need it reaaally

I have this code that detects the temperature value with a temperature sensor (TMP36) of four bateries of the electric kart, as I have already explained, I have an Arduino shield card + BUS I’ll connect the sensor with the arduino card + CAN BUS, Then transfer all the information to the control system carried out by a NI MIYRIO available to us.

Can you tell me what is the program that I made is true