CAN bus Arduino Nano 33 BLE

Hello!

I'm currently working on a project that involves using an Arduino Nano 33 BLE Sense to send a CAN bus signal. For this task, I'm utilizing an MCP2515 module to manage the CAN bus communication. I've integrated the following library into my project: GitHub - 107-systems/107-Arduino-MCP2515: Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames..

However, I'm encountering difficulties in successfully sending a CAN bus signal. I've provided my current code and hardware setup below for reference. It's important to note that implementing BLE functionality will come later; my primary focus at the moment is resolving the CAN bus signal issue.

Additionally, I've soldered the pads on the BLE to produce 5V. Any guidance or insights on what might be causing the problem with the CAN bus signal would be greatly appreciated. Thanks!

#include <SPI.h>
#include <ArduinoMCP2515.h>

static int const MKRCAN_MCP2515_CS_PIN = 10;
static int const MKRCAN_MCP2515_INT_PIN = 2;

//static SPISettings const MCP2515x_SPI_SETTING{10000000, MSBFIRST, SPI_MODE0};
void onReceiveBufferFull(uint32_t const timestamp_us, uint32_t const id, uint8_t const * data, uint8_t const len)
{
  Serial.println(id, HEX);
}

ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
                       []() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
                       [](uint8_t const d) { return SPI.transfer(d); },
                       micros,
                       onReceiveBufferFull,
                       nullptr);
void setup()
{
  Serial.begin(9600);
  while (!Serial)
  {
    Serial.println("Waiting for serial connection...");
    delay(100);
  }

  Serial.println("Serial connection established!");

  // Setup SPI access
  SPI.begin();
  //SPI.beginTransaction(MCP2515x_SPI_SETTING);
  pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
  digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
  attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), [](){ mcp2515.onExternalEventHandler(); }, FALLING);


  // Initialize MCP2515
  mcp2515.begin();
 
  mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ);
  
  mcp2515.setNormalMode();                           // Set MCP2515 to normal mode

}

void loop()
{
    // Transmit a test frame
  uint8_t data[4] = {0xCA, 0xFE, 0xBA, 0xBE};
  if (mcp2515.transmit(0x123, data, 4))                     // or mcp2515.sendMsgBuf(0x123, 0, 4, data)
  {
    Serial.println("Message transmitted successfully!");
    
  }
  else
  {
    Serial.println("Error transmitting message!");
  }

  delay(100);
}

Now I tried a different approach with the following code, but it still does not work. One thing I noticed is that it still print out my message even when I have set two different baud rates for Serial begin and on the monitor?

Any insight or help would be useful, thank you!

#include <SPI.h>
#include <107-Arduino-MCP2515.h>
#undef max
#undef min
#include <algorithm>


//static const uint8_t SS = PIN_SPI_SS;
//static int const MKRCAN_MCP2515_INT_PIN = 7;


static SPISettings const MCP2515x_SPI_SETTING{10000000, MSBFIRST, SPI_MODE0};
//void onReceiveBufferFull(uint32_t const timestamp_us, uint32_t const id, uint8_t const * data, uint8_t const len)
//{
//  Serial.println(id, HEX);
//}

ArduinoMCP2515 mcp2515([]() { digitalWrite(SS, LOW); },
                       []() { digitalWrite(SS, HIGH); },
                       [](uint8_t const d) { return SPI.transfer(d); },
                       micros,
                       nullptr,
                       nullptr);
void setup()
{
  Serial.begin(115200);
  while (!Serial)
  {
    Serial.println("Waiting for serial connection...");
    delay(100);
  }


  Serial.println("Serial connection established!");
  

  // Setup SPI access
  SPI.begin();
  SPI.beginTransaction(MCP2515x_SPI_SETTING);
  pinMode(SS, OUTPUT);

  digitalWrite(SS, HIGH);
  



  // Initialize MCP2515
  mcp2515.begin();
 
  mcp2515.setBitRate(CanBitRate::BR_500kBPS_16MHZ); // Set the CAN bit rate
  
  Serial.println("MCP2515 initialized successfully!");
  
  mcp2515.setNormalMode();                           // Set MCP2515 to normal mode


}
/*uint8_t data[4] = {0xCA, 0xFE, 0xBA, 0xBE};
uint32_t messageId = 0x123;
uint8_t dataLength = sizeof(data); // Get the data length
*/
typedef struct
{
  uint32_t id;
  uint8_t  data[8];
  uint8_t  len;
} sCanTestFrame;
static sCanTestFrame const test_frame_1 = { 0x001, {0x01}, 1 };                                              /* Minimum (no) payload */
static sCanTestFrame const test_frame_2 = { 0x002, {0xCA, 0xFE, 0xCA, 0xFE, 0, 0, 0, 0}, 4 };             /* Between minimum and maximum payload */
static sCanTestFrame const test_frame_3 = { 0x003, {0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE}, 8 }; /* Maximum payload */
static sCanTestFrame const test_frame_4 = { 0x404, {0x2}, 2 };                                              /* RTR frame */
static sCanTestFrame const test_frame_5 = { 0x007, {0x3}, 3 };                                              /* Highest standard 11 bit CAN address */
static sCanTestFrame const test_frame_6 = { 0x980, {0x3}, 3 };                                              /* Lowest extended 29 bit CAN address */
static sCanTestFrame const test_frame_7 = { 0x911, {0x4}, 4 };   

static std::array<sCanTestFrame, 7> const CAN_TEST_FRAME_ARRAY =
{
  test_frame_1,
  test_frame_2,
  test_frame_3,
  test_frame_4,
  test_frame_5,
  test_frame_6,
  test_frame_7
};

void loop() {
  // Transmit a test frame

  std::for_each(CAN_TEST_FRAME_ARRAY.cbegin(),
                CAN_TEST_FRAME_ARRAY.cend(),
                [](sCanTestFrame const frame)
                {
                  Serial.print("Transmitting frame with ID 0x");
                  Serial.print(frame.id, HEX);
                  Serial.print(", Data: ");
                  for (int i = 0; i < frame.len; ++i) {
                    Serial.print(frame.data[i], HEX);
                    Serial.print(" ");
                  }

                  if(mcp2515.transmit(frame.id, frame.data, frame.len)) {
                    Serial.println(" - Success");
                  } else {
                    Serial.println(" - Error");
                  }

                  delay(1000);
                });
}

What CAN module are you using?
Does it have a 16Mhz or 8 Mhz crystal installed?

Hi! Thanks for answering. Im curretly using MCP2515 moudle that uses 16Mhz

Output pins from the MCP2515 need 5v to 3.3v level translators.

I have made a a solder bridge and use USB to power the Arduino ble so I do get 5V output voltage to the MCP2515

The Nano 33 BLE is 3.3v device and is not designed to handle the 5v signals from the MCP2515.

Even when i used a dc-dc boost converter it did not work

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