I've been struggling with the Arduino MKR CAN shield and still cannot make it work.
I will be appreciated if anyone could show me a library or some sample codes which works on the MKR CAN shield.
I am developing a testing circuit to send the data from Arduino MKR ZERO to Arduino UNO through the CAN bus. The MKR ZERO is connected with the MKR CAN shield and the UNO is connected with the MCP2515 CAN module.
I first tried to use the CAN library (GitHub - sandeepmistry/arduino-CAN: An Arduino library for sending and receiving data using CAN bus.) suggested by the MKR tutorial, but I failed to receive anything from the UNO.
Then I tried the modified mcp2515 library (GitHub - milesfrankland/arduino-mcp2515: Arduino MCP2515 CAN interface library). The UNO received messages but the address and data were totally wrong:
(Here are the sample codes provided by the library, I change the CS for MKR to the correct pin)
The code for the MKR ZERO (Sender) was:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(3); // cs for MKR ZERO: pin 3
void setup() {
canMsg1.can_id = 0x0F6;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x8E;
canMsg1.data[1] = 0x87;
canMsg1.data[2] = 0x32;
canMsg1.data[3] = 0xFA;
canMsg1.data[4] = 0x26;
canMsg1.data[5] = 0x8E;
canMsg1.data[6] = 0xBE;
canMsg1.data[7] = 0x86;
canMsg2.can_id = 0x036;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x0E;
canMsg2.data[1] = 0x00;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x08;
canMsg2.data[4] = 0x01;
canMsg2.data[5] = 0x00;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0xA0;
while (!Serial);
Serial.begin(115200);
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
Serial.println("Example: Write to CAN");
}
void loop() {
mcp2515.sendMessage(&canMsg1);
mcp2515.sendMessage(&canMsg2);
Serial.println("Messages sent");
delay(1000);
}
And the code for UNO (Receiver) was:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
void setup() {
Serial.begin(115200);
mcp2515.begin();
mcp2515.setBitrate(CAN_125KBPS);
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();
}
}
What I expected to receive from the UNO should be 8 digit data in addresses 0x0F6 and 0x036, but what I actually received were:
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
40000310 5 F0 89 A4 DF AA
I tried to use the UNO as the sender and MKR ZERO as the receiver but I could receive nothing. I've replaced the MKR CAN shield in case of the hardware issue but it couldn't help.
I also wondering if there are any issues in the pin selection, but I failed to manually change the pin allocation with
SPI.setMOSI (MCP2515_SI) ;
SPI.setMISO (MCP2515_SO) ;
SPI.setSCK (MCP2515_SCK) ;
because "class SPIClass' has no member named 'setMOSI'".