Can communication with can spi module

I tried CAN communication between two arduino unos using the CAN SPI MCP2515 modules but its seemingly very unreliable as it only worked once

This was the code i used, any idea what could be wrong?
thanks!!

transmitter:

#include<SPI.h>

#include <can.h>
#include <mcp2515.h>

struct can_frame canMsg1;
MCP2515 mcp2515(10);

void setup(){
canMsg1.can_id = 0x037;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 1;
canMsg1.data[1] = 2;
canMsg1.data[2] = 3;
canMsg1.data[3] = 4;
canMsg1.data[4] = 5;
canMsg1.data[5] = 6;
canMsg1.data[6] = 7;
canMsg1.data[7] = 8;

Serial.begin(9600);

mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
mcp2515.setNormalMode();

Serial.println("Example: Write to CAN");

}

void loop(){

mcp2515.sendMessage(&canMsg1);
Serial.println("Messages sent");

delay(1000);

}

receiver:
#include<SPI.h>

#include <can.h>
#include <mcp2515.h>

struct can_frame canMsg1;
MCP2515 mcp2515(10);

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

mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
mcp2515.setNormalMode();

Serial.println("-----CAN Read-----");

}

void loop() {
// put your main code here, to run repeatedly:
if(mcp2515.readMessage(&canMsg1) == MCP2515::ERROR_OK){

Serial.print("ID: ");
Serial.print(canMsg1.can_id, HEX);
Serial.println(" ");
Serial.print("DLC: ");
Serial.print(canMsg1.can_dlc);
Serial.println(" ");
Serial.print("DATA: ");

for(int i =0; i< canMsg1.can_dlc; i++){
Serial.print(canMsg1.data[i]);
Serial.print(" ");
}

Serial.println();

}
}

Hi, @ananya24_7
Welcome to the forum.

How do you mean worked once!
The code only cycled once?
OR
The code only performed once and now will not work at all?

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post your dose in code tags as shown in the above instructions?

Have you got gnd as well as CANHI and CANLOW connected between the modules?
Can you please post a circuit diagram of your connection.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Are you monitoring the serial output from the sender, the receiver, or both?

Does the sender print "Messages sent" once or many times?

Welcome to the forum.

Can you please confirm whether you understand the CAN basics?

You need a bus with 120Ohm termination resistor on each of the two ends. If you have two modules with resistors make sure they are enabled. Many modules have jumpers for that. If you have more modules make sure only two resistors are enabled.

You always need two CAN modules. One module cannot send a message successfully into the void. You need at least two modules and they need to be set to the exact same CAN bit rate. A module with a different bit rate will create errors and stop after a while until the bus is quiet.
A single module will repeat a message continuously because the send will never be successful.

Do you have an oscilloscope to look at the signal on the bus? Any cheap, old analog oscilloscope will do for CAN.

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