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

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

void setup(){

Serial1.begin(9600); Serial.begin(9600); //This is for the serial debug

//Wait for a little while before sending the reset command to the OBD-II-UART delay(1500); //Reset the OBD-II-UART Serial1.print("ATWS\r"); //Wait for a bit before starting to send commands after the reset. delay(1000);

// turn echo off Serial1.print("ATE0\r");

// send 01 00 until we are connected do { Serial1.print("0100\r"); delay(1000); getResponse(); //This is only called once since the schoecho has been turned off using ATE0\r Serial.print("0100 Response: "); Serial.print(rxData[0]);Serial.print(rxData[1]);Serial.print(rxData[3]);Serial.print(rxData[4]);Serial.print(rxData[6]);Serial.print(rxData[7]); Serial.print(rxData[9]);Serial.print(rxData[10]); Serial.println(""); } while(ELM_CHECK_RESPONSE("0100", rxData)!=0);

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(Serial1.available() > 0){ //Start by checking if we've received the end of message character ('\r'). if(Serial1.peek() == '\r'){ //Clear the Serial buffer inChar=Serial1.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 = Serial1.read(); //Add the new character to the string, and increment the index variable. rxData[rxIndex++]=inChar; } } }}

My car ('02 Ford Focus) uses the J1850-PWM protocol and the code seems to initialize fine and returns the right protocol, but when i ask for live data, i just get some random numbers. Speed jumps between 0 and 13 km/h and rpm can go from 0 to 19 to 832 and sometimes even displays higher or negtive values, bot with and without the engine running.

I have spent a lot of time googling the problem and looking through the elm327 datasheet, but so far i haven't found anything that works.

I have also tried loading the OBDuino code, but that only seems to be working with the ISO-9141 protocol.

If anyone has any code or just pointers on how to get this to work with the J1850-PWM protocol i would be very happy to hear from you, as i'm starting to get a bit frustrated.

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

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

boolean gotResponse = false;

void setup(){

Serial1.begin(9600); Serial.begin(9600); //This is for the serial debug

//Wait for a little while before sending the reset command to the OBD-II-UART delay(1500); //Reset the OBD-II-UART Serial1.print("ATWS\r"); //Wait for a bit before starting to send commands after the reset. delay(1000);

// turn echo off Serial1.print("ATE0\r");

// send 01 00 until we are connected do { Serial1.print("0100\r"); delay(1000); getResponse(); //This is only called once since the echo has been turned off using ATE0\r Serial.print("0100 Response: "); Serial.print(rxData[0]);Serial.print(rxData[1]);Serial.print(rxData[3]);Serial.print(rxData[4]);Serial.print(rxData[6]);Serial.print(rxData[7]); Serial.print(rxData[9]);Serial.print(rxData[10]); Serial.println(""); } while(ELM_CHECK_RESPONSE("0100", rxData)!=0);

void printRpm(){ //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;

void loop(){ //Query the OBD-II-UART for the Vehicle Speed Serial1.print("010D\r"); delay(20); //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();

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(Serial1.available() > 0){ //Start by checking if we've received the end of message character ('\r'). if(Serial1.peek() == '\r'){ //Clear the Serial buffer inChar=Serial1.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; gotResponse = true; } //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 = Serial1.read(); //Add the new character to the string, and increment the index variable. rxData[rxIndex++]=inChar; } } }}

getResponse(); //This is only called once since the echo has been turned off using ATE0\r Serial.print("0100 Response: "); Serial.print(rxData[0]);Serial.print(rxData[1]);Serial.print(rxData[3]);Serial.print(rxData[4]);Serial.print(rxData[6]);Serial.print(rxData[7]); Serial.print(rxData[9]);Serial.print(rxData[10]); Serial.println("");So, now getResponse() sets a flag. Let's just ignore it... Why are we bothering to set the flag?

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(Serial1.available() > 0){ //Start by checking if we've received the end of message character ('\r'). if(Serial1.peek() == '\r'){ //Clear the Serial buffer inChar=Serial1.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 = Serial1.read(); //Add the new character to the string, and increment the index variable. rxData[rxIndex++]=inChar; } } }

Im kinda curious you say that its printing the response before it has been fully received. who is that possible, as far as i can see it has a while loop that is checking to see if the \r is received why is that not enough ? how is it escaping from that ? and why will it work to add a flag and another while loop that essentially does the same check?

do { Serial1.print("0100\r"); delay(1000); getResponse(); //This is only called once since the echo has been turned off using ATE0\r while(gotResponse == false){} Serial.print("0100 Response: "); Serial.print(rxData[0]);Serial.print(rxData[1]);Serial.print(rxData[3]);Serial.print(rxData[4]);Serial.print(rxData[6]);Serial.print(rxData[7]); Serial.print(rxData[9]);Serial.print(rxData[10]); Serial.println(""); } while(ELM_CHECK_RESPONSE("0100", rxData)!=0);