Skip to content

Commit 2a296f0

Browse files
committed
Add initial (untested) OBDII examples
1 parent efc914c commit 2a296f0

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <CAN.h>
2+
3+
// Most cars support 11-bit adddress, others (like Honda),
4+
// require 29-bit (extended) addressing, set the next line
5+
// to true to use extended addressing
6+
const bool useStandardAddressing = true;
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
11+
Serial.println("CAN OBD-II supported pids");
12+
13+
// start the CAN bus at 500 kbps
14+
if (!CAN.begin(500E3)) {
15+
Serial.println("Starting CAN failed!");
16+
while (1);
17+
}
18+
19+
if (useStandardAddressing) {
20+
CAN.filter(0x7e8);
21+
} else {
22+
CAN.filterExtended(0x18daf110);
23+
}
24+
}
25+
26+
void loop() {
27+
if (useStandardAddressing) {
28+
CAN.beginPacket(0x7df, 8);
29+
} else {
30+
CAN.beginExtendedPacket(0x18db33f1, 8);
31+
}
32+
CAN.write(0x02); // number of additional bytes
33+
CAN.write(0x01); // show current data
34+
CAN.write(0x0c); // engine RPM
35+
CAN.endPacket();
36+
37+
// wait for response
38+
while (CAN.parsePacket() == 0 ||
39+
CAN.read() != 0x41 || // correct mode
40+
CAN.read() != 0x0c); // correct PID
41+
42+
float rpm = ((CAN.read() * 256.0) + CAN.read()) / 4.0;
43+
44+
Serial.print("Engine RPM = ");
45+
Serial.println(rpm);
46+
47+
delay(1000);
48+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <CAN.h>
2+
3+
// Most cars support 11-bit adddress, others (like Honda),
4+
// require 29-bit (extended) addressing, set the next line
5+
// to true to use extended addressing
6+
const bool useStandardAddressing = true;
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
11+
Serial.println("CAN OBD-II supported pids");
12+
13+
// start the CAN bus at 500 kbps
14+
if (!CAN.begin(500E3)) {
15+
Serial.println("Starting CAN failed!");
16+
while (1);
17+
}
18+
19+
if (useStandardAddressing) {
20+
CAN.filter(0x7e8);
21+
} else {
22+
CAN.filterExtended(0x18daf110);
23+
}
24+
}
25+
26+
void loop() {
27+
for (int pid = 0x00; pid < 0xe0; pid += 0x20) {
28+
if (useStandardAddressing) {
29+
CAN.beginPacket(0x7df, 8);
30+
} else {
31+
CAN.beginExtendedPacket(0x18db33f1, 8);
32+
}
33+
CAN.write(0x02); // number of additional bytes
34+
CAN.write(0x01); // show current data
35+
CAN.write(pid); // PID
36+
CAN.endPacket();
37+
38+
// wait for response
39+
while (CAN.parsePacket() == 0 ||
40+
CAN.read() < 6 || // correct length
41+
CAN.read() != 0x41 || // correct mode
42+
CAN.read() != pid); // correct PID
43+
44+
unsigned long pidsSupported = 0;
45+
46+
for (int i = 0; i < 4; i++) {
47+
pidsSupported <<= 8;
48+
pidsSupported |= CAN.read();
49+
}
50+
51+
for (unsigned int i = 31; i > 0; i--) {
52+
if (pidsSupported & (1UL << i)) {
53+
int pidSupported = pid + (32 - i);
54+
55+
Serial.print("0x");
56+
if (pidSupported < 16) {
57+
Serial.print("0");
58+
}
59+
Serial.println(pidSupported, HEX);
60+
}
61+
}
62+
63+
if ((pidsSupported & 0x00000001) == 0x00000000) {
64+
// next round not supported, all done
65+
break;
66+
}
67+
}
68+
69+
while (1); // all done
70+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <CAN.h>
2+
3+
// Most cars support 11-bit adddress, others (like Honda),
4+
// require 29-bit (extended) addressing, set the next line
5+
// to true to use extended addressing
6+
const bool useStandardAddressing = true;
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
11+
Serial.println("CAN OBD-II VIN reader");
12+
13+
// start the CAN bus at 500 kbps
14+
if (!CAN.begin(500E3)) {
15+
Serial.println("Starting CAN failed!");
16+
while (1);
17+
}
18+
19+
if (useStandardAddressing) {
20+
CAN.filter(0x7e8);
21+
} else {
22+
CAN.filterExtended(0x18daf110);
23+
}
24+
}
25+
26+
void loop() {
27+
// send the request for the first chunk
28+
if (useStandardAddressing) {
29+
CAN.beginPacket(0x7df, 8);
30+
} else {
31+
CAN.beginExtendedPacket(0x18db33f1, 8);
32+
}
33+
CAN.write(0x02); // Number of additional bytes
34+
CAN.write(0x09); // Request vehicle information
35+
CAN.write(0x02); // Vehicle Identification Number (VIN)
36+
CAN.endPacket();
37+
38+
// wait for response
39+
while (CAN.parsePacket() == 0 ||
40+
CAN.read() != 0x10 || CAN.read() != 0x14 || // correct length
41+
CAN.read() != 0x49 || // correct mode
42+
CAN.read() != 0x02 || // correct PID
43+
CAN.read() != 0x01);
44+
45+
// print out
46+
while (CAN.available()) {
47+
Serial.write((char)CAN.read());
48+
}
49+
50+
// send the request for the remaining bytes
51+
if (useStandardAddressing) {
52+
CAN.beginPacket(0x7e0, 8);
53+
} else {
54+
CAN.beginExtendedPacket(0x18db10f1, 8); // TODO - verify this
55+
}
56+
CAN.write(0x30);
57+
CAN.endPacket();
58+
59+
// read in remaining bytes
60+
for (int i = 0; i < 2; i++) {
61+
// wait for response
62+
while (CAN.parsePacket() == 0 || // wait for response
63+
CAN.read() != 0x21);
64+
65+
// print out
66+
while (CAN.available()) {
67+
Serial.write((char)CAN.read());
68+
}
69+
}
70+
71+
while (1); // all done
72+
}

0 commit comments

Comments
 (0)