Arduino Forum

Topics => Home Automation and Networked Objects => Topic started by: JJ_Table_Turner on Mar 04, 2013, 11:39 am

Title: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: JJ_Table_Turner on Mar 04, 2013, 11:39 am
I've been playing around with CAN BUS, with a view to using it for home automation, mainly because it seems a really cheap way of off-loading communications to another device, freeing up the Arduino to do whatever in the meantime. The main problem I've had is the shortage of documentation and simple beginners sketches to start off with. I built two breadboard copies of the Seeeduino CAN-BUS Shield (http://www.seeedstudio.com/wiki/CAN-BUS_Shield (http://www.seeedstudio.com/wiki/CAN-BUS_Shield)) using two Nano Arduinos. CS is shown as selectable, but I used D10 and it works fine, 16MHz crystal with 2x22pf capacitors and 2x 0.1uF capacitors, one across each chip's supply as decoupling, and a random 330 Ohm resistor I had laying around as CAN-BUS terminator!

The Seeduino CAN-BUS Shield library does not work properly, and neither do the examples! Or at least I couldn't get them working initially in Arduino IDE 1.0.1. Fortunately the debugging in the compile window at the bottom of the Arduino IDE is pretty good, and I found that this was enough to help me correct the odd mistakes made there. I had to re-write the Receive example, as the contents of the original seem intended to do something completely different!

I hope this helps someone; it's far from being a complete network, but from reading the MCP2515 datasheet it looks relatively straightforward to implement Masks and Filters. If you can't be bothered to do that you could use the Arduino to do the filtering instead and just use the cheap hardware to do the receiving, buffering and CRC etc.

Seeduino CAN-BUS Shield clone Send.ino
Code: [Select]
#include "mcp_can.h"
#include <SPI.h>

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup()
{
  delay(5000);  // delay to allow me time to open the serial monitor window to check all is well
  Serial.begin(9600);
  if(CAN.begin(CAN_500KBPS) ==CAN_OK)  //check the Seeeduino CAN-BUS wiki for more details on code
  {
    Serial.print("can init ok!!\r\n");
      for (int i = 0; i < 9; i = i + 1)
      {
        Serial.print(stmp[i], DEC);
      }
  }
  else Serial.print("Can init fail!!\r\n");
}

void loop()
{
  CAN.sendMsgBuf(0x00, 0, 8, stmp);
  delay(1000);  //
}


Seeduino CAN-BUS Shield clone Receive.ino
Code: [Select]
#include "mcp_can.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char

unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];

void setup()
{
  delay(5000);
  Serial.begin(9600);
  if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
  else Serial.print("Can init fail!!\r\n");
  attachInterrupt(0, MCP2515_ISR, FALLING);  // interrupt indicates when message is available
}

void MCP2515_ISR()
{
     Flag_Recv = 1;
}

void loop()
{
    if(Flag_Recv)
    {
      Flag_Recv = 0;
      CAN.readMsgBuf(&len, buf);
      Serial.print("CAN message length = ");
      Serial.print(len, DEC);
      Serial.print(" CAN message = ");
      for (int i = 0; i < 8; i = i + 1)
      {
        Serial.print(buf[i], DEC);
      }
      Serial.println();
    }
}


If anyone else wants to try this and has problems with Seeduino CAN-BUS library, I post those as well; I just can't remember exactly what I had to change!

Cheers, JJ
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: giants_fan3 on Apr 03, 2013, 12:37 pm
Thank you for this! I'm getting started on CAN.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: gmazza on Sep 05, 2013, 04:05 pm
Hello,
i've bought a Seedstudio CAN Bus shield for Arduino and i want to read a load cell with it. I tried to wire two Arduino with CAN BUS shield and i uploaded the example "send" in the first one and the "receive" example in the second one. I modified these sketches because they didn't worked, how explained above. The correct sketches are attached at this post. The two Arduino communicate without problems and the LEDs configuration is this:
- SEND: RX and TX flash
- RECEIVE: RX and INT flash
Then I tried to connect the Arduino that receive with my load cell. In this case i don't receive anything and the LED configuration is this:
- RECEIVE: RX always turned on

