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.

11 thoughts on “Arduino ELM327 Library

  1. Thanks for the work on the code. I’m out here banging away on a gauge retrofit project meant for rod and custom applications. I’ll be doing some test/eval on your source code. If I find anything worthwhile in terms of improvement I’ll pass it along.

    Note: You may want to check permissions on Google Code. I’ve got editing rights which I don’t think is correct. Also, I may be missing something, but there wasn’t a convenient way to download your code in one shot.

    1. Hi Jake,

      Good luck with the project! I look forward to any feedback you have. You should be able to edit the wiki on google code and any improvements you can make are much appreciated.

      To download the complete source code, your best bet is to use a subversion client, on windows tortoiseSVN is a pretty good client. On Mac and Linux the default svn installation should be good to go.

      Hope that helps, and thanks for the interest.

  2. Hey David,

    Very interested in the Loguino code! Am assisting a mate who is currently working to get a salt flats mustang going, and I’m hoping to use Loguino to get some data this year to try to improve for next year…. But I just wanted to check a couple of quick things regarding logging, the main question that I have is log rate for GPS and the second is whether there would be any limit to how many barometric pressure sensors I could connect? hoping to get an idea on some localised pressures during the run to allow improved aero optimisation.

    Thanks

    Andy

    1. Hi Andrew, Sounds like a cool project.

      GPS Sampling rate is usually bound by how often the GPS outputs NMEA sentences, which is usually once a second, some GPS units can be configured to output more frequently.

      The next bottleneck will be how quickly you can output data, once you have it working, turning of serial output (If you are writing to SD card) and turning off any modules that are not required will give you better performance, but usually it’s limited by the speed of the SD module, which is pretty fast, you can expect into the low tens of hz for most cases.

      In terms of the maximum number of pressure sensors, there is no limit per se with Loguino, it will depend on the sensor type – if it uses analog input, then you are limited by the number of pins, if it’s I2C based then usually the number of addresses supported by the chip etc.

      What sensor do you plan to use?

  3. First of all, thank’s a lot for this library. It is perfect.

    I’m using it with Arduino IDE 1.0.6 and work’s perfect.
    But I tried to used it with the new IDE 1.6.0 ( and the previosly 1.5.7) and the result it’s not the same. It’s not working. I can compile the code, uploaded connect to the car but the result is not corectly.

    Can you give me some tip? What could be the problem with the new IDE? I would try to find the problem but I have to work in the car and it;s not near me…

    Thank’s!

  4. Hi

    I am very intrested in the Arduino ELM327 Library, Can you please help with a schematic of how you connected it all together

    Thanks

  5. Hi, Thanks for you library! It is what I’ve been looking for.
    I have a question. I am using HC-05 Bluetooth module connected to an ELM237 Bluetooth OBDII. The HC-05 Connects perfect with ELM237 everytime I turn on Arduino. So… Can I use this library with this? And… How can I define TX and RX pins? Because only shows in your code “Serial3” and maybe means that you are using an Arduino Mega, and I am using an Arduino UNO.

    Thanks in advance!
    Julio.

Leave a Reply

Your email address will not be published. Required fields are marked *