Go Down

Topic: Can not receive CAN DATA from Arduino UNO with MCP2515 Module. (Read 25599 times) previous topic - next topic

coryjfowler

I use the Nirem CAN interface.
This thing? https://wiki.kewl.org/dokuwiki/_detail/interfaces:niren.jpg?id=interfaces%3Aniren

You would be somewhere around the third individual to have no luck with those boards and unless you have access to an actual CAN interface dongle and tools(Peak, Kvaser, Vector, etc) troubleshooting will be difficult.  I cannot recommend that CAN interface board.
"Taking the time to make a proper, punctuated, post is a mark of courtesy and respect."  http://forum.arduino.cc/index.php?topic=149022.0

Christer_A

I have Arduino UNO and Mega2560 with CAN bus shields from SEEED and Sparcfun and everything works fine.
These shields use a 16MHz Q.
Tested against each other RX/TX, Keysight Oscilloscope with CAN bus capability, against Garmin nmea2000 marine network and finally against Märklin digital toy train ! (Märklin uses CAN bus also...)
Eveything works perfectly!

I now use the CAN library from Cory Fowler with support also for 8 MHz.
Now I also have tried the NiRem Canbus board from China with a 8 MHz Q.
Tested 4 pieces and it simply does not work (only modification in code is 8MHz).

I think the problem is in these boards/PCB. I have run out of ideas to test more.

Christer_A

Regarding CAN library I am a bit confused.

In early 2016 I used the library from SEEED studio. This worked fine for my experiments.
However there was no support for other than 16 MHz Q.

Then in 2016 I used The library from Cory Fowler, and also changed some of my code. In this library there are support also for 8 MHz Q (and other).
I also noticed not to use   "CAN.getCanId();  ".

Now I see a new library from Adlerweb and still the use of   "CAN.getCanId();


As I am new to this I would appreciate some clarifications.

And what library to use.



coryjfowler

Regarding CAN library I am a bit confused. [...]
I also noticed not to use   "CAN.getCanId();  ".

Now I see a new library from Adlerweb and still the use of   "CAN.getCanId();
I removed it because I gave the CAN receive function ("CAN0.readMsgBuf(&rxID, &len, rxBuf)") the ability get that information into the user's "sketch" in one function call instead of two.

I would go to assume Adlerweb forked his version of the library before my changes we made.  At one point my change made it to Seeed's library, but they added it back for whatever reason.

As I am new to this I would appreciate some clarifications.

And what library to use.

You're welcome to use any library you want, but the sweet thing is, if you use mine I am more willing to help you when a problem occurs.  For instance, if you want to send me a couple of those boards you're not having luck with, I can take a look at them and see what I can come up with.

I actually have two of those boards I linked to in my previous post on this thread working, but I've not had near as many issues as I've encountered from the community which makes me feel like a counterfeit part is ruining an otherwise nice product.

PM me if you're interested.
"Taking the time to make a proper, punctuated, post is a mark of courtesy and respect."  http://forum.arduino.cc/index.php?topic=149022.0

Christer_A

Using the Cory Fowler library.
I forgot to say that the 8MHz "NiRem" board Initialize correct.
But there is nothing transmitted on the CAN bus.

Today I tested 3 other CAN shields. The 1.2 version from SEEED, Sparcfun CAN bus shild and also a shield from ElecFreaks in China. All three with the same formfactor and using 16 MHz Q.
Tested these shields tested against different Arduini UNO and MEGA 2560.

Everything works fine.

Checked against Garmin GPS plotter with nmea2000 network and also tested against a Microchip CAN development box.

It is only the NiRem boards that will not work !
 

coryjfowler

Using the Cory Fowler library.
I forgot to say that the 8MHz "NiRem" board Initialize correct.
But there is nothing transmitted on the CAN bus.
Correct.  I have seen this from others before you.  I purchased a few of those boards a year ago and they did work for me.  I do not know what is going on with the other boards until I get my hands on some, but I suspect a bad, possibly counterfeit, part.

Today I tested 3 other CAN shields. The 1.2 version from SEEED, Sparcfun CAN bus shild and also a shield from ElecFreaks in China. All three with the same formfactor and using 16 MHz Q.
Tested these shields tested against different Arduini UNO and MEGA 2560.

Everything works fine.
That's great!  Glad those are working fine.

Checked against Garmin GPS plotter with nmea2000 network and also tested against a Microchip CAN development box.
Oh thats nice... I need to borrow one of those so I can figure out the plotter turns GLONASS on in the GPS I have.  They will not give me any information on their NMEA 2K GPS, its pretty silly.  I have a sketch decoding the output of the GPS otherwise.

It is only the NiRem boards that will not work !
I'm as baffled as you are until I can investigate further.
"Taking the time to make a proper, punctuated, post is a mark of courtesy and respect."  http://forum.arduino.cc/index.php?topic=149022.0

