OBD2 K-Line Library
Vehicle diagnostics over the K-Line for Arduino and ESP32.
One library for universal OBD-II (ISO 9141-2, ISO 14230-4 / KWP2000) and for manufacturer protocols — VAG KW1281, BMW DS2 and Opel KW82. It handles the handshake, framing, checksums and timing, so your sketch only asks for the data.
Installation · Quick Start · Protocols · API · Wiring · Examples · License
🌟 Overview
K-Line is the single-wire diagnostic bus found on most European and Japanese vehicles built between roughly 1987 and 2010, before CAN became mandatory. This library lets an Arduino, ESP32 or similar microcontroller talk directly to the vehicle's ECU over that wire — no ELM327 adapter in between.
It covers both layers of K-Line diagnostics:
- Generic OBD-II — the legislated services every compliant car answers: live data, freeze frame, trouble codes, vehicle information.
- Manufacturer protocols — the deeper, car-specific access that generic OBD-II never exposes.
flowchart LR
ECU["Vehicle ECU"] <-->|"K-Line · OBD-II pin 7<br/>12 V · 10.4 kbaud"| IF["K-Line interface<br/>L9637D · MC33290 · LM393 · transistors"]
IF <-->|"UART"| LIB["OBD2 K-Line<br/>library"]
LIB --> APP["Your sketch"]
❓ Does Your Vehicle Use K-Line?
Look at the OBD-II connector under the dashboard:
- ✅ Pin 7 populated → K-Line (ISO 9141 / ISO 14230). This library will work.
- ❌ Only pins 6 and 14 populated → CAN bus. Use the OBD2 CAN Bus Library instead.
 |
 |