Then I tried to upload this sketch: "listener" dnd i've saved its library on my PC (https://code.google.com/p/canduino/source/browse/trunk/#trunk%2FLibrary%2FCAN)

The result is this: i read this string from the serial monitor:
hello
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Jan 17, 2014, 07:22 am
Try the library here: https://github.com/coryjfowler/MCP2515_lib

I have fixed a lot of the issues I encountered with the Seeedstudio library and the examples.  It has been working pretty well at this point even hooked up to highly utilized CAN Buses.  I also have the intention to add support for multiple MCP2515 ICs, error support, and a better interrupt handling method.

Also, using the MCP2515 in "listen" mode causes it to NOT acknowledge any messages it receives.  It is in a purely ears-only mode.  If you use this mode on a CAN bus with only one other active CAN transceiver, you will find messages are sent infinitely till its controller errors out and shuts down.

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: bsahilu on Feb 21, 2014, 09:01 pm
Guys,

I've been doing a bit of tinkering with bus networking for a particular project I'm working on.  Initially I attempted to implement  ModBus protocol running on an RS-485 physical layer using 3 arduino slaves and one Mega master.  My over-all intention/objective is to prototype for a large 100+ node-support each performing simple tasks.  Each slave/node has ONE output and TWO inputs which they need to monitor and report to a master controller. A GUI app will be tied to the master node making use of the data for an operator.

This was successfully done using ModBus, however, the major draw-back is that for my project I will NOT have the ablity to individually preset the slave IDs.  On the flip-side, I will be embedding a UNIQUE serial IC that will guarantee each node to have a non-repeating addressable value.
My issue with ModBus is that I cannot detect a new slave added to the network without knowing it's address to ask it it's address....if that makes sense. Furthermore, as per the requirements of the task at hand, I am to assume that ALL 100+ nodes are identically flashed and are ALREADY connected to a bus.  Oh AND I have no physical access to each node (they're mounted behind cabinets and installed by NON-technical people....they just connect them...that's IT!).

That's where I stumbled upon switching to a CAN bus.  Being that I can load identical firmware on each node and (in theory) I can have a multi-master situation where each node can broadcast onto the bus, this seemed pretty promising. First of all, am I understanding correctly? Can I in fact upon connecting to the network send out a message that has a destination address of the MASTER node to say 'hey, I'm new, give me a node address?.'  Or even have the MASTER node send out a broadcast message periodically asking 'hey, is there anyone new?.' 

