Hi !
My name is Marcos and I´m new with arduino. I´m trying to get data from CAN BUS from my motorcycle (2019 Harley Davidson Low Rider S).
I´m using arduino UNO and MCP2515, I can connect and see can bus data flowing. But, when I send a request for RPM (PID 0x0C), I can see a lot of messages and very rarely I got a response with 0 value with PID 0x5C1.
I don´t know if Harley Davidson uses a different encoding for this messages, so, if somebody haves experience with this and can give me a clue, I will be vare grateful.
Here is my code :
// Service 01 PIDs (more detail: https://en.wikipedia.org/wiki/OBD-II_PIDs)
#define PID_ENGINE_LOAD 0x04
#define PID_COOLANT_TEMP 0x05
#define PID_SHORT_TERM_FUEL_TRIM_1 0x06
#define PID_LONG_TERM_FUEL_TRIM_1 0x07
#define PID_SHORT_TERM_FUEL_TRIM_2 0x08
#define PID_LONG_TERM_FUEL_TRIM_2 0x09
#define PID_FUEL_PRESSURE 0x0A
#define PID_INTAKE_MAP 0x0B
#define PID_ENGINE_RPM 0x0C
#define PID_VEHICLE_SPEED 0x0D
#define PID_TIMING_ADVANCE 0x0E
#define PID_INTAKE_TEMP 0x0F
#define PID_MAF_FLOW 0x10
#define PID_THROTTLE 0x11
#define PID_AUX_INPUT 0x1E
#define PID_RUNTIME 0x1F
#define PID_DISTANCE_WITH_MIL 0x21
#define PID_COMMANDED_EGR 0x2C
#define PID_EGR_ERROR 0x2D
#define PID_COMMANDED_EVAPORATIVE_PURGE 0x2E
#define PID_FUEL_LEVEL 0x2F
#define PID_WARMS_UPS 0x30
#define PID_DISTANCE 0x31
#define PID_EVAP_SYS_VAPOR_PRESSURE 0x32
#define PID_BAROMETRIC 0x33
#define PID_CATALYST_TEMP_B1S1 0x3C
#define PID_CATALYST_TEMP_B2S1 0x3D
#define PID_CATALYST_TEMP_B1S2 0x3E
#define PID_CATALYST_TEMP_B2S2 0x3F
#define PID_CONTROL_MODULE_VOLTAGE 0x42
#define PID_ABSOLUTE_ENGINE_LOAD 0x43
#define PID_AIR_FUEL_EQUIV_RATIO 0x44
#define PID_RELATIVE_THROTTLE_POS 0x45
#define PID_AMBIENT_TEMP 0x46
#define PID_ABSOLUTE_THROTTLE_POS_B 0x47
#define PID_ABSOLUTE_THROTTLE_POS_C 0x48
#define PID_ACC_PEDAL_POS_D 0x49
#define PID_ACC_PEDAL_POS_E 0x4A
#define PID_ACC_PEDAL_POS_F 0x4B
#define PID_COMMANDED_THROTTLE_ACTUATOR 0x4C
#define PID_TIME_WITH_MIL 0x4D
#define PID_TIME_SINCE_CODES_CLEARED 0x4E
#define PID_ETHANOL_FUEL 0x52
#define PID_FUEL_RAIL_PRESSURE 0x59
#define PID_HYBRID_BATTERY_PERCENTAGE 0x5B
#define PID_ENGINE_OIL_TEMP 0x5C
#define PID_FUEL_INJECTION_TIMING 0x5D
#define PID_ENGINE_FUEL_RATE 0x5E
#define PID_ENGINE_TORQUE_DEMANDED 0x61
#define PID_ENGINE_TORQUE_PERCENTAGE 0x62
#define PID_ENGINE_REF_TORQUE 0x63
//----------------------------------------------
#define CAN_ID_PID 0x7DF //OBD-II CAN frame ID
#include <mcp_can.h>
#include <SPI.h>
#define CAN0_INT 2 // Set INT to pin 2 <--------- CHANGE if using different pin number
MCP_CAN CAN0(10); // Set CS to pin 10 <--------- CHANGE if using different pin number
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
void sendPID(unsigned char __pid)
{
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
byte sndStat = CAN0.sendMsgBuf(CAN_ID_PID, 0, 8, tmp);
if (sndStat == CAN_OK) {
Serial.print("PID sent: 0x");
Serial.println(__pid, HEX);
}
else {
Serial.println("Error Sending Message...");
}
}
void receivePID(unsigned char __pid)
{
if (!digitalRead(CAN0_INT)) { // If CAN0_INT pin is low, read receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
sprintf(msgString, "Received -> Standard ID: 0x%.3lX, DLC: %1d, Data: ", rxId, len);
Serial.print(msgString);
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
Serial.println("");
switch (__pid) {
case PID_THROTTLE:
if(rxBuf[2] == PID_THROTTLE){
uint8_t percent;
percent = (100 / 255) * rxBuf[3];
Serial.print("Throttle position (%): ");
Serial.println(percent, DEC);
}
break;
case PID_ENGINE_RPM:
if(rxBuf[2] == PID_ENGINE_RPM){
uint16_t rpm;
rpm = ((256 * rxBuf[3]) + rxBuf[4]) / 4;
Serial.print("Engine Speed (rpm): ");
Serial.println(rpm, DEC);
}
break;
}
}
}
void setup()
{
Serial.begin(9600);
// Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ) == CAN_OK) { //< -------- - CHANGE if using different board
Serial.println("MCP2515 Initialized Successfully!");
}
else {
Serial.println("Error Initializing MCP2515...");
while (1);
}
//initialise mask and filter to allow only receipt of 0x5xx CAN IDs
CAN0.init_Mask(0, 0, 0x05000000); // Init first mask...
CAN0.init_Mask(1, 0, 0x05000000); // Init second mask...
for (uint8_t i = 0; i < 6; ++i) {
CAN0.init_Filt(i, 0, 0x05000000); //Init filters
}
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("Sending and Receiving OBD-II_PIDs Example...");
}
void loop()
{
//request Throttle %
sendPID(PID_THROTTLE);
delay(40); //to allow time for ECU to reply
receivePID(PID_THROTTLE);
//request engine speed
sendPID (PID_ENGINE_RPM);
delay(40); //to allow time for ECU to reply
receivePID(PID_ENGINE_RPM);
//abitrary loop delay
delay(40);
}
And my output :
20:44:51.382 -> Entering Configuration Mode Successful!
20:44:51.414 -> Setting Baudrate Successful!
20:44:51.446 -> MCP2515 Initialized Successfully!
20:44:51.486 -> Sending and Receiving OBD-II_PIDs Example...
20:44:51.526 -> PID sent: 0x11
20:44:51.561 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:51.628 -> PID sent: 0xC
20:44:51.628 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x00
20:44:51.728 -> Engine Speed (rpm): 0
20:44:51.762 -> PID sent: 0x11
20:44:51.762 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:51.827 -> PID sent: 0xC
20:44:51.861 -> Received -> Standard ID: 0x544, DLC: 8, Data: 0x0D 0xBB 0x40 0xFF 0x00 0x00 0x00 0x00
20:44:51.960 -> PID sent: 0x11
20:44:51.960 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0x11 0xA2 0x82 0x80 0xFF 0x7F 0x20 0x10
20:44:52.061 -> PID sent: 0xC
20:44:52.061 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:52.160 -> PID sent: 0x11
20:44:52.160 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:52.227 -> PID sent: 0xC
20:44:52.260 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:52.327 -> PID sent: 0x11
20:44:52.360 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:52.426 -> PID sent: 0xC
20:44:52.460 -> Received -> Standard ID: 0x570, DLC: 6, Data: 0x01 0x00 0x00 0x00 0x00 0x00
20:44:52.526 -> PID sent: 0x11
20:44:52.559 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x00
20:44:52.625 -> PID sent: 0xC
20:44:52.659 -> Received -> Standard ID: 0x544, DLC: 8, Data: 0x0D 0xBB 0x40 0xFF 0x00 0x00 0x00 0x00
20:44:52.759 -> PID sent: 0x11
20:44:52.759 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0x11 0xA2 0x82 0x80 0xFF 0x7F 0x20 0x10
20:44:52.859 -> PID sent: 0xC
20:44:52.859 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:52.959 -> PID sent: 0x11
20:44:52.959 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:53.059 -> PID sent: 0xC
20:44:53.059 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:53.125 -> PID sent: 0x11
20:44:53.158 -> Received -> Standard ID: 0x501, DLC: 1, Data: 0x01
20:44:53.192 -> PID sent: 0xC
20:44:53.225 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:53.292 -> PID sent: 0x11
20:44:53.325 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:53.391 -> PID sent: 0xC
20:44:53.425 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0x11 0xA2 0x82 0x80 0xFF 0x7F 0x20 0x10
20:44:53.525 -> PID sent: 0x11
20:44:53.525 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:53.625 -> PID sent: 0xC
20:44:53.625 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:53.739 -> PID sent: 0x11
20:44:53.746 -> Received -> Standard ID: 0x540, DLC: 8, Data: 0x01 0x11 0x57 0xCF 0xFF 0x00 0x00 0x00
20:44:53.814 -> PID sent: 0xC
20:44:53.814 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:53.891 -> PID sent: 0x11
20:44:53.891 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:53.991 -> PID sent: 0xC
20:44:53.991 -> Received -> Standard ID: 0x541, DLC: 8, Data: 0x00 0x00 0x00 0x00 0x00 0x50 0x00 0xFF
20:44:54.090 -> PID sent: 0x11
20:44:54.125 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x00
20:44:54.222 -> Received -> Standard ID: 0x542, DLC: 8, Data: 0x20 0xA2 0x00 0x08 0x4B 0x10 0xA0 0x00
20:44:54.324 -> PID sent: 0x11
20:44:54.324 -> Received -> Standard ID: 0x532, DLC: 8, Data: 0xA0 0xAA 0xAA 0xA0 0x00 0x00 0x00 0x00
20:44:54.423 -> PID sent: 0xC
20:44:54.423 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:54.523 -> PID sent: 0x11
20:44:54.523 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:54.591 -> PID sent: 0xC
20:44:54.636 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x00
20:44:54.690 -> Engine Speed (rpm): 0
20:44:54.723 -> PID sent: 0x11
20:44:54.723 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:54.823 -> PID sent: 0xC
20:44:54.856 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:54.920 -> PID sent: 0x11
20:44:54.920 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7C 0x60 0x10
20:44:55.022 -> PID sent: 0xC
20:44:55.022 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:55.122 -> PID sent: 0x11
20:44:55.122 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:55.222 -> PID sent: 0xC
20:44:55.222 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.322 -> PID sent: 0x11
20:44:55.322 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.422 -> PID sent: 0xC
20:44:55.455 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.522 -> PID sent: 0x11
20:44:55.555 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:55.322 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.422 -> PID sent: 0xC
20:44:55.455 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.522 -> PID sent: 0x11
20:44:55.555 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:55.621 -> PID sent: 0xC
20:44:55.655 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:55.721 -> PID sent: 0x11
20:44:55.754 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:55.821 -> PID sent: 0xC
20:44:55.854 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:55.922 -> PID sent: 0x11
20:44:55.954 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x10
20:44:56.054 -> PID sent: 0xC
20:44:56.054 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:56.120 -> PID sent: 0x11
20:44:56.155 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:56.221 -> PID sent: 0xC
20:44:56.221 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
20:44:56.320 -> PID sent: 0x11
20:44:56.320 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:56.420 -> PID sent: 0xC
20:44:56.420 -> Received -> Standard ID: 0x550, DLC: 6, Data: 0x00 0x00 0x02 0x00 0x00 0x00
20:44:56.520 -> PID sent: 0x11
20:44:56.520 -> Received -> Standard ID: 0x543, DLC: 2, Data: 0x2D 0x1A
20:44:56.587 -> PID sent: 0xC
20:44:56.619 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:56.687 -> PID sent: 0x11
20:44:56.718 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:56.786 -> PID sent: 0xC
20:44:56.820 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:56.919 -> PID sent: 0x11
20:44:56.919 -> Received -> Standard ID: 0x532, DLC: 8, Data: 0xA0 0xAA 0xAA 0xA0 0xE6 0x5D 0x00 0x00
20:44:57.019 -> PID sent: 0xC
20:44:57.019 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x10
20:44:57.119 -> Engine Speed (rpm): 0
20:44:57.153 -> PID sent: 0x11
20:44:57.153 -> Received -> Standard ID: 0x541, DLC: 8, Data: 0x00 0x00 0x00 0x00 0x00 0x50 0x00 0xFF
20:44:57.252 -> PID sent: 0xC
20:44:57.252 -> Received -> Standard ID: 0x521, DLC: 7, Data: 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF
20:44:57.352 -> PID sent: 0x11
20:44:57.385 -> Received -> Standard ID: 0x535, DLC: 8, Data: 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00
20:44:57.451 -> PID sent: 0xC
20:44:57.484 -> Received -> Standard ID: 0x520, DLC: 5, Data: 0x00 0x00 0x00 0x00 0x00
20:44:57.554 -> PID sent: 0x11
20:44:57.585 -> Received -> Standard ID: 0x542, DLC: 8, Data: 0x20 0x82 0x00 0x08 0x4B 0x10 0xA0 0x00
20:44:57.650 -> PID sent: 0xC
20:44:57.685 -> Received -> Standard ID: 0x520, DLC: 5, Data: 0x00 0x00 0x00 0x00 0x00
20:44:57.753 -> PID sent: 0x11
20:44:57.786 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
20:44:57.851 -> PID sent: 0xC
20:44:57.884 -> Received -> Standard ID: 0x520, DLC: 5, Data: 0x00 0x00 0x00 0x00 0x00
20:44:57.949 -> PID sent: 0x11
20:44:57.984 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
20:44:58.051 -> PID sent: 0xC
20:44:58.084 -> Received -> Standard ID: 0x520, DLC: 5, Data: 0x00 0x00 0x00 0x00 0x40
20:44:58.153 -> PID sent: 0x11
20:44:58.183 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
20:44:58.251 -> PID sent: 0xC
20:44:58.284 -> Received -> Standard ID: 0x520, DLC: 5, Data: 0x00 0x00 0x00 0x00 0x40
20:44:58.350 -> PID sent: 0x11
20:44:58.384 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
20:44:58.449 -> PID sent: 0xC
20:44:58.483 -> Received -> Standard ID: 0x5C1, DLC: 8, Data: 0x00 0x00 0x0C 0x00 0x00 0x37 0x00 0x10
20:44:58.550 -> Engine Speed (rpm): 0
20:44:58.583 -> PID sent: 0x11
20:44:58.617 -> Received -> Standard ID: 0x530, DLC: 8, Data: 0xFF 0xFF 0x81 0x83 0xFF 0x7B 0x60 0x10
Thanks !