Cheap & simple MCP2551/MCP2515 CAN BUS set up

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:


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
The sketch for the slave you can see here: CAN SLAVE
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
,
the Slave is a Nano with this small MCP2515 PCB,8Mhz.

more updates will be published...

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.

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/

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.

looks pretty good!
Maybe i will use it for my homeautomation!

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?

well, i don't know, do you talk about the buffer in the chip or in the arduino?

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 ?

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: UMGELEITET [MK4-Wiki]

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 :slight_smile: 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...

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?

based on the idea of Megahz i created a project on GitHub - MbedAndroid/Can: Homeautomation

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

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.

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

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.

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.

@baghernaseri

Its not nice to cross post >:(

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.

I'm hoping to learn how Can bus works using a Usb Peak device, and an Arudino/MCP2515.

The readme on Cory's MCB_CAN_lib github page, says it was tested using a Peak.. so I'm wondering how?

I know CAN doesn't use master/slave, but how can I setup PCAN-View and the Arudino to just see basic communication between the two?

Any ideas how Cory used the Peak / Arduino ?

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

parkerlewis404:
I'm hoping to learn how Can bus works using a Usb Peak device, and an Arudino/MCP2515.

The readme on Cory's MCB_CAN_lib github page, says it was tested using a Peak.. so I'm wondering how?

I tested the baud rate settings in the library using a CAN bus shield and a PCAN-USB adapter connected to a Windows PC. I used PCAN-View basically checking that no errors were reported about messages the Arduino was sending on the small, terminated, CAN bus between the two.

parkerlewis404:
I know CAN doesn't use master/slave, but how can I setup PCAN-View and the Arudino to just see basic communication between the two?
Any ideas how Cory used the Peak / Arduino ?

Basically connect CAN_H of the Peak to CAN_H on the sheild and CAN_L of the Peak to CAN_L of the sheild. Depending on what you're using, you may need to enable the CAN termination on the shield. You need at least one 120ohm resistor placed across CAN_H and CAN_L at one end of the bus. A proper termination uses two 120ohm resistors at the far ends of the bus.

As long as the baudrate set in PCAN-View matches the baudrate programed into the Arduino and you have the Arduino sending a message, something should show up on PCAN-View.