Need Help with Arduino Mega 2560 with Can bus not receiving message

Im trying to communicate 1 Arduino UNO R4 Wifi as receiver. and 1 Arduino Uno as sender and 1 Arduino Mega 2560 as sender 2. The communication between this device is through CAN Bus (MCP2515). But from serial monitor of R4 Wifi the Receiver is only receive data from Uno the sender 1 and no data form Mega sender 2 as picture below

The circuit connection as below.

Code for R4 Wifi (Receiver)

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

struct can_frame canMessage;
MCP2515 mcp2515(10);

void setup() {
  Serial.begin(115200);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();
}

void loop() {
  if (mcp2515.readMessage(&canMessage) == MCP2515::ERROR_OK) {
    Serial.println("CAN_ID" + String(canMessage.can_id) + ":" + String(canMessage.data[0])); // Print CAN ID and CAN data
  }
}

Code for Uno Sender 1

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

struct can_frame canMessage;
MCP2515 mcp2515(10);

void setup() {
  while (!Serial);
  Serial.begin(115200);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();
}

void loop() {
  int testData = random(1,99); // Generate random numbers (from 1 to 99) to simulate sensor data

  canMessage.can_id  = 0x001; // ID must be unique in a single CAN Bus network.
  canMessage.can_dlc = 1; // Data Length Code
  canMessage.data[0] = testData;
  mcp2515.sendMessage(&canMessage); // Send CAN messaage

  delay(200);
}

Code for Mega 2560 Sender 2

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

struct can_frame canMessage;
MCP2515 mcp2515(10);

void setup() {
  while (!Serial);
  Serial.begin(115200);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();
}

void loop() {
  int testData = random(1,99); // Generate random numbers (from 1 to 99) to simulate sensor data

  canMessage.can_id  = 0x002; // ID must be unique in a single CAN Bus network.
  canMessage.can_dlc = 1; // Data Length Code
  canMessage.data[0] = testData;
  mcp2515.sendMessage(&canMessage); // Send CAN messaage

  delay(200);
}

you did not update the CS pin number in sender2 code (should be 53 according to your wiring :wink: )

hope that helps....

PS: Arduino UNO R4 has builtin CAN so it does not really need to be wired up to a MCP2515 board if using only one CAN bus on this board ! :upside_down_face:

1 Like

this fix the issues. Thank you very much !

Make sure you change MCP2515 mcp2515(10); to MCP2515 mcp2515(53); for the MAGA

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