Go Down

Topic: Arduino cannot read data from local CAN bus (Read 217 times) previous topic - next topic

mlewando

Jun 16, 2019, 11:27 pm Last Edit: Jun 18, 2019, 10:35 pm by mlewando
Hi all,

I'm trying to set up local CAN bus between two Arduinos (mega and pro micro).
For both boards, I'm using this CAN module: https://www.amazon.com/CHENBO-Electronics-Receiver-Controller-Development/dp/B015W4D9WY/ref=sr_1_3

I'm connecting the modules in this way:
ProMicro:
- VCC on module - VCC from micro
- GND on module - GND from micro
- CS on module -PIN 10 on board
- SO on module -PIN 14 on board
- SI on module - PIN 16 on board
- SCK on module - PIIN 15 on board

Mega 2560:
- VCC on module - 5V VCC from micro
- GND on module - GND from micro
- CS on module -PIN 53 on board
- SO on module -PIN 50 on board
- SI on module - PIN 51 on board
- SCK on module - PIN 52 on board

Both boards are connected to the PC and attached to the Serial monitor.
I have also a raspberry PI with this module: https://www.waveshare.com/rs485-can-hat.htm connected to it and used to sniff CAN communication.
On Arduino, I'm using this library: https://github.com/Seeed-Studio/CAN_BUS_Shield

The problem is, that no matter which Arduino I'd be using as sender/receiver, I can see CAN messages on raspberry but cannot see anything on the second Arduino.

On both arduinos modules, I have 120 oHm resistors applied, but I tried every combination of resistors without lack.

Here are the codes of minimal projects:
Sender:
Code: [Select]

#include <mcp_can.h>
#include <SPI.h>

const int SPI_CS_PIN = 53;

long unsigned int txID = 0xFF;
unsigned char stmp[8] = {0x0E, 0x00, 0xFF, 0x22, 0xE9, 0xFA, 0xDD, 0x51};

MCP_CAN CAN(SPI_CS_PIN);

void setup()
{
  Serial.begin(9600);

  while (CAN_OK != CAN.begin(CAN_250KBPS, MCP_8MHz))
  {
    Serial.println("CAN BUS Module Failed to Initialized");
    Serial.println("Retrying....");
    delay(200);
  }
  Serial.println("CAN BUS Shield init ok!");
}

void loop()
{
  Serial.println("In loop");
  CAN.sendMsgBuf(txID, 1, 8, stmp);
  delay(500);
}


Receiver:
Code: [Select]

#define DEBUG_EN 1
#include <SPI.h>
#include "mcp_can.h"

long unsigned int rxId;

unsigned char len = 0;
unsigned char buf[8];

const int SPI_CS_PIN = 10;

MCP_CAN CAN(SPI_CS_PIN);

void setup()
{
  Serial.begin(9600);
  delay(1000);

  while (CAN_OK != CAN.begin(CAN_250KBPS, MCP_8MHz)) // init can bus : baudrate = 500k
  {
    Serial.println("CAN BUS Module Failed to Initialized - ?");
    Serial.println("Retrying....");
    delay(200);
  }
  Serial.println("CAN BUS Module Initialized!");
  Serial.println("PGN\t\tByte0\tByte1\tByte2\tByte3\tByte4\tByte5\tByte6\tByte7");
}

void loop()
{
  if (CAN_MSGAVAIL == CAN.checkReceive())
  {
    CAN.readMsgBuf(&len, buf);
    rxId = CAN.getCanId();

    Serial.print("0x");
    Serial.print(rxId, HEX);
    Serial.print("\t");

    for (int i = 0; i < len; i++)
    {
      if (buf[i] > 15)
      {
        Serial.print("0x");
        Serial.print(buf[i], HEX);
      }
      else
      {
        Serial.print("0x0");
        Serial.print(buf[i], HEX);
      }
      Serial.print("\t");
    }
    Serial.println();
  }
}


I'd appreciate any help with figuring out why modules cannot read data from the bus but they can send the data.

UPDATE:

I managed to send commands to arduinos from raspberry pi via node using https://github.com/sebi2k1/node-can
Both arduinos can receive messages send from raspberry, raspberry can receive messages from both arduinos. The problem is that one arduino cannot see the message from the second one...

Go Up