Issues with retrieving values from OBD-II connector

I made a post a while ago about a project I have been working on using a Freematics OBD-II UART Adapter V2.1, an Arduino UNO and a Adafruit 1.14 TFT display. the code I was assisted with works quite well, I'm trying to add some more commands/ different commands specifically voltage and VIN retrieval. I used the library properties to try and assist me in getting the code working (library properties linked here) ArduinoOBD/libraries/OBD2UART/OBD2UART.h at master · stanleyhuangyc/ArduinoOBD · GitHub . both commands will display only zeros, there is a chance with the voltage, that the reason I'm getting a zero value is because I had to modify the OBD connector in a way where it dose not get power from itself so it is not always powered on in the vehicle, but the VIN should work if I knew what I was doing incorrectly. I'm also not sure if I can display a decimal with the "char buffer" command. any assistance would be much appreciated.

// HMD G1 BLOCK 2 code

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <OBD2UART.h>

COBD obd;

  #define TFT_CS        10
  #define TFT_RST        9 // Or set to -1 and connect to Arduino RESET pin
  #define TFT_DC         8

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int MODE1 = 2; 
int KPH = 12; 
int LEDBACK1 = 9;
int potPin = A1;

char buffer[21];

void setup() {
  
delay(2000);
  pinMode(MODE1, INPUT);
  pinMode(KPH, INPUT);
   pinMode(LEDBACK1,OUTPUT);
   Serial.begin(115200);
 
 tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
//void setup Master color 

 tft.init(135, 240); 
 tft.fillScreen(ST77XX_BLACK);
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30, 50);
  tft.print("MPH ");
  tft.print("---");


 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30,30 );
  tft.print("RPM ");
  tft.print("--00");


 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(130,30 );
  tft.print("|");
  tft.print("--- ");
  tft.print("%");
// Demo display 

delay(1000);
  obd.begin();
  while (!obd.init()); 
 tft.fillScreen(ST77XX_BLACK);
 
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(50, 40);
  tft.println("OBD CONNECTED");

 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(10, 60);

int valueVIN;
bool getVIN(valueVIN);
tft.print(valueVIN);
 
delay(4000);
// Connection to display 

  tft.fillScreen(ST77XX_BLACK);
// Clear display 
}

void loop() {
  
tft.setTextColor(ST77XX_WHITE,ST77XX_BLACK); //void loop master color 

  int potentiometerValue = 
  analogRead (potPin);
  int brightness = potentiometerValue / 4;
  analogWrite(LEDBACK1,brightness);
  // dimming control

  if (digitalRead(KPH) ==LOW){

   int value;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30, 50);
  tft.print("MPH ");

  if (obd.readPID(PID_SPEED, value)){
  
  float a = value;
float b = 1.609344;
int c = 0;
c = a / b;
  
  sprintf(buffer, "%3i", c);
  tft.print(buffer);
   }
  }
// MPH
else{   
   int valueKPH;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30, 50);
  tft.print("KPH ");

  if (obd.readPID(PID_SPEED, valueKPH)){
  sprintf(buffer, "%3i", valueKPH );
  tft.print(buffer);
  }
}
// KPH
// speed control 

int value2;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30,30 );
  tft.print("RPM ");
  
  if (obd.readPID(PID_RPM, value2)) {
    value2-=value2 % 100;
    sprintf(buffer, "%4i", value2);
 tft.print(buffer);
  }
// RPM control

int value3;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(130,30 );
  tft.print("|");
  
  if (obd.readPID(PID_RELATIVE_THROTTLE_POS, value3)) {
        sprintf(buffer, "%3i", value3);
 tft.print(buffer);
  }
tft.print("%");

if (digitalRead(MODE1) ==HIGH){
// Throttle pos control

int valueFUEL;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(130,50 );
  tft.print("|");
  
  if (obd.readPID(PID_FUEL_LEVEL, valueFUEL)) {
             sprintf(buffer, "%3i", valueFUEL);
 tft.print(buffer);
  }
  tft.print("%");


int valueTEMP;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(130,70 );
  
  if (obd.readPID(PID_COOLANT_TEMP, valueTEMP)) {
             sprintf(buffer, "%3i", valueTEMP);
 tft.print(buffer);
  }
  tft.print("C");

int valueVOLT;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30,70 );
  
   float getVoltage(valueVOLT);
             sprintf(buffer, "%2i", valueVOLT);
 tft.print(buffer);
  
  tft.print("V");
}
// Info mode set 

else{
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(130,50 );
  tft.print("          ");
 tft.setCursor(130,70 );
  tft.print("          ");
 tft.setCursor(30,70 );
  tft.print("          ");


}

}
// Normal mode set 

What are you asking? You can use the A/D with a resistor divider and get voltages, dependent on where you connect them.

There is a command to retrieve voltage values from the OBD adapter itself, although I suspect I'm miss using it, as it just displays a value of zero, here is the code I have tried to use:

int valueVOLT;
 tft.setRotation(3);
 tft.setTextSize(2);
 tft.setCursor(30,70 );
  
   float getVoltage(valueVOLT);
             sprintf(buffer, "%2i", valueVOLT);
 tft.print(buffer);
  
  tft.print("V");

This is the command from the library I have tried using:

virtual float getVoltage();

Same with the VIN command only displaying zero instead of the VIN. here is the library command:

virtual bool getVIN(char* buffer, byte bufsize);

First of all you should format your code properly by using ctrl-t for autoformatting.

You should provide more and more precise information

what is a library-property in this case?
what is a "command" in this case?

C++ only knows about functions

If you have modified the library it is not sufficient to post a link to the unmodified library on github. You will have to post the modified library here directly in the forum.

What do you expect this line of code to do?

I did not know about auto formatting, that helps. I am still pretty new to Arduino, C++, and coding as a whole, so i do apologize if I'm miss using terms and ideas. the library is unmodified, command is a function as you corrected me, I did not know this. As it appears I was missing using the term. I'm attempting to, with that line of code to get the VIN in this case as series of letters and numbers which I called a value which seems incorrect, than display this series of information onto the display. same with the voltage function get a value from the OBD adapter and than display it onto the display. with that line retrieving the information, and than displaying the information on to the display with this function.

tft.print(valueVIN);

And the others above

 tft.setRotation(3);
  tft.setTextSize(2);
  tft.setCursor(10, 60);

To position and size the information on the display.

if you want to expand the functionality you will have to learn quite some basics about c++-programming. Functions need to be defined.

I downloaded the source-code as the usual zip-file and tried to install it.
The arduino-IDE complaint "not a valid library-zip-file"
There are a lot of subdirectories in this github repository.

To reproduce your compiling-process exact and 100% you will have to describe how you made your code compile.

I guess with your new added function the code did not compile at all.

You should use a texteditor that is able to search through files
and then do a search through all files of this OBD2-library to find examples where the getVIN-function is used.
Then you can see how to use the getVIN-function

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.