Not receiving CAN data

Hello

I am working in a project where I am using Arduino UNO + MCP2515 to receive CAN messages that are coming from CAN high & CAN low of a sensor. CAN h & CAN L of the sensor are connected to MCP2515.

It's probably something simple, but I can't get a CAN bus filter working for CAN messages and even not receiving any CAN msg but 0 . The code I have written is compiling successfully. Problem is the CAN data received in the MCP2515 is not showing on the LCD or on Serial Monitor. Messages shown on Serial monitor : CAN check Not ok . Here is the code:

[code]
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <mcp2515.h>



LiquidCrystal_I2C lcd(0x27,16,2);
int y;
struct can_frame canMsg;
MCP2515 mcp2515(10);  //SPI chip select (CS) pin 10

void setup() {
//  lcd.init(); //initialize I2C LCD
  lcd.clear();
  lcd.backlight();
  //delay(1000);
  SPI.begin();
  Serial.begin(115200);  //begins baud rate at this rate
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  //mcp2515.setBitrate(MCP_8MHZ);
  mcp2515.setNormalMode();
  
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
  
}

void loop() {
  Serial.println("can check");
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)// to receive data
  
  {
    Serial.println("------- CAN Read ----------");
    Serial.print(canMsg.can_id); // print ID
    Serial.print(" ");
    
  Serial.println("ID  DLC   DATA");
    Serial.print(canMsg.can_dlc); // print DLC
    Serial.print(" ");

    if(canMsg.can_id==0x18FF02)
    {
      for(int i=0; i<canMsg.can_dlc; i++)  //print the data for id 18ff02
      {
        //Serial.print(canMsg.data[i],Hex);
        Serial.print(canMsg.data[6]);
         y=canMsg.data[6];
      }
    }
   
//    for (int i = 0; i<canMsg.can_dlc; i++)  
//    {  // print the data
//      Serial.print(canMsg.data[i],HEX);
//      Serial.print(" ");
//    }

    Serial.println();
    lcd.setCursor(1,0);
    lcd.print("CAN received...");
    lcd.setCursor(2,1);
    lcd.print(y);
    lcd.setCursor(2,2);
    Serial.print(canMsg.can_id);
    delay(200);     
 }
else
Serial.println("not ok"); 
}
[/code]

Please help me to get these CAN messages of the sensor on screen as well as serial monitor and filtering out the particular CAN ID from all messages.

This,

is very helpful.

I have searched the problem.. tried multiple codes then only posted my question. This code is giving zero in data and ID for my set up.

And post some images of your project and post a schematic, please.

Thankyou for the guidance.

  • This is the complete set up with Arduino UNO , MCP2515 & Sensor for CAN data.

  • This is how connections are made between MCP2515 & Arduino


The circuit shows a direct connection to a sensor, right?

I’m not sure here but if you are connected to a sensor , that has a can bus output , it will need 12v (5v?) and 0v power for the sensor to
Work which I can’t see.

Not sure what the sensor is , but unusual for a sensor itself to have a can connection .

Yes to MCP I have connected the CAN high and low directly. I have checked , getting signals at CAN bus from the sensor.

In order to directly control the sensor you are going to have to get the sensors data sheet and program the sensor to run.

I use the automobiles can bus to read can data. The MCU of the car does the sensor setup.

Good luck.

Thankyou for your support.

I have connected 12V battery to the sensor setup

I don't intent to control the sensor, just need a CAN ID data present at 18FF02 ID's 6th byte.
I have the data sheet of the sensor with me. Sensor is behaving properly. On connecting the CAN high and low and passing this data through mcp , I intend to filter out this CAN ID (18FF02) and specifically 6th byte of it . Plan to pass this byte to some variable.

"can bus termination resistor" <<< words to use in a search engine that may help you directly interface with a CAN bus sensor. Otherwise, I've got nothing else for you.

First wanted to check what CAN IDs I am getting on serial monitor. Then filter out the ID which has data that I need. But the data and IDs are not displaying by the code that I have shared or by the code that @Idahowalker has suggested. Getting ID 00 and Data (blank)

okay Thankyou.

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