Christer_A

Problem solved !
Thanks Cory Fowler for support.
The main problem was some bad cables between the Arduino and the NiRem board !
Stupid problem.  I tried several boards but I did not change the cables.

When digging into this I could not resist to build my own CAN BUS "shield" on a breadboard.
Jpg attached. The local electronics shop here in my town stores MCP2515, MCP2551 and also 8 and 16 MHz Quarz. I did not expect this. This breadboard shield worked directly, and was very stable.
Easy to change between 8 and 16 MHz Quarz.
I also added two LEDs with resistors for TXD and RXD so I could get indication of something transmitted and received.

I now have 3 NiRem boards here and 2 of them works perfectly. The third one is not stable. When moving my finger on the PCB it could stop working or start working.

rrdesign

i am using the elekfreaks shield with Arduino UNO and SparkFun Can Library. Everything's working fine. I tried to use coryjfowler's can Lib because i need dual can support. I connected 2 shield to Arduino, one shield with INT 2 and CS 10 and the other with INT 3 and CS 9. both CAN: Init OK Baudrate Successful and Configuration Mode Successful. i can see data incoming. The RX Led is blinking but nothing is being printed on the serial console. I tried the receive example with one shield attached only. But same problem. Everything is been initialized ok but i am not receiving anything. The shield is running on 16 Mhz. With the same setup i can start the receive example script from spark fun library and its working fine. (without changing any wires)  what could be the problem?

Code: [Select]
// Demo: Dual CAN-BUS Shields, Data Pass-through
// Written by: Cory J. Fowler
// January 31st 2014
// This examples the ability of this library to support more than one MCP2515 based CAN interface.


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

unsigned long rxId;
byte len;
byte rxBuf[8];

byte txBuf0[] = {};
byte txBuf1[] = {};

MCP_CAN CAN0(10);                              // CAN0 interface usins CS on digital pin 10
MCP_CAN CAN1(9);                               // CAN1 interface using CS on digital pin 9

void setup()
{
  Serial.begin(115200);
 
  // init CAN0 bus, baudrate: 250k@16MHz
  if(CAN0.begin(MCP_ANY, CAN_100KBPS, MCP_16MHZ) == CAN_OK){
  Serial.print("CAN0: Init OK!\r\n");
  CAN0.setMode(MCP_NORMAL);
   pinMode(2, INPUT); 
  } else Serial.print("CAN0: Init Fail!!!\r\n");
 
  // init CAN1 bus, baudrate: 250k@16MHz
  if(CAN1.begin(MCP_ANY, CAN_100KBPS, MCP_16MHZ) == CAN_OK){
  Serial.print("CAN1: Init OK!\r\n");
  CAN1.setMode(MCP_NORMAL);
   pinMode(3, INPUT); 
  } else Serial.print("CAN1: Init Fail!!!\r\n");
 
  SPI.setClockDivider(SPI_CLOCK_DIV2);         // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)
}

void loop(){ 
  if(!digitalRead(2)){                         // If pin 2 is low, read CAN0 receive buffer
    CAN0.readMsgBuf(&rxId, &len, rxBuf);       // Read data: len = data length, buf = data byte(s)
    if(rxId == 0x130)
    {
      Serial.println("0x130 empfangen");
      byte rxBuf[5] = {0x45,0x41,0x21,0x8F,0x57};
      CAN1.sendMsgBuf(rxId, 0, len, rxBuf);
    }
    else       
      CAN1.sendMsgBuf(rxId, 0, len, rxBuf);      // Immediately send message out CAN1 interface
  } 
  if(!digitalRead(3)){                         // If pin 3 is low, read CAN1 receive buffer
    CAN1.readMsgBuf(&rxId, &len, rxBuf);       // Read data: len = data length, buf = data byte(s)
    Serial.print("ID: ");
    Serial.print(rxId,HEX);
    Serial.print(", ");
    Serial.print("Data: ");
    for(int i=0;i<len;i++)
    {
      Serial.print(rxBuf[i],HEX);
      Serial.print(" ");
    }
    Serial.println("");
  }
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

kenzcode

Having just about wasted the whole day trying to get a simple send/receive working, using two Nano v3.0s and two NiRen MCP2515 modules. (Using Corys wonderfull library with crystal set to 8Mhz)

The problem turned out to be that Nano V3.0 powered from it's USB port does not provide enough voltage to allow the NiRen modules to function correctly....

They look like they're working and initialise OK but sending fails and nothing is received...

Power every thing from a separate 5v power supply and everything works as expected :-)

Hope this saves someone a bit of time!

Cheers
Ken


Malaga

Hi,

I have meet the same problem.

I use Arduino Uno and Niren MCP2515(8 Mhz) Modul, I also use a CAN Generator that creates CAN BUS signals.

