Still can't figure out CAN bus

Hello everyone,

Long story short, I have been working on this little project for a couple of years now in the background, and still cannot figure out why it doesn't work. Made another attempt yesterday and still didn't work.

The project: I am trying to build an arduino nano CAN device (using the CAN modules available) and receive CAN messages from my battery management system (BMS). The BMS sends out CAN messages every 8 ms with data like battery current, voltage, state of charge, etc. in each byte. All of these messages can be customized and sent out with standard can IDs. I can view live CAN traffic in the BMS software and have verified that it's indeed sending out the correct can messages.

I used the basic receive code form the library, and set my baud rate to 500kbps, which is the baud of the BMS. I as hoping to just sniff the CAN bus and see the messages, then later I can filter out the info and try to compute later.

The goal is to use a nextion display and show data from the BMS via CAN bus. But still not sure why it isn't working. As far as hardware goes, I followed an instructable for my connections.

MCP2515_CAN Module Nano
VCC VCC
GND GND
CS D10
SO D12
SI D11
SCK D13
INT D2

I have made sure I have my two 120ohm termination resistors as well. All systems share a common ground too.

My code can be found below, thank you to everyone for their time, and any advice would be truly appreciated. I have just got to so many dead ends with this project, and exhausted all of the info I can find online, and still no progress. Thank you!

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();      
  }
}

So it's not printing anything I assume. Which implies that the read didn't return error_ok. Can you grab the return value and print it?

Also, do you have a scope to verify that there are CAN signals coming from the BMS as well as checking for them on the nano side.

wildbill:
So it's not printing anything I assume. Which implies that the read didn't return error_ok. Can you grab the return value and print it?

Also, do you have a scope to verify that there are CAN signals coming from the BMS as well as checking for them on the nano side.

Thanks so much for the reply!

It did not print anything that us correct. All it printed was:
" ------- CAN Read ----------;
ID DLC DATA"

Which is in the setup function anyway. But once in the loop it does not print anything.

I have a small cheap scope that I will check for on the CAN signals going into the CAN module, as well as signals coming from the BMS. I'll definitely do that for sure. I'm not sure what to look for exactly, but I'll do some googling.

It looks like the only difference between your sketch and the example from the library is the bitrate.

Perhaps you should try other bitrates? Maybe whatever told you that BMS requires 500KBPS is wrong.

johnwasser:
It looks like the only difference between your sketch and the example from the library is the bitrate.

Perhaps you should try other bitrates? Maybe whatever told you that BMS requires 500KBPS is wrong.

No that’s not the case unfortunately :frowning: I set the BMS baud rate to 500kbps

Going to test with the oscilloscope and see what I get here this evening and report back

wildbill:
So it's not printing anything I assume. Which implies that the read didn't return error_ok. Can you grab the return value and print it?

Also, do you have a scope to verify that there are CAN signals coming from the BMS as well as checking for them on the nano side.

Alright here is what I have found with the scope. I really don't know what to look for in regard to the waveform of a can bus signal. I did some googling to get an idea, and it seems correct. There's only 3 or so can messages from the bms that I see in the "live CAN traffic" on the bms software, so I don't expect this can bus to be as busy as a car's can network or anything.

Here is a photo of the scope. The first photo is the scope (+) connected to Can high, and scope (-) connected to GND that the BMS shares. The second photo is scope(+) connected to can low, and scope (-) connected to GND. I checked on the bms side of the bus and checked with the scope at the can module of the nano and it did indeed show the same general waveform. The max voltages read were also the same. So it appears that the can signal is correct, from what I can see.

Can high is the waveform that is over 3V max, and can low is the waveform that is around 2V max. (not sure which image will display in which order)

Anyone have any advice at this point?:frowning: ugh. Thanks for any advice and thank you all again for your time

Bump?:confused:

Any advice would be greatly appreciated

I did some playing with canbus, but gave up I worked out most of the bits that I needed but not all off them,

I used these to read the messages. I used the stuff in the list below and a tool called canHacker V2 and Can cool this displayed the messages. I used the seed studio shield along with the unoThere is also J1939 protocol for canbus.

Is it the standard protocol or the extended 29 bits of Data ?
Mine was the extended vesrion

J1939 canbus link
https://copperhilltech.com/a-brief-introduction-to-the-sae-j1939-protocol/

On there if you search you can find there demo stuff to read J1939 (that's if it is)

Try this library GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library and use the CAN_loopback sketch to verify that the SPI communication to the MCP2515 is working correctly.