If so, does the library linked above (https://github.com/coryjfowler/MCP2515_lib (https://github.com/coryjfowler/MCP2515_lib))  have commands to support this?
I want to make sure before I buy a few shields to support CAN bus.  Maybe CAN isn't the best answer....any ideas guys?

Thanks in advance.

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: tomsvilans on Mar 19, 2014, 06:52 pm
bsahilu,

Did you get answers to your questions? I am a noob at CAN - very simply trying to establish some triggering / simple monitoring of some equipment using the Sparkfun CAN-BUS shield - and can't seem to figure it out... Would be interested if you got anywhere with your questions!
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: polyglot on Mar 25, 2014, 06:26 am

Guys,

I've been doing a bit of tinkering with bus networking for a particular project I'm working on.  Initially I attempted to implement  ModBus protocol running on an RS-485 physical layer using 3 arduino slaves and one Mega master.  My over-all intention/objective is to prototype for a large 100+ node-support each performing simple tasks.  Each slave/node has ONE output and TWO inputs which they need to monitor and report to a master controller. A GUI app will be tied to the master node making use of the data for an operator.

This was successfully done using ModBus, however, the major draw-back is that for my project I will NOT have the ablity to individually preset the slave IDs.  On the flip-side, I will be embedding a UNIQUE serial IC that will guarantee each node to have a non-repeating addressable value.


The CANopen NMT protocol will solve this for you. It has a Master node that keeps track of what nodes are on the bus, sends out periodic requests for new nodes to announce themselves, allocates them an unused address and implicitly gives them permission to participate in the bus with that new address.


That's where I stumbled upon switching to a CAN bus.  Being that I can load identical firmware on each node and (in theory) I can have a multi-master situation where each node can broadcast onto the bus, this seemed pretty promising. First of all, am I understanding correctly? Can I in fact upon connecting to the network send out a message that has a destination address of the MASTER node to say 'hey, I'm new, give me a node address?.'  Or even have the MASTER node send out a broadcast message periodically asking 'hey, is there anyone new?.' 


Yes, CAN is multi-master.  But you generally want to design your network so that only one special head node runs particular master services like address-space management.  The CANopen NMT is similar to what you suggest, with a dedicated NMT Master service running on a central node and managing who has access to the bus with what address.  The specification provides for several levels of intelligence from both the NMT master and each of the slaves, from fully-dynamic and capable of error-reporting, down to basically brain-dead operation from jumpered addresses.

If you go with a recognised presentation protocol like CANopen, it also gives you formalisms for communicating the input and output values of each node that will possibly be compatible with other commercial (factory automation) systems.  Plus other useful things like the ability to download configuration data/code to your slaves and whole lot more.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: lian999111 on Mar 28, 2014, 09:18 am

Try the library here: https://github.com/coryjfowler/MCP2515_lib

I have fixed a lot of the issues I encountered with the Seeedstudio library and the examples.  It has been working pretty well at this point even hooked up to highly utilized CAN Buses.  I also have the intention to add support for multiple MCP2515 ICs, error support, and a better interrupt handling method.

Also, using the MCP2515 in "listen" mode causes it to NOT acknowledge any messages it receives.  It is in a purely ears-only mode.  If you use this mode on a CAN bus with only one other active CAN transceiver, you will find messages are sent infinitely till its controller errors out and shuts down.




Hi cory,
Your library for MCP2515 is the best one I've tried so far. Thank you very much. I also found a bug in sendMsg() function, which I guess you might want to be informed of.

I think the code: res1 = mcp2515_readRegister(txbuf_n) should be changed to: res1 = mcp2515_readRegister(txbuf_n - 1) to get TXBnCTRL correctly. I check the library, txbuf_n is the address of SIDH returned by mcp2515_getNextFreeTXBuf(&txbuf_n). If bit3 in SIDH happen to be 1,  the bit will never be cleared even the CAN message has been sent successful, and sendMsg() will be stuck in the while loop until the set TIMEOUTVALUE is reached.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Jun 03, 2014, 07:14 am
The library I linked to has no high-level functions.  It is purely a library to control the MCP2515 for generic CAN use.  Higher level functionality would need to be coded for your specific needs in your sketch.  I have used the library to mock other protocols like J1939 and NMEA2000 but my sketch is what contained that higher functionality.  It would be possible to create additional libraries that take advantage of the MCP2515 library to allow a more streamlined approach to these other protocols but that is outside the scope of the library I have been debugging and improving.  I hope that clarifies things a bit.


Hi cory,
Your library for MCP2515 is the best one I've tried so far. Thank you very much. I also found a bug in sendMsg() function, which I guess you might want to be informed of.

I think the code: res1 = mcp2515_readRegister(txbuf_n) should be changed to: res1 = mcp2515_readRegister(txbuf_n - 1) to get TXBnCTRL correctly. I check the library, txbuf_n is the address of SIDH returned by mcp2515_getNextFreeTXBuf(&txbuf_n). If bit3 in SIDH happen to be 1,  the bit will never be cleared even the CAN message has been sent successful, and sendMsg() will be stuck in the while loop until the set TIMEOUTVALUE is reached.

I will look into that, thanks for pointing it out!  

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: Da_Beast on Jan 23, 2015, 02:24 pm
Hi Guys!

I've been looking at this library and the code for some time now. The CAN.sendMsgBuf() function uses the ID of the receiving end but is this limited to 0xFF? Normaly CAN can handle more nodes so is this a restriction of the library?
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: goodguy on May 12, 2016, 07:37 pm
Try the library here: https://github.com/coryjfowler/MCP2515_lib

I have fixed a lot of the issues I encountered with the Seeedstudio library and the examples.  It has been working pretty well at this point even hooked up to highly utilized CAN Buses.  I also have the intention to add support for multiple MCP2515 ICs, error support, and a better interrupt handling method.

Also, using the MCP2515 in "listen" mode causes it to NOT acknowledge any messages it receives.  It is in a purely ears-only mode.  If you use this mode on a CAN bus with only one other active CAN transceiver, you will find messages are sent infinitely till its controller errors out and shuts down.


Did you ever build a library for multiple mcp2515 ?

I'm interested in having 2 can bus shields controlled by one controller.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Jun 30, 2016, 05:32 pm
Did you ever build a library for multiple mcp2515 ?

I'm interested in having 2 can bus shields controlled by one controller.
The library has been capable since 2014, if I recall correctly.

Before the setup() function, does the following exist?
Code: [Select]

MCP_CAN CAN0(10);


If so, make
Code: [Select]

MCP_CAN CAN1(the unique digital pin the second CAN chip select is on);

MCP_CAN CAN2(the unique digital pin the third CAN chip select is on);

and so on.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: goodguy on Jun 30, 2016, 05:41 pm
The library has been capable since 2014, if I recall correctly.

Before the setup() function, does the following exist?
Code: [Select]

MCP_CAN CAN0(10);


If so, make
Code: [Select]

MCP_CAN CAN1(the unique digital pin the second CAN chip select is on);

MCP_CAN CAN2(the unique digital pin the third CAN chip select is on);

and so on.

I must have looked at the wrong library.

The one I played with has;

const int SPI_CS_PIN = 10;

MCP_CAN CAN (SPI_CS_PIN);

Do you have a link to the gethub page with the correct library?  Thanks so much.

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Jun 30, 2016, 06:42 pm
https://github.com/coryjfowler/MCP_CAN_lib/tree/Sub-Development
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: goodguy on Jul 14, 2016, 08:31 pm
Had time today to try this library.

Thanks Cory for the link and library.

Having a problem with it.

Have any idea why?

Thanks

See attached image.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Jul 15, 2016, 01:38 am
DO NOT use MCP_STD or MCP_EXT in the can0.begin(...) function.  There is a bug in the silicon of the MCP2515 and the feature does NOT operate as specified in the datasheet.  The only two solid modes are MCP_ANY or MCP_STDEXT.  I have confirmation from Microchip on the bug.

Quote
**** AUTOMATED MESSAGE - SEE RESPONSE METHODS BELOW ****

Below is a proposed resolution from Microchip Engineering Support Team for your Ticket 278363

Area         : Analog/Interface Products
Product Group: SMPINT
Product      : MCP2515

Problem Description:
I have been developing a library for the MCP2515 protocol controller for the Arduino community.  I am at the point where I believe that the issue lies in the IC and not the library.  My issue is with the mask and filtering functions with-in the IC.  I have found that what I am seeing contradicts what the datasheet reads. DS21801G page 27 and 28 inform me that setting RXM1 and RXM0 on the RXBnCTRL register will change how received frames are handled.  Setting those bits to '11' receives everything and ignores the mask and filter functions, setting those bits to '10' will receive only valid extended IDs that meet filter criteria and so on..  I have set those bits to '10' in both registers and I still get standard IDs entering the receive buffer.  I have set up the library to read the register after it has written to the IC and shows the correct values were written.  The same goes if I set those bits to '01' I will still get extended IDs appearing, but, as the data sheet declares, the registers RXFnEID8:RXFnEID0 are ignored.  Basically, it was filtering the first 11 bits of the extended IDs that came in.  This was across more than one IC, though, they were probably from the same lot as they were purchased together from Mouser.  Assistance would be greatly appreciated. 

This is identical to Ticket 273853.

My test bus has standard identifier and extended identifier messages being transmitted on it.  With both masks empty both extended identifiers and standard identifiers entering the receive buffer of the MCP2515 I am using regardless of how the above bits are set which does not correspond to the datasheet as I understand it.

Problem Resolution:
Hi Cory,

I have received feedback from the MCP2515 applications engineer.

In short, he was able to reproduce the observations related to RXM 01 and 10 are not working as described. I am expressed interest in exercising similar tests on the MCP25625, a new CAN module, and exercising a design review of cause if there is a similar result. However, the given the age of the MCP2515 design, it is unlikely the MCP2515 will be reviewed by design and updated.

Observation Summary:
RXM 00 can be used to setup filters.
RXM 11 can be used to receive all messages.
RXM 01 and 10 are not working as described.

Thanks for your persistence,

Michael
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: Casca on Sep 05, 2016, 10:02 am
Hello,
I am trying to set up an CAN network using a can shield from aliexpress with the MCP2515 and a TJA1050 tranceiver.
For the test I am using a arduino nano and your libray Cory.

When I try the Send example the initializing works fine, but then I always get the "error sending message"

Is there a way, to get more debug information? Do I have the send an ack message or something else? I am just reading the can signals from the Arduino with a CAN USB interface which I use at work.

BR Alex
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Sep 06, 2016, 05:18 am
"Do you have to send the acknowledge"  - No, the acknowledge is a single dominant bit that is sent by any active receiving node on the CAN bus and should be an automatic thing handled by the protocol controller in your USB dongle assuming it is not in a listen-only mode.

What kind of USB dongle is this and are you receiving anything?

If you are receiving a message, I would check to ensure that the USB dongle you are using is in an active mode on the CAN bus (Not listen-only).  

If you are not receiving any messages, I would check to confirm the crystal frequency set in the program matches the actual crystal frequency on the shield and then confirm baud-rates are the same between the program and your USB dongle. (I have see a lot of cheap Chinese boards using 8MHz crystals instead of 16MHz that Seeedstudio and SKPang have used.)
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: Casca on Sep 06, 2016, 08:07 am
I am using a Vector VN1630 and for viewing the messages I use the trace function in CANape.
No I am not receiving anything. My shield has a 8Mhz crystal so I changed the initialize line to "CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)"

For wiring the shield I used Pin10 (CS), Pin11 (MOSI), Pin12 (MISO), 13 (SCK).
This should be correct and also I got the "MCP2515 Initialized Successfully!" message so it seems that the spi connection works.

Edit: I found the error in the Shield. It has a pin header as well as a screw terminal for the CAN connection. The screw terminal somehow seems not be connected properly on the PCB because as I tried the pin header it works just fine!
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: coryjfowler on Sep 12, 2016, 03:48 pm
Sorry about the delay, I just returned from a trip and I didn't have an Arduino with a CAN shield with
me.

I have a clone UNO setup with an old Seeed Studio CAN shield and without any changes to the send example, I am able to see the example message come across CANalyzer at 500k.

Unfortunately the shield I have uses a 16MHz crystal, but I have a MCP2515_CAN break out that uses an 8Mhz crystal and with the frequency modification to the begin function, I see the message come across CANalyzer.

At this point I suspect the shield you sourced may have a bad connection between the protocol controller IC and the transceiver IC, or there is a wiring error between the shield and the CAN dongle.  I assume you also have the bus terminated; at least one termination resistance must be present for reliable communication to occur.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mega-hz on Nov 27, 2016, 11:44 am
for the CAN-BUS Home Automation i made PCBs for use behinde a light switch or power outlet, the pcbs are already in production. This little PCB will have an atmega8 and a MCP2515/TJA1050 Can Bus Transceiver, a Relays for switching / a MOSFET for powering LEDs with pwm.
There are also 7 analog inputs for reading back some things like NTC, LDR or for other useage.
The 8th analog Input replys the state of the relays/mosfet.
There are 8 solder-jumpers for setting the CAN ID (lower 8 Bits of 11)
Here a preview of the pcb:
(http://www.mega-hz.de/Arduino/CAN-Bus/CAN-BusUP.jpg)
I use a standart 4 wire telephone cable for CAN-L, CAN-H, GND and 24V.
Every Slave has a little step-down converter to make the 5V.
Somewhere there will be 24V/7Ah PB-Batteries for Backup-Power, so the system runs even mains fail.
The sketch for the master you can see here: CAN MASTER (http://www.mega-hz.de/Arduino/CAN-Bus/CAN-BUS-WF-MASTER1/CAN-BUS-WF-MASTER1.ino)
The sketch for the slave you can see here: CAN SLAVE (http://www.mega-hz.de/Arduino/CAN-Bus/CAN-BUS-WF-SLAVE1/CAN-BUS-WF-SLAVE1.ino)
These 2 Units are running very stable since about 2 weeks!
I am using standart IDs 000-7FF.
The Master is a UNO+ElecFreaks CAN Shield
(http://www.mega-hz.de/Arduino/CAN-Bus/mcp2515-shield.jpg),
the Slave is a Nano with this small MCP2515 PCB,8Mhz.
(http://www.mega-hz.de/Arduino/CAN-Bus/mcp2515.jpg)
more updates will be published...

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mega-hz on Dec 05, 2016, 06:09 am
the pcbs has arrived and i assembled two of them, everything works fine!
on my test-setup there are one Nano+2515 pcb, one uno+can-shield, 2 round pcb with atmega8.
one atmega8 runs at 12Mhz, the other one with 8 Mhz, both are more than fast enouhg.
The one round pcb is lighting a LED with PWM, the other one turns on/off a relays.
The Voltage is at the moment 12V, later 24-28V.
On AnalogInput7 there is a VoltageDivider to measure the output, there will be a second voltage divider on A6 for measuring the 24V, so i can read and monitor this.
The next step will be making a website for home automation.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: musskopf on Dec 05, 2016, 12:28 pm
Very nice! Like to see projects using Can Bus, it's very simple, reliable and incredibly cheap.

For your Website, have a look on using Grafana + Influxdb for graphing if you plan to collect sensor and status data. It's very simple to insert data and the create nice dashboards. I have created a little blog showing some basics: https://talk2.wisen.com.au/2016/05/24/influxdb-grafana/ (https://talk2.wisen.com.au/2016/05/24/influxdb-grafana/)

I normally run it on a RPI with a Can Bus (don't forget RPi is 3.3v only) or connected via Serial with another Arduino working as Gateway, but I also have some setups using a Google Cloud VM, sending the data over the I internet.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mega-hz on Dec 05, 2016, 07:07 pm
looks pretty good!
Maybe i will use it for my homeautomation!
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: Deacon214 on Dec 20, 2016, 01:40 am
So im trying to use this library with a Sparkfun board, it seems as though the transmit buffers are not clearing after the message is being sent. Are the messages in the transmit buffers cleared by the code in this library? Or are they cleared on a receipt of an ACK?
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mega-hz on Dec 21, 2016, 07:05 am
well, i don't know, do you talk about the buffer in the chip or in the arduino?
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: uneasy on May 01, 2017, 02:47 pm
Hi everyone,

I think this is the most live topic with MCP2515 so I will ask here.

I started playing with CAN Bus in my car. I got MCP2515, the one from the pictures above.
I have changed crystal for 16MHZ. I got some libraries and I made it work.
Or at least partially.
I used CanHacker software to sniff for messages from my CAN, and I got quite few.
I am trying to figure out whats what.

However I decided I want to send messages to the CAN, I have tried different libraries and even one from this post. But I am not sure if it works - I got "message sent successfully" but nothing seems to happening at all.
I only get reaction when I remove delay(100) between messages and the reaction is I have all possible errors on my dashboard - but I think this is due to flooding CAN network.

I decide I want to get information using OBD2 PID request through CAN bus.
So I sent 0x7DF [8] 02 01 0C 00 00 00 00 00 and I suppose to get message back on ID: 0x7E8.
However no matter what I send, I get nothing back.

I am not really sure where to go from this point.
Also, my main thing is I want to get RPM value. I can see which ID and bit is changing with car RPM. But I wasn't able to actually translate that bit HEX value to decimal real RPM value.
Anyone can suggest something with that ?
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mikrotron on Jul 05, 2017, 10:28 pm
uneasy, sending CAN messages seems trivial at first, but it isn't. First, you must distinguish between the CAN frame and OBD-II messages. CAN is only the transport of the message, where OBD-II is a protocoll which describes what is carried inside the data.

You can't just send a single frame containing OBD-II information an expect a replay, it's not simple challenge/response. You must first establish a session.

Your car uses a foreign protocoll and codes the messages according to it's own standard. You can, of course determine current RPM from a OBD-II CAN communication, but also direct from the appropriate CAN-Message the powertrain module is sending to other modules in the car, e.g. the instrument-cluster which displays the RPM value by a gauge.

Also there are at least two separated CAN-busses in your car. One for the powertrain and one for comfortsystems (central locking, doors, interior light, etc.). Some cars so have an additional bus for multimedia/audio systems. Not all information is available on every bus. The powertrain is mostly driven at 500 kbit/s whereas the comfort and multimedia is 250 or 125 kbit/s. Often a gateway routes messages from one bus to the other. The OBD-II port of your car is a good place to gain access to the CAN bus(ses). At least HS-CAN (powertrain) and MS-CAN (comfort) are connected here. If you are lucky, they are directly wired to the bus. But some vendors connect the OBD-II port to a special gateway device, which will filter all traffic not wanted or authenticated to the car. This device is most often the router between the busses.

So, without some deeper knowledge of your cars wiring and modules, there are too many pitfalls. You should try to get some wiring diagrams and bus-lists. I done this myself before and believe me to start this way, or will won't get happy. Take a look here, on my website, where you can find the results of my research on Ford Mondeo MK4: http://mk4-wiki.denkdose.de/grundlagen/can-bus/start

I've spend hours and hours loggind CAN data, parsing, filtering and reading logs to know what CAN-ID came from which module, takes what data and how it is assembled. There are hundrets of messages with thousands of different data. To analyze them and find actors, sensors i "invented" some processes. The simplest is to look for differences in data while pushing some button. You can filter out all data not changing while you push a button on your dash and focus on the remaining. Then build a time profile to synchronize a data change with your action. This can mostly be done by looking at CANhacker in Monitor-Mode.

Push buttons are flags/bits and very easy to find. Komplex values hide better :-) Sometimes you need to know how the data is coded, even if you are shure about where in the messages it is. E.G. decimal values often shiftet by the number of their fractional part. So a 28.3 degree value could be in a two byte data, coded as 283. Also values which can have a sign (+ or -) can be coded with an offset. Temperaturevalues for example, often added with 40, so a value of 0 means -40° and a value of 40 means 0° and 60 means +20°.

You must know to divide by 10 to get the value in question. Other values are more complex to calculate, not all are linear. This is the hardest part. Look at OBD-II content. Here you find math to calculate the values. Maybe they are used by the car manufacturers CAN-messages also...
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: eltonio on Aug 04, 2017, 03:42 pm
mega-hz,

Thank you for sharing your efforts. Seems to be small, universal and eligible for home automation needs CAN slave PCB. Could you share your EAGLE schema project too?
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mbedadroid on Aug 14, 2017, 11:59 am
based on the idea of Megahz i created a project on https://github.com/MbedAndroid/Can

its running now on 3 devices now. Got 4 analog inputs, Canident i use a Ds1280 temp controller.
The frames are send to a Mbed which send it up via internet to a Pi
Home automation is running on the Pi

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: dearroz on Oct 24, 2017, 09:14 pm
I understand the basic send and receive sketches, but I wish to send / receive a data structure rather than bytes.  The structure will be something like:
struct MsgType
{
  unsigned int NodeID;
  unsigned int NodeLoc;
  unsigned int DevType;
  byte DevID;
  byte DevStat;
};


MsgType  MsgData

After assigning values the data would be transmitted.  On receiving the data it would be translated back into a structure format.

All examples that I have seen on the Internet deal only with byte data.  Can a structure be transmitted and received, and can you please provide some sample code.

I am wanting to use this in a model railroad to transmit messages to operate various devices such as crossarms and servos.

Appreciate your assistance.

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mbedadroid on Dec 05, 2017, 11:10 pm
you fix those things with:

 typedef union {
    UCHAR array[sizeof(float)];
    float ebyte;
} E_float;

typedef union {
   UCHAR array[sizeof(int)];
   int Eint;
} E_int;

typedef union {
   UCHAR array[sizeof(UCHAR)];
   UCHAR Euchar;
} E_UCHAR;


in fact you need a converter visa versa to convert your structure

Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: baghernaseri on Dec 26, 2017, 09:05 pm
Hello friends,
I wanted to know how to turn on the brake lights through the canbus protocol. Please help me with this. I can use the can bus library from my arduino and mcp2515.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: mikrotron on Dec 27, 2017, 02:12 pm
Hello baghernaseri. First of all: "You know that you should not do this, because it's illegal and risk to harm yourself and others!" Don't mess around with your CAR, it is not a playground.

Now, to the technical issues to solve. Due to the nature of CAN-Bus usage in cars, it is not a trivial task to produce messages which already exist. That means that most messages are repeated constantly in a fast period between 5 and 1000 ms, depending on the priority of the data it contains and the bus speed. If two nodes tries to send the same message (ID), they overlapp and produce CAN-Bus errors, which will shut down some systems sooner or later. Also the different signals inside the message will drive the receiver-module nuts and it may ignore them or shutdown itself.

The only chance to produce CAN messages at will it so put a filterdevice between the module to control and the CAN-Bus connector for it. This is some kinde of "man-in-the-middle-attack". A microcontroller must receive all signals from one interface (side) and pass them to the other unchanged. Only the requestet signales may be mangled/manipulated.

Next, i would doubt about that you cars brake lights are connected to any CAN-Bus! It is a critical system. In most cars the brake-pedal has a switch, which signals the brake to the central computer (BCM/GEM). There may also other module which signal a "brake" to the central computer like Adaptive Cruise Controls, or pedestrian protection systems. The BCM will then drive a lamp or LED with a direct wire to light. So if you don't own one of these new fancy cars, where everything is CAN connected, it would be the way i described.


Hope this helps you get things clearer.
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: JohnRob on Dec 28, 2017, 10:04 pm
@baghernaseri


Its not nice to cross post >:(
Title: Re: Cheap & simple MCP2551/MCP2515 CAN BUS set up
Post by: Rumenigg on Oct 17, 2018, 11:25 pm
   Hello.
  I'm working in a personal project with my car and I'm trying to get RPM information. For that I bought a Serial CAN-BUS Module based on MCP2551 and MCP2515 from SeeeStudio, at https://www.seeedstudio.com/Serial-CAN-BUS-Module-based-on-MCP2551-and-MCP2515-p-2924.html and I'm not getting information from my car. I'm using a teensyduino 3.1 and serial CAN Module.
  I asked for a help on seeed site but I didn't receive any help.
  My car is a Volkswagem Saveiro 2013 and on OBD-II conector doesn't have CAN-H and CAN-L cables, just K-MASS cable, and I'm catching CAN-H, CAN-L cables from instruments' panel.
  On Serial Monitor is showing that is sending information, but don't receive anything.
  Their library works with SoftwareSerial library to create aditional serial port but my teensyduino has three serial ports and I'm thinking that the problem is with their library.
  I'm connecting the module, tx on pin 7 and rx on pin 8 of teensy.
  Could you help me please?
  Thank you.