MCP2515 Connections Failed

Submitted by Ulaş on

Hello, I am new to CAN Bus communications and I'm trying to set up the basic CAN communication circuit with an LDR sensor and LED instead of DHT sensor and LCD screen. I followed this tutorial:

https://circuitdigest.com/microcontroller-projects/arduino-can-tutorial…

 

When I connected everythig accordingly to here, I did not get any results. Here is my code for the transmitting Arduino Uno:

#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
#define LDRPIN 5
struct can_frame canMsg;
MCP2515 mcp2515(10);

void setup(){
  canMsg.data[1] = 0x00; //Rest all with 0
  canMsg.data[2] = 0x00; 
  canMsg.data[3] = 0x00;
  canMsg.data[4] = 0x00;
  canMsg.data[5] = 0x00;
  canMsg.data[6] = 0x00;
  canMsg.data[7] = 0x00;
  while (!Serial);
  Serial.begin(9600);
  SPI.begin(); //Begins SPI communication
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){  
  
  int h = digitalRead(LDRPIN); //Gets Humidity value
  canMsg.can_id = 0x056; //CAN id as 0x036
  canMsg.can_dlc = 8; //CAN data length as 8
  canMsg.data[0] = h; //Update brightness value in [0]
  Serial.print(canMsg.data[0]);
  
  mcp2515.sendMessage(&canMsg); //Sends the CAN message
  delay(1000);
}

 

And here is my code for the receiver Arduino Uno:

#include <SPI.h> //Library for using SPI Communication 
#include <mcp2515.h> //Library for using CAN Communication

struct can_frame canMsg; 
MCP2515 mcp2515(10); // SPI CS Pin 10 
int ledPin = 8;
void setup() {
  SPI.begin();   //Begins SPI communication
  Serial.begin(9600); //Begins Serial Communication at 9600 baud rate 
  mcp2515.reset();                          
  mcp2515.setBitrate(CAN_50KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 
  mcp2515.setNormalMode();  //Sets CAN at normal mode
}

void loop(){
 if ((mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) && (canMsg.can_id == 0x056)){

     int x = canMsg.data[0]; 
     if(x==1){
      digitalWrite(ledPin, HIGH);  // sets the LED to the button's value
     }
   }
}

 

I get the sensor data at transmitter side, although I can't receive anything at the transmitter side serial window. I controlled and realized canMsg is returned as nothing, so there is no communication, but I did not understand why. I would be glad if you could help.