Problem in communicating to obd via CAN bus

Hi,
I'm trying to communicate to OBD over CAN bus. I'm using MCP2551 and MCP2515 along with Arduino UNO. But I'm not getting any response from the ECU. I'm using seeedstudio library for CAN. I have made two identical circuits and tested the hardware using blink_receive and blink_send examples and they are working. I'm using sparkfun OBDII to DB9 cable for connecting to OBD port.
I have also tried the blink examples with the OBDII to DB9 cable, thinking that there might be some losses, but the examples are working on it. I have tried different BUS speeds too.
Below is the code that I'm using to communicate with ECU:

// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
#include <mcp_can_dfs.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 = 10;

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] = {0x02, 0x01, 0x00, 0, 0, 0, 0, 0};
void loop()
{
    // send data:  id = 0x00, standrad frame, data len = 8, stmp: data buf
    CAN.sendMsgBuf(0x7DF, 0, 8, stmp);
//    delay(100);                       // send data per 100ms
    unsigned char len = 0;
    unsigned char buf[8];
    unsigned long currTime = millis();
    while(millis() - currTime < 1000)
    {
//      Serial.print(CAN.checkReceive());
      if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
      {
          CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf

          unsigned char canId = CAN.getCanId();
        
          Serial.println("-----------------------------");
          Serial.println("get data from ID: ");
          Serial.println(canId, HEX);

          for(int i = 0; i<len; i++)    // print the data
          {
              Serial.print(buf[i]);
              Serial.print("\t");
          }
          Serial.println();
      }
    }
//    Serial.println("Done");
    delay(1000);
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

I have also attached the schematic for the reference. Any help would be appreciated. Thanks in advance :slight_smile:

What ECU are you trying to communicate with?

Is the Baud Rate set correctly?

Do you know that your "stmp" data will return something to you?

I am trying my circuit on Honda Jazz ECU.
I've tried the baud rate of 500, 250, 125 Kbps. When used with STN1110, the ECU return data correctly but I don't know at what baud rate its communicating, I tried looking it up, couldn't find it. If you have resources, please let me know.
Yeah, the stmp should return the data with supported PIDs by the ECU. I've used the format shown in OBDII wiki, and on various other resources, one being OBDII Can Bus - Cookbook | Mbed. This link is also using same format.

Thanks for the reply :slight_smile:

It turns out that I had checked everything 10 times except for the MCP2515 crystal. I was using 20Mhz crystal, damn, turn out that the libraries are for 16Mhz. So the baud rate was not as expected, changing the crystal does the job :slight_smile:

And my board has an 8MHz crystal. Changing setting to 250kb/s when running 125 works fine.

Seems like you far from result.

MCP_CAN CAN(10); just like that;

7df this your Id just letting you know. Your side...

This some of my design pls use it:

void loop()
{
       

                    stmp[0] = 0x02;
                    stmp[1] = 0x01;
                    stmp[2] = 0x0C;
        

                  //   or just like that :
                 //          stmp[0] = 0x2;
                //             stmp[1] = 0x1;
               //              stmp[2] = 0xC;

                    CAN.sendMsgBuf(0x7DF, 0, 8, stmp);
    
                     delay(100);
  

if(CAN_MSGAVAIL ==  CAN.checkReceive())
  {       
       int CanId = CAN.getCanId();
       CAN.readMsgBuf(&len, buf);
             
   
  
                         
                          Serial.print(CanId);
                          Serial.print(" ");

                          Serial.print(CanId,HEX);
                          Serial.print(" ");

                          for(int i=0; i<=8; i++)  {
                            
                                         if(buf[i]<=15)  {
                                         Serial.print("0");}
                                 
                                           
                               Serial.print (buf[i],HEX);
                               Serial.print(" ");
                                        
                          }
                          
                        Serial.println("  "); 
              
             }
    }          
 
}

}