0
\$\begingroup\$

I tried to connect arduino uno R3 to STM32 using MCP2515 CAN bus transceiver. This was the transmitter code:


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



struct can_frame canMsg;
MCP2515 mcp2515(10);

void setup(){
  while (!Serial);
  Serial.begin(9600);
  SPI.begin(); //Begins SPI communication
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_16MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){
  
  canMsg.can_id = 0x036; //CAN id as 0x036
  canMsg.can_dlc = 8; //CAN data length as 8
  canMsg.data[0] = 0x04; //Update humidity value in [0]
  canMsg.data[1] = 0x03; //Update temperature value in [1]
  canMsg.data[2] = 0x02; //Rest all with 0
  canMsg.data[3] = 0x01;
  canMsg.data[4] = 0x00;
  canMsg.data[5] = 0x00;
  canMsg.data[6] = 0x00;
  canMsg.data[7] = 0x00;
  mcp2515.sendMessage(&canMsg); //Sends the CAN message
  Serial.println("Sent")  ;
  
  delay(1000);
}

And the receiver code was

#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

void setup() {
  SPI.begin();   //Begins SPI communication
  Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_16MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();  //Sets CAN at normal mode
}

void loop(){
  Serial.println(mcp2515.readMessage(&canMsg));
  delay(1000);
 if ((mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) && (canMsg.can_id == 0x036)){
     int x = canMsg.data[0];
     int y = canMsg.data[1];
     
     Serial.print(x);
     
     Serial.println(y);
     
     
   }
}

I found out that "Sent" was printed properly while the message was not received on the other end. I tried to print mcp2515.readMessage(&canMsg) which was found to be 5. What's going wrong? Some image of my setup: enter image description here

enter image description here

enter image description here

\$\endgroup\$
15
  • \$\begingroup\$ Where are the 120 Ohm termination resistors? \$\endgroup\$
    – Jeroen3
    Sep 1 at 18:47
  • \$\begingroup\$ OK, so what does the return value of 5 mean? If you have two devices on the bus that are otherwise isolated, don't you need a common ground reference between the devices? \$\endgroup\$
    – Justme
    Sep 1 at 18:48
  • \$\begingroup\$ @Justme We dont know what does 5 mean. Can u please elaborate the 2nd Q? \$\endgroup\$
    – Helix xxx
    Sep 1 at 18:55
  • \$\begingroup\$ @Jeroen3 the can transceiver has 120 ohm termination resistor \$\endgroup\$
    – Helix xxx
    Sep 1 at 18:56
  • 1
    \$\begingroup\$ Have you tried this ? protosupplies.com/product/mcp2515-can-bus-interface-module github.com/coryjfowler/MCP_CAN_lib \$\endgroup\$
    – Antonio51
    Sep 2 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.