ok i did some tests, with below code it’s allways returns STOPPED no matter how long is the delay;
/*
* OBD-II-UART Quickstart Sketch
* Written by Ryan Owens for SparkFun Electronics 7/5/2011
* Updates for Arduino 1.0+ by Toni Klopfenstein
*
* Released under the 'beerware' license
* (Do what you want with the code, but if we ever meet then you buy me a beer)
*
* This sketch will grab RPM and Vehicle Speed data from a vehicle with an OBD port
* using the OBD-II-UART board from SparkFun electronics. The data will be displayed
* on a serial 16x2 LCD. See the tutorial at https://www.sparkfun.com/tutorials/294
* to learn how to hook up the hardware:
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Create an instance of the new soft serial library to control the serial LCD
//Note, digital pin 3 of the Arduino should be connected to Rx of the serial LCD.
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;
//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;
void setup(){
//Both the Serial LCD and the OBD-II-UART use 9600 bps.
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.clear();
//Put the speed header on the first row.
lcd.setCursor(0,0);
lcd.print("S: ");
//Put the RPM header on the second row.
lcd.setCursor(0,1);
lcd.print("R: ");
//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 before we begin.
Serial.flush();
}
void loop(){
//Delete any data that may be in the serial port before we begin.
Serial.flush();
//Set the cursor in the position where we want the speed data.
lcd.setCursor(7,0);
//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();
Serial.print("S : ");
Serial.println(rxData[6]);
//Convert the string data to an integer
vehicleSpeed = strtol(&rxData[6],0,16);
//Print the speed data to the lcd
lcd.print(vehicleSpeed);
lcd.print(" km/h");
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,1);
//Clear the old RPM data, and then move the cursor position back.
//Query the OBD-II-UART for the Vehicle rpm
Serial.println("010C");
//Get the response from the OBD-II-UART board
getResponse();
getResponse();
Serial.print("R : ");
Serial.println(rxData);
//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);
//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;
}
}
}
}
With this code it reads RPM without any problem, but it requires 3 secodns delay and it’s dead slow, for example ;
started engine->hold idle for 10 seconds->push throttle to hold rpm arround 2.000->lcd shows 2.000 after like 10 seconds, it shows iddle rpm for 9-10 seconds than shows 2.000 rpm and when you stop hold to throttle it takes like 10 seconds to go back to idle rpm.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
char rxData[20];
char rxIndex=0;
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print("ATZ->");
Serial.println("ATZ");
getResponse();
getResponse();
lcd.setCursor(0,1);
lcd.print(rxData);
delay(3000);
Serial.flush();
lcd.clear();
//lcd.setCursor(0,1);
//lcd.print("010C->");
}
void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("010C->");
Serial.println("010C");
getResponse();
getResponse();
lcd.setCursor(0,1);
lcd.print(((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4);
delay(3000);
lcd.clear();
}
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;
}
}
}
}
if you shorten the delay (i’ve tried 1000-1500-2000-2500) it returns “STOPPED” no matter what you do.
i also have this ELM327 bluetooth diagnostic tool https://www.ebay.com/c/1038655933 when i connect to OBDII port of my car and open it’s mobile app it shows runtime rpm with much shorter delay like 1 second delay.
On the other hand, i wrote a simple program with delphi to send AT commands through comport via usb tfdi adapter with 1 second delay, when i run the program it reads runtime rpm or other values without any problem (through your obd board), things start to fail when i use arduino and it drives me crazy, i’ve tried 3 different arduino UNO but result was the same, if the problem is car or car’s protocol then how does it work on pc with the same obd board of yours?(i’m not angry, i just don’t understand )
What could cause this problem and is there any fix for this delay?