| Author |
Message |
Tobi163
Joined: 10/12/2017 Posts: 4 Karma: +0 / -0
Free account, no CAN development support
|
12-10-2017, 15:33 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
Hello Canhack members, my name is Tobi and I don't have much experience with CAN bus systems yet, but I find the topic very interesting. Since I also enjoy working with Arduino, I wanted to start with a small project.
I would like to use a CAN bus shield and an Arduino Uno with my old BMW E46 to extract standard data such as engine speed/speed, and then display it on an external LCD screen.
I've tested some Arduino libraries for the CAN bus area, but unfortunately, I haven't had much success.
My CAN shield is the Aptotec V1.2, which uses the MCP2515 CAN controller and the MCP2551 CAN transceiver.
I only powered the Arduino via USB from my MacBook and connected the CAN High (pin 6) / CAN Low (pin 14) signals to the two screw terminals on the CAN shield. I desoldered the optional termination resistor.
"As I mentioned, I've tried different code snippets. With one of them, I can connect to a W204 Mercedes-Benz belonging to a friend, but I can't establish a connection with a BMW. Sometimes, the program even sends requests over the bus without errors, but I still encounter intermittent connection problems."
Do I need to establish a common ground connection between the vehicle and the Arduino, as is typically done with other electrical interfaces like RS232? Or are only the two signals, CAN High/Low, required?
Here are my attempts, followed by the serial output.
Code: |
// MCP2515 Mask and Filter example for extended CAN message frames.
// Written by Cory J. Fowler (20140717)
/***********************************************************************************
If you send the following extended IDs below to an Arduino loaded with this sketch
you will find that 0x00FFCC00 and 0x00FF9900 will not get in.
ID in Hex is the same as the Filter in Hex.
0x00FFEE00
0x00FFDD00
0x00FFCC00 This example will NOT be receiving this ID
0x00FFBB00
0x00FFAA00
0x00FF9900 This example will NOT be receiving this ID
0x00FF8800
0x00FF7700
This mask will check the filters against ID bits 23 through 8.
(Those familiar with J1939 might see why I used this mask.)
MASK = 0x00FFFF00
If there is an explicit filter match to those bits, the message will be passed to the
receive buffer and the interrupt pin will be set.
This example will NOT be exclusive to ONLY the above message IDs, for that a mask such
as the below would be used:
MASK = 0x1FFFFFFF
At the moment, to disable a filter or mask, copy the value of a used filter or mask.
***********************************************************************************/
#include <mcp_can>
#include <SPI>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
else Serial.print("MCP2515 Init Failed!!\r\n");
pinMode(2, INPUT); // Setting pin 2 for /INT input
CAN0.init_Mask(0,1,0x00FFFF00); // Init first mask...
CAN0.init_Filt(0,1,0x00FFEE00); // Init first filter...
CAN0.init_Filt(1,1,0x00FFDD00); // Init second filter...
CAN0.init_Mask(1,1,0x00FFFF00); // Init second mask...
CAN0.init_Filt(2,1,0x00FFBB00); // Init third filter...
CAN0.init_Filt(3,1,0x00FFAA00); // Init fouth filter...
CAN0.init_Filt(4,1,0x00FF8800); // Init fifth filter...
CAN0.init_Filt(5,1,0x00FF7700); // Init sixth filter...
Serial.println("MCP2515 Library Mask & Filter Example...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
Serial.print("ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for(int i = 0; i<len; i++) // Print each byte of the data
{
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print("0");
}
Serial.print(rxBuf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
|
BMW:
Code: |
KEntering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Simple CAN OBD-II PID Request
Error Sending Message...
Error Sending Message...
|
Mercedes:
Code: |
MCP2515 Initialized Successfully!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Mask!
Setting Mask Successful!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Starting to Set Filter!
Setting Filter Successfull!
Simple CAN OBD-II PID Request
Error Sending Message...
Error Sending Message...
Message Sent Successfully!
Error Sending Message...
Error Sending Message...
Message Sent Successfully!
Error Sending Message...
Error Sending Message...
Message Sent Successfully!
Error Sending Message...
Message Sent Successfully!
Message Sent Successfully!
Message Sent Successfully!
Message Sent Successfully!
Message Sent Successfully!
Error Sending Message...
Message Sent Successfully!
Message Sent Successfully!
Error Sending Message...
Message Sent Successfully!
Message Sent Successfully!
Error Sending Message...
Message Sent Successfully!
Error Sending Message...
Error Sending Message...
Message Sent Successfully!
Error Sending Message...
Message Sent Successfully!
Message Sent Successfully!
Error Sending Message...
Error Sending Message...
Error Sending Message...
|
I would really appreciate it if someone could give me some advice. I've also been considering ordering the Can Hack Adapter from this forum, but what exactly are its capabilities? Could I use it to, for example, identify signals related to power windows or extract data from the steering wheel remote control?
Best regards, Tobi.
Translated on 12-08-2026, 12:45.
|
|
| Back to top |
Profile PM |
 |
postmann CAN-Profi

Joined: 05/23/2013 Posts: 145 Karma: +64 / -0
CAN Support
|
12-10-2017, 20:48 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
I suspect you have set the wrong baud rate. Are you sure you are connected to a 500 kbaud network on the BMW?
Translated on 12-08-2026, 12:47.
|
|
| Back to top |
Profile PM |
 |
Tobi163
Joined: 10/12/2017 Posts: 4 Karma: +0 / -0
Free account, no CAN development support
|
12-10-2017, 23:25 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
No, I've tried all the baud rates from 125 to 500. Unfortunately, no results yet! Or could it be that only specific PIDs can be queried on the BMW E46 OBD 2, and that for my project, I need to directly tap into the CAN bus, perhaps at the PDC control unit or something similar?
Can someone provide me with information about the CAN Hack adapter?
Best regards, Tobi.
Translated on 12-08-2026, 12:48.
|
|
| Back to top |
Profile PM |
 |
CAN-Diagnose Administrator


Joined: 06/07/2011 Posts: 573 Karma: +29 / -0 Location: Ländle
|
13-10-2017, 11:25 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
Hello Tobi,
It's possible that your BMW behaves like most VAG models in that it only transmits data if the correct messages are received on the diagnostic CAN bus.
If you want to "eavesdrop," the best way is to directly tap into the relevant CAN bus (e.g., for powertrain, comfort features, etc.).
Quote: |
Can someone give me some information about the Can Hack adapter? |
https://shop.dieselschrauber.de/can-usb-interface-kit-mit-rks-can-p-313.php
Best regards, Rainer.
Translated on 12-08-2026, 12:49.
|
|
| Back to top |
Profile PM WWW |
 |
Tobi163
Joined: 10/12/2017 Posts: 4 Karma: +0 / -0
Free account, no CAN development support
|
14-10-2017, 12:15 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
Hello Rainer,
Thank you for your response.
I found the problem. Absolutely nothing is connected to pin 6/14.
I need to continue working on my other car with the experiments related to the CAN bus.
Zum Can hack adapter: kann man diesen nicht einfach mit *halbseidener Zahlungsanbieter für unschlaue Menschen* bezahlen und zugeschickt bekommen ? I'm interested.
The description also mentions that a sample code is included. Is this code for Arduino and similar platforms to send CAN messages?
Best regards, Tobi.
Translated on 12-08-2026, 12:50.
|
|
| Back to top |
Profile PM |
 |
CAN-Diagnose Administrator


Joined: 06/07/2011 Posts: 573 Karma: +29 / -0 Location: Ländle
|
14-10-2017, 12:21 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
Hello Tobi,
If you are not satisfied with the payment options available in the shop, unfortunately, I cannot assist you further, as there are already a sufficient number of options listed under "Payment Options."
The question about "example code for Arduino & Co." is also not very helpful. I don't sell Arduinos, so why would I write example code for them?
Are you trying to joke with me? And how do you even imagine a productive development on the CAN bus of your car? I don't think it's a good idea for you to inject "any" messages into the vehicle buses of your vehicle...
Best regards, Rainer.
Translated on 12-08-2026, 12:52.
|
|
| Back to top |
Profile PM WWW |
 |
Tobi163
Joined: 10/12/2017 Posts: 4 Karma: +0 / -0
Free account, no CAN development support
|
14-10-2017, 14:31 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
Hello Rainer,
I overlooked the other payment options. Visa card is also acceptable.
Is the adapter and software intended for diagnostics and sniffing CAN bus messages, or am I mistaken?
I don't want to do anything extremely complicated on my vehicle. And I think no one is born knowing how everything works. I'm interested in CAN bus and related topics, and I thought a forum was meant for sharing information and helping each other, or at least giving tips – that's what I've experienced in other forums.
My goal would be to, for example, send my own messages via the CAN bus and display them on the instrument cluster, or to control things like the window lifters.
Currently, I have a CAN bus shield with an MCP2515 connected to an Arduino. Initially, I tried to retrieve data through the OBD-2 interface, but it's not working as expected because, as far as I understand, it only sends responses to specific PIDs. However, if I connect directly to the bus, for example, at the car radio, shouldn't I receive messages? Or am I misunderstanding something?
"I currently have the Arduino Shield with the MCP2515 connected to the wiring of the Command Radio (Mercedes) using the bn/rt CAN high and br CAN low wires. When I then power the Arduino, including the shield, via the USB port, my instrument cluster displays garbled messages. However, no CAN bus messages are received in the Arduino's serial monitor; it only shows 'CAN init OK'."
I'm a beginner, that's obvious, but I would really appreciate it if people could offer helpful tips and answers instead of just dismissing me with comments like "you don't know anything," etc. That's not constructive and doesn't help anyone.
Here's another question: Shouldn't the CAN bus connected to my Command unit only be for multimedia functions? Or am I mistaken? Because, as I said, I'm getting confusing information on the instrument cluster, like the high beam indicator being on, then the yellow airbag warning light appears with the message "Please visit a workshop." When I disconnect the Arduino and turn the ignition on and off once, everything goes back to normal.
Greetings, Tobi.
Translated on 12-08-2026, 12:55.
|
|
| Back to top |
Profile PM |
 |
postmann CAN-Profi

Joined: 05/23/2013 Posts: 145 Karma: +64 / -0
CAN Support
|
15-10-2017, 12:41 Subject: Arduino CAN Bus: PID Filtering Tutorial |
Quote |
|
The various networks within a vehicle are typically connected to each other via a gateway.
The entire communication system is defined by its topology. When you encounter a problem in one network, it often affects other networks as well.
To that extent, I have to agree with Rainer: before you send anything (not even an acknowledgment), try to understand the topology and just listen for a while. Moreover, sending a frame from within the vehicle isn't easy because the actual sender won't appreciate it.
Translated on 12-08-2026, 12:58.
|
|
| Back to top |
Profile PM |
 |
|