Seeking direction for CAN bus project

Hi, I'm new here. I've had an Arduino for a little while and have done basic I/O projects but now I'm trying to make the leap into CAN bus programming.

My background is in hardware, I can make anything work if it can be wired together. I've got my CAN bus network setup with an Arduino UNO and Inland CAN bus shield, connected to my Raspberry PI 4 with PiCan3 hat. Communication is working... that's the easy part for me.

Programming is my weak point. I'm alright with flow charts and planning on what I want the thing to do, but implementing it... yeah. Most of the programming I've done was in BASIC and on an Apple //e, so kind of outdated, lol. But I did some hardware interfacing with it, so I don't consider myself a total newb.

All of the YouTube videos I've seen are about connecting to an existing network, vehicle, or just getting the network running. I'm doing something a little different. This is going into a vehicle, but it's one I'm building from the ground up. I want the CAN bus to run the lighting, horn, turn on the fuel pump, cooling fans, etc. I'm thinking something like IF "input" turned on, send "message" over bus. On the other end, IF "message" received, THEN turn on "output".

Can someone point me in the right direction on code that will let me parse out CAN messages and associate them with I/O? I want to be able to turn on an input on the PI, and have it turn on an output on the Arduino. If I can get that far, I think I can figure the rest out on my own. Just this one hurdle I can't seem to get over. I can't seem to figure out how to assign the CAN ID or figure out what the existing IDs are for my devices either.

Give this tutorial a try. Also Cory Fowler has a great library for CAN and the MPC2515 using Arduino. It has the basic send and receive modules. Start with them to get a feel for it. Initially forget about the filters etc and pick a decent baud such as 250K. The Arduino Cookbook has a lot of good information, it would be worth a read for you. Hint: CAN does not care who is on the other end as long as they speak the same language.

CAN is Multi-Master not Peer-to-Peer:

  • Peer-to-peer implies direct communication between two nodes without an intermediary.
  • In CAN, messages are broadcast to all nodes on the network. Each node decides if the message is relevant based on its identifier (ID).
  • While nodes can respond to specific requests, the system fundamentally operates as a broadcast network, not a direct peer-to-peer link.

don't forget Canbus is a peer-to-peer network - all devices are equal - all can transmit and all receive every message
receivers can use filters to filter out the messages which are of interest or for simplicity use if statements, e.g. a can module controlling the lights will accept only messages controlling light settings
unless you are using devices with preassigned Can IDs you assign each Can node on your vehicle a CAN ID - 11bit is probably sufficient
design a protocol which can be very simple - each CAN message can be up to 8ytes
e.g. using a single byte message CAN ID 1 is rear light controller ID 2 is front light controller
ID 1 message 1 switch on rear light
ID 1 message 2 switch on stop light
ID 1 message 3 switch on both lights
ID 2 message 1 switch on front lights
etc etc

if you are using commercial modules they may have preassigned Can IDs and message formats

I find when testing Can networks a USB-CAN dongle which plugs into a PC and displays traffic etc very useful for monitoring and debugging the network

1 Like

Is this via OBD2 into an ECU or BCM?.

If not, the number one problem is going to be finding documentation that tells you how to talk to the various devices. Everything in automotive is proprietary and what little information is publicly available is usually the result of reverse engineering. Discovered information about a device may be specific to a particular make, model and year of car and therefore may not be relevant to an "identical" looking device you find in a scrap yard or on Ebay.

1 Like

I'm using a Raspberry Pi as a sort of BCM, but just to control the bare minimum of devices to make the vehicle street legal. The plan is to put an Arduino UNO on both ends of the vehicle to control the lighting, etc. to cut down on wiring. The Arduino outputs will be wired to Triacs or SS relays to isolate the circuitry.

Thanks for the tips guys, I will check out those tutorials and hopefully I will have an A-HA moment when I get it working.

I'm also hoping Python will be a simple syntax conversion for programming. Unfortunately I don't really have the time to be fluent in one programming language, let alone multiple.

where are you using Python?
The Arduino UNO mainly uses C/C++ and the RPi can use C++, Java, Python, etc etc

Seems like Python is the go to language on the Raspberry Pi. The PiCan3 comes with some examples for code in Python. I have the basic stuff running, but still need to learn how to associate the I/O with messages.

Doing a little more research, it seems I misunderstood what CAN IDs are, I thought it was similar to memory mapped I/O but it seems like with the number of messages I intend to use, I don't really need the message content at all, I can just set up a different ID for every command. Unless I'm still missing something and there is something that would make using the same ID with different message content easier.

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