MCP_CAN_LIB source files: https://github.com/Seeed-Studio/CAN_BUS_Shield

And the code is very simple with send and receive functions just as the Library.

BUT the digitalRead(CAN_INT) ist always HIGH and I can receive nothing. I habe tried all the receive code examples in the library and no one works.

Has anyone got this problem? What can I do to solve it?
I wasted fast a week.

Code: [Select]
// CAN Send Example
//
#include <SPI.h>
#include <avr/wdt.h>


#include "mcp_can.h"
#include "Arduino.h"

//initial all parametrs
const int CAN_INT=2;
const int SPI_CS_PIN=10;

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56};


MCP_CAN CAN(SPI_CS_PIN);     // Set CS to pin 10


void setup()
{
    // Pins definieren
  pinMode(SPI_CS_PIN, OUTPUT);
  pinMode(CAN_INT, INPUT); // CAN_INT set to 2
 
  Serial.begin(115200);
  Serial.print("Starting CAN-Bus: ");
  // Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
 while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ))    // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println(" Init CAN BUS Shield again");
        delay(100);
    }
    Serial.println("CAN BUS Shield init ok!");

    /*
     * set mask, set both the mask to 0x3ff
     */
    CAN.init_Mask(0, 0, 0x000);                         // there are 2 mask in mcp2515, you need to set both of them
    CAN.init_Mask(1, 0, 0xfff);


    /*
     * set filter, we can receive id from 0x04 ~ 0x09
     */
    CAN.init_Filt(0, 0, 0x04);                          // there are 6 filter in mcp2515
    CAN.init_Filt(1, 0, 0x05);                          // there are 6 filter in mcp2515

    CAN.init_Filt(2, 0, 0x06);                          // there are 6 filter in mcp2515
    CAN.init_Filt(3, 0, 0x07);                          // there are 6 filter in mcp2515
    CAN.init_Filt(4, 0, 0x08);                          // there are 6 filter in mcp2515
    CAN.init_Filt(5, 0, 0x09);                          // there are 6 filter in mcp2515
   
   CAN.setMode(MCP_NORMAL);
}

void loop()
{
    Serial.println("Start receiving Data:");
   
    if(!digitalRead(CAN_INT))
  {
    CAN.readMsgBuf(&rxId, &len, rxBuf);   // read data,  len: data length, buf: data buf
    Serial.print("rxID: ");
    Serial.println(rxId);
    Serial.print("Readdetails:   ");
    for(int i=0;i<sizeof(rxBuf);i++)
    {
      Serial.print("0x");
      Serial.print(rxBuf[i],HEX);
      Serial.print("  ");
    }
  }
  else
  {
    Serial.println("Error receiving Message...");
  }
    Serial.println();
   
    Serial.println("Start sending Data:");
    CAN.sendMsgBuf(0x100, 0, 8, data);
    delay(10000);
}

ChrisBarker

I too have been having problems sending and receiving data using 8MHZ MCP2515 boards connected to two arduinos and using Mr Fowler's library and example sketches. The loop back sketch works fine on both arduinos but the send sketch initialises but returns an error on sending data and therefore nothing gets through to the receive sketch. I've tried all the suggestions given in previous posts, swapped over boards, checked and rechecked the wiring all to no avail.
Yesterday, having ordered some 16mhz boards, I was checking through the library files and noticed that the 8MHZ constant in the keywords file was spelt with a lowercase z, i.e 8MHz. Everywhere else it was always MHZ.  Didn't think it would matter as I would have expected a compilation error still it was a "straw" so I changed it to an uppercase Z, recompiled the sketches and WOW, my serial monitors were flooded with successful send and receive messages.

Mbuchanan

I was wondering if anyone has been able to get this to read data back? I have checked what I am sending and it matches the code I am intending to send. I have verified that the ecu is responding back using Vector, however I only getting message successfully sent, no data

turbozob

So guys saying on these Nirem boards 8MHz Q should be replaced with 16MHz Q and then it should work?
I can get the communication running between two arduino uno's but not with the car. Thanks for reply

noiasca

you should just use the proper initializer - either with 8MHz or 16MHz depending on your module.

while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_250KBPS, MCP_16MHZ)) 

vs

while (CAN_OK != CAN.begin(MCP_STDEXT, CAN_250KBPS, MCP_8MHZ)) 

if you do it right - it should work
DE: Wie man Fragen postet:
1. was hat man (Sketch und Hardware)
2. was SOLL es machen
3. was macht es: IST (Fehlverhalten, Fehlermeldungen, Serial.Output ...)
4. Eine Frage stellen bzw. erklären was man erwartet

turbozob

Sorry for late reply, forgot to subscribe to topic. I used MCP_8MHZ but nothing comes from the car, guess I am doing something else wrong. Has anyone successfully connected with the car over OBD2 with this Nirem board?

Go Up