| K-Line — pin 7 |
CAN bus — pins 6 and 14 |
✨ Features
- 🧩 Six protocols, one API — the packet format is a setting, not a rewrite of your sketch.
- 🔍 Automatic detection — finds the protocol on its own when you do not yet know what the car speaks.
- 🤝 Flexible initialization — 5-baud slow init, fast init, ping and handshake-free modes, each selectable independently of the protocol.
- 🧮 Handles the plumbing — headers, length bytes, checksums, echo removal and handshake timings are built and verified for you.
- 🔌 Works with your serial port —
HardwareSerial, SoftwareSerial and AltSoftSerial, with custom pins.
- 🐞 Developer friendly — integrated debug output showing every byte on the bus.
- 🧱 Layered and extensible — diagnostics live in opt-in ECU files, so unused tables never reach your flash.
📡 Supported Protocols
| Protocol |
Used by |
Framing |
| ISO 9141-2 |
Generic OBD-II |
Header, no length byte, modulo-256 checksum |
| ISO 14230-4 (KWP2000) |
Generic OBD-II |
Length embedded in the header, modulo-256 checksum |
| KW1281 |
VAG — VW, Audi, SEAT, Škoda |
Block protocol, every byte acknowledged with its complement |
| DS2 |
BMW |
Separate length byte counting the whole frame, XOR checksum |
| KW82 |
Opel / Vauxhall |
No header, separate length byte, modulo-256 checksum |
| Custom |
Anything else |
You define the framing yourself |
| Automatic |
— |
Tries the known protocols until one connects |
The connection method is a separate setting from the packet format: 5-baud init, fast init, ping or none can be combined with any protocol through setInitType().
🔍 What You Can Read and Control
Standard OBD-II — works on any compliant car
Generic diagnostics defined by SAE J1979. No car-specific configuration is needed.
| Mode |
Description |
01 |
Live data — real-time sensor values (RPM, coolant temperature, speed, throttle, fuel trims …) |
02 |
Freeze frame — the sensor snapshot stored when a fault appeared |
03 |
Stored Diagnostic Trouble Codes (DTCs) |
04 |
Clear DTCs and reset the MIL |
05 |
Oxygen sensor test results |
06 |
On-board monitoring test results |
07 |
Pending Diagnostic Trouble Codes |
09 |
Vehicle information — VIN, calibration IDs, calibration verification numbers |
DTCs come back as readable codes (P0123 style), and a supported-PID scan lets you ask the ECU which PIDs it actually implements before requesting them.
Manufacturer protocols — deeper, car-specific access
- Extended live data — manufacturer measurement blocks carrying far more channels than the standard PIDs, decoded into named values with real units. One request returns the whole block, so a dozen values cost a single message.
- Vehicle control and actuator tests — command the ECU to drive real hardware: MIL and service lamps, fuel pump relay, A/C relay, throttle actuator, tank vent valve, and per-cylinder ignition coil or injection cut-off.
- Full ECU identification — VIN, part number, supplier, hardware version, software number and engine code.
- ECU memory and flash reading — read the ECU's internal memory block by block, streamed over the debug port so you can capture the dump on your PC.
- Raw service access — send any manufacturer service by hand; the library still adds the header, length byte and checksum.
- Discovery tools — scan which identifiers an ECU answers and dump the responses as offset tables, to map an ECU nobody has documented yet.
[!NOTE]
Manufacturer-specific access is provided through ECU definition files. The generic OBD-II layer ships with the library. Definitions for specific ECUs — Siemens Simtec 71, Bosch Motronic M1.5.5, Bosch ME7.5, Bosch BMS 46, Bosch EDC15VM+ — are maintained separately and are available on request; see Contact.
📦 Installation
Arduino IDE — open Sketch → Include Library → Manage Libraries…, search for OBD2 K-Line and click Install.
PlatformIO — add it to platformio.ini:
lib_deps = muki01/OBD2 K-Line
Manual — download this repository as a ZIP and add it with Sketch → Include Library → Add .ZIP Library…
On the Arduino Uno and Nano the library uses AltSoftSerial; install it from the Library Manager as well.
🚀 Quick Start
Read engine speed, coolant temperature and vehicle speed. The same sketch runs on AVR boards and on the ESP32.
#include "OBD2_KLine.h" // core: connection + protocol layer
#include "ecus/OBD2_Standard.h" // standard OBD2 diagnostics -> OBD2_KLine
OBD2_KLine KLine;
// Uno / Nano have no spare hardware serial, so they fall back to AltSoftSerial
// on its fixed pins (RX 8, TX 9). Every other board uses Serial1.
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#include <AltSoftSerial.h>
AltSoftSerial altSerial;
#define OBD_SERIAL altSerial
#else
#define OBD_SERIAL Serial1
#endif
#define OBD_RX_PIN 5
#define OBD_TX_PIN 4
void setup() {
Serial.begin(115200);
KLine.setSerial(OBD_SERIAL);
KLine.setPins(OBD_RX_PIN, OBD_TX_PIN);
KLine.setDebug(Serial); // View communication logs
KLine.setProtocol(Automatic); // Automatic, ISO9141, ISO14230, KW1281, DS2, KW82, Custom
}
void loop() {
if (!KLine.isConnected() && !KLine.connect()) return;
float rpm = KLine.getLiveData(0x0C); // PID 0x0C: Engine RPM
float coolant = KLine.getLiveData(0x05); // PID 0x05: Coolant Temp
float speed = KLine.getLiveData(0x0D); // PID 0x0D: Vehicle Speed
Serial.print("RPM: "); Serial.println(rpm);
Serial.print("Temp: "); Serial.print(coolant); Serial.println(" C");
Serial.print("Speed: "); Serial.print(speed); Serial.println(" km/h");
}
Reading trouble codes
int storedCount = KLine.readStoredDTCs(); // Mode 03
for (int i = 0; i < storedCount; i++) {
Serial.println(KLine.getStoredDTC(i)); // e.g. P0171
}
KLine.clearDTCs(); // Mode 04
Choosing the protocol and the handshake yourself
KLine.setProtocol(ISO14230); // packet format
KLine.setInitType(Init_5Baud); // connection method — always call it after setProtocol()
📘 API Reference
Connection
| Method |
Description |
setSerial(port) |
Use a HardwareSerial, SoftwareSerial or AltSoftSerial port. |
setPins(rx, tx) |
Select the RX and TX pins. |
setProtocol(protocol) |
Automatic, ISO9141, ISO14230, KW1281, DS2, KW82 or Custom. |
setInitType(type) |
Init_5Baud, Init_Fast, Init_Ping or Init_None. |
setInitAddress(address) |
Address sent during the 5-baud init (0x33 for generic OBD-II). |
connect() |
Run the handshake; returns true when the ECU answers. |
isConnected() |
Whether the link is still alive. |
getConnectedProtocol() |
The protocol that actually answered. |
setDebug(stream) |
Print every byte on the bus to any Stream. |
Standard OBD-II diagnostics
| Method |
Description |
getLiveData(pid) |
Mode 01 value, converted to engineering units. |
getFreezeFrame(pid) |
Mode 02 value from the stored snapshot. |
readStoredDTCs() / getStoredDTC(i) |
Read and retrieve stored trouble codes. |
readPendingDTCs() / getPendingDTC(i) |
Read and retrieve pending trouble codes. |
clearDTCs() |
Clear trouble codes and reset the MIL. |
getVehicleInfo(pid) |
VIN (0x02), calibration ID (0x04), calibration verification number (0x06). |
readSupportedLiveData() / getSupportedData(mode, i) |
Scan which PIDs the ECU supports. |
Low-level access
| Method |
Description |
writeData(data) |
Send data bytes; the library adds header, length byte and checksum. |
writeRawData(data, length, checksum) |
Send a packet as is and append only the requested checksum. |
sendBytes(data, length) |
Put bytes on the bus exactly as given. |
readData() |
Read the response; returns its length. |
getResultBuffer() / getResultLength() |
Access the raw response. |
setHeader(), setLengthMode(), setChecksumType() |
Define a custom packet format. |
setP1Time() … setP4Time(), setWakeUpDelay() |
Adjust the protocol timings. |
🔌 Wiring
K-Line is a single-wire, 12 V bus and cannot be connected directly to a microcontroller pin. Each of these circuits does the level shifting; pick the one that suits your project.
| OBD-II pin |
Signal |
| 7 |
K-Line |
| 16 |
Battery +12 V |