Arduino ELM327 Library

The ELM327 IC is a multi-function OBD2 Interpreter that can be used to query information about most relatively new vehicles.  The ELM327 connects to the vehicle using the OBD2 port, and provides an RS232 Interface.   I recently purchased an OBD2 shield form SparkFun which uses an ELM clone, the STN1110.  I wanted to add OBD2 support to Loguino, but first I needed to be able to communicate using the ELM protocol.

Subsequently I created a the ELM327 class, which contains both high level and low level methods to get information from the controller.  The low level class is used to send arbitrary commands to the ELM chip, there are two main methods,

  • getBytes which requests a specific PID and parses the returned bytes into an array of values.
  • runCommand which sends an arbitrary command to the ELM device, and parses the output into a buffer which contains the response from the ELM.

The high level class provides methods to gather metrics from the controller, each method parses the response from the ELM device into the actual value.  The following code illustrates how to use the class:

#define ELM_TIMEOUT 9000
#define ELM_BAUD_RATE 9600
#define ELM_PORT Serial3

byte status;
Elm327 Elm;
status=Elm.begin();
if (status != ELM_SUCCESS){
    Serial.print("Error: ");
    Serial.println(status);
}
int temp;
Serial.print("OBD2 Coolant Temperature: ");
status=Elm.coolantTemperature(temp);
if (status  == ELM_SUCCESS)
{
    Serial.println ("Pass");
    Serial.print(" Value: ");
    Serial.println(i);
}else{   
    Serial.print("Error: ");
    Serial.println(status);
}

byte values[2];
status=getBytes("01","41","0C",values,2);
if (status == ELM_SUCCESS){
    Serial.print("Elm returned: ");
    Serial.print(values[0], HEX);
    Serial.print(" ");
    Serial.println(values[1], HEX);
    Serial.print("Which is: ");
    Serial.print(((values[0]*256)+values[1])/4, DEC);
    Serial.println(" RPM");
}else{   
    Serial.print("Error: ");
    Serial.println(status);
}

char data[20];
status=runCommand("ATI",data,20);
Serial.print("Current version is: ");
Serial.println(data);

Feature requests, bugs, suggestions and other feedback always appreciated.