Sparkfun OBD2/UNO sketch not working

Hi,

I have the older sparkfun OBD2 UART board and have modified the example code to work with a non serial two line LCD display for testing. From searching the forum somone else had the same ‘832’ issue, but it wasn’t solved.

I’m printing to the LCD ok, but can’t get a sensible RPM figure back from the car, only “832”

This is my first inroad into OBD2, and I guess I’m doing something stupid, but what?

(using a UNO, with serial to OBD2 board on pins 0,1)

// include the library code:
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//This is a character buffer that will store the data from the serial port
char rxData[20]; 
byte  rxIndex = 0;

//Variables to hold the speed and RPM data.
int vehicleSpeed = 0;
int vehicleRPM = 0;

//*********************************************
void setup() {
  lcd.begin(16, 2);
  // OBD-II-UART use 9600 bps.
  Serial.begin(9600);

  //Put the speed header on the first row.
  lcd.setCursor(0, 1);//column, line
  lcd.print("Speed: ");

  //Put the RPM header on the second row.
  lcd.setCursor(0, 0); //column, line
  lcd.print("RPM: ");

  //Wait for a little while before sending the reset command to the OBD-II-UART
  delay(1500);
  //Reset the OBD-II-UART
  Serial.println("ATZ");
  //Wait for a bit before starting to send commands after the reset.
  delay(2000);

  //Delete any data that may be in the serial port.
  Serial.flush();
}
//********************************************
void loop()
{
  //Delete any data that may be in the serial port before we begin.
  Serial.flush();

  //Query the OBD-II-UART for the Vehicle Speed
  Serial.println("010D");
  //Get the response from the OBD-II-UART board. We get two responses
  //because the OBD-II-UART echoes the command that is sent.
  //We want the data in the second response.
  getResponse();
  getResponse();
  //Convert the string data to an integer
  vehicleSpeed = strtol(&rxData[6], 0, 16);
  //Print the speed data to the lcd
  lcd.setCursor(6, 1);
  lcd.print(vehicleSpeed);
  lcd.print(" km/h");
   rxIndex = 0; // in case a return is missed in 'getresponse()
  delay(100);

  //Delete any data that may be left over in the serial port.
  Serial.flush();
  //Move the serial cursor to the position where we want the RPM data.
  lcd.setCursor(6, 0);

  //Query the OBD-II-UART for the Vehicle rpm
  Serial.println("010C");
  //Get the response from the OBD-II-UART board
  getResponse();
  getResponse();
  //Convert the string data to an integer
  //NOTE: RPM data is two bytes long, and delivered in 1/4 RPM from the OBD-II-UART
  vehicleRPM = ((strtol(&rxData[6], 0, 16) * 256) + strtol(&rxData[9], 0, 16)) / 4;
  //Print the rpm data to the lcd

  lcd.print(vehicleRPM);
  rxIndex = 0; // in case a return is missed in 'getresponse()' 
  //Give the OBD bus a rest
  delay(100);

}
//*******************************************************
//The getResponse function collects incoming data from the UART into the rxData buffer
// and only exits when a carriage return character is seen. Once the carriage return
// string is detected, the rxData buffer is null terminated (so we can treat it as a string)
// and the rxData index is reset to 0 so that the next string can be copied.
void getResponse(void)
{
  char inChar = 0;
  //Keep reading characters until we get a carriage return
  while (inChar != '\r')
  {
    //If a character comes in on the serial port, we need to act on it.
    if (Serial.available() > 0)
    {
      //Start by checking if we've received the end of message character ('\r').
      if (Serial.peek() == '\r')
      {
        //Clear the Serial buffer
        inChar = Serial.read();
        //Put the end of string character on our data string
        rxData[rxIndex] = '\0';
        //Reset the buffer index so that the next character goes back at the beginning of the string.
        rxIndex = 0;
      }
      //If we didn't get the end of message character, just add the new character to the string.
      else
      {
        //Get the new character from the Serial port.
        inChar = Serial.read();
        //Add the new character to the string, and increment the index variable.
        rxData[rxIndex++] = inChar;
      }
    }
  }
}

Just found out "832" is maybe the STOPPED message from the ECU ?, and so it might be the format of my send command.

I tried changing println("010D") to print("010D\r") but still no go :(

  //Delete any data that may be in the serial port.
  Serial.flush();

That does not clear the input buffer (although it may have in the early Arduino versions). To clear the input buffer, use:

  while (Serial.available()) Serial.read();

thx John I''ll give it a go !

  • yes now sorted ! thank you again.