OBD-II UART adapter intermittently works/fails

Hi,

I have an OBD-II UART adapter from Freematics OBD-II Adapter for Arduino | ArduinoDev.com (ver 1.2, B) that I'd like to use with my Diavolino in a 98 Honda.

I'm still waiting on my forum account to be approved at dfrobot.com, and the Arduino forum's always been a good starting point for advice.

I've downloaded the libraries recommended on their site, and am using a slightly altered version of their RPM_LED sketch (started tweaking random things after it didn't work consistently). The new sketch's goal is to take information from the OBD connection and print it to a separate serial connection.

The problems I've been running into feel really varied, and sometimes change between unplugging everything and giving it a fresh start, much less doing something in code. The most prevalent, however, follows this rough description:

  • I plug in the OBD UART adapter, which is wired to power the arduino, and connected to D0, and D1.
  • The arduino and UART turn on, because it's getting power from the car.
  • The arduino hangs while waiting on [obd.init()]
  • I unplug the serial connection (D0, D1) and then plug it back in after a few seconds.
  • I repeat unplugging/plugging until the arduino magically connects to the UART and starts displaying data, either by LED notifications or software serial.

If I get it working, figure something out in code, or wind up asking for help on dfrobot, I'll post back here.

Thanks for the help!

My sketch as it stands:

/*************************************************************************
* Sample sketch based on OBD-II library for Arduino
* Distributed under GPL v2.0
* Copyright (c) 2012-2013 Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
*************************************************************************/

#include <Arduino.h>
#include "OBD.h"
#include <SoftwareSerial.h>

COBD obd;

SoftwareSerial softSerial(10, 11); // RX, TX

void setup()
{
  // we'll use the debug LED as output
  pinMode(13, OUTPUT);  
  int pinState = 0;
  
  // start serial communication at the adapter defined baudrate
  obd.begin();
  
  softSerial.begin(9600);
  
  softSerial.println("Software Serial Init.");
  
  while(!softSerial.available())
  {
    softSerial.println("Waiting on confirmed connection...");
    delay(1000);
  }
  
  // initiate OBD-II connection until success
  softSerial.println("begin obd.init()");
  while (!obd.init())
  {
    // blink LED until obd.init() returns true?
    if (pinState == 0)
    {
      digitalWrite(13, HIGH);
      pinState = 1;
    }
    else
    {
      digitalWrite(13, LOW);
      pinState = 0;
    }   
  };  
  
  // alert me that the connection to obd is hopefully open
  softSerial.println("obd.init() passed");
  for (int c = 0; c < 20; c++)
  {
    if (2%c)
    {
      digitalWrite(13, HIGH);
    }
    else
    {
      digitalWrite(13, LOW);
    }
    delay(50);
  }
}

void loop()
{
  int value = 0;
  
  softSerial.println("\n\r\n\rTop of loop:");
  
  obd.readSensor(PID_RPM, value);

  digitalWrite(13, value > 1000 ? HIGH : LOW);

  softSerial.print("\n\rRPM: ");
  softSerial.print(value);
  
  obd.readSensor(PID_SPEED, value);
  
  softSerial.print("\tSPEED: ");
  softSerial.print(value);
  
  
  delay(400);
}

What, exactly, is connected to pins 10 and 11?

I've downloaded the libraries recommended on their site

On who's site?

I'm still waiting on my forum account to be approved at dfrobot.com

Good luck with that. Nearly useless company for support, from what i've observed.

Pins 10-11 connect to the Adafruit FTDI Friend's Tx-Rx, which is connected to a computer and viewed through GNU screen. I tested this setup with software serial separately and it seemed to work well.

FTDI friend: http://www.adafruit.com/products/284

Sorry, github page for OBD library: GitHub - stanleyhuangyc/ArduinoOBD: OBD-II library and sketches for Arduino

:-\ That's, um, disheartening. I was really betting on that forum, since just about every google search I make returns the sparkfun OBD project's troubleshooting instead.

Is it just as intermittent running only their example?

#include <OBD.h>

COBD obd;

void setup()
{
  pinMode(13, OUTPUT);
  obd.begin();
  while (!obd.init());
}

void loop()
{
  int value;
  if (obd.ReadSensor(PID_RPM, value)) 
  {
    // light pin13LED when RPM exceeds 1200
    // [changed to lower val for test]
    digitalWrite(13, value > 1200 ? HIGH : LOW);
  }
}

GO / NOGO:

#include <OBD.h>

COBD obd;

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);
  obd.begin();
  while (!obd.init());
}

void loop()
{
  digitalWrite(13,HIGH);
  delay(250);
  digitalWrite(13,LOW);
  delay(250);
}

It is; feels totally random for when it works or doesn't work. I lowered the LED value to 1000 RPM, so I could see when it was working by just tapping the gas pedal.

I'm going to run your second sketch, seems like a good direction to take troubleshooting, rather than add extra bits to the program like I did.

This may well not be a software problem. I think you may have a case of electronic interference from the engine and need to add some shielding for your cables and electronics.

Mark

Okay, since you kinda mentioned it, one of the sporadic issues is the arduino will connect, and then spit random characters for values, which seem to fluctuate as I rev the engine. It sounds juvenile, but maybe the alternator isn't super regulated?

Shielding as in wrap the cables in tinfoil, or as in solder in some capacitors and 7805 voltage regulators?


From http://www.dfrobot.com/forum/index.php?topic=806.msg3703#msg3703 on the dfrobot thread, I decided to do a quick test.

I ran a new sketch with the car OFF, and obd.init() worked fairly consistently. Then I measured the voltage on the power from the UART adapter, and it sat around 5.02-5.04 with the car off, on, and revving...I'm not sure if maybe the serial connection to the UART is being interfered with though, I've had nightmares from my last endeavor in debugging serial connection grounding issues..

The sketch to test obd.init:

/*************************************************************************
* Sample sketch based on OBD-II library for Arduino
* Distributed under GPL v2.0
* Copyright (c) 2012-2013 Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
*************************************************************************/

#include <Arduino.h>
#include "OBD.h"
#include <SoftwareSerial.h>

COBD obd;

SoftwareSerial softSerial(10, 11); // RX, TX

int pinState = 0;

void setup()
{ 
  // we'll use the debug LED as output
  pinMode(13, OUTPUT);
  
  // start serial communication at the adapter defined baudrate
  obd.begin();
  
  // start serial connection to FTDI friend
  softSerial.begin(9600);
  
  softSerial.println("Software Serial Init.");
  
  while(!softSerial.available())
  {
    softSerial.println("Waiting on confirmed connection...");
    delay(1000);
  }
  
  softSerial.read();
  
}

void loop()
{
  
  int value = 101;
  
  while(!softSerial.available())
  {
    softSerial.println("Check obd.init again? ");
    delay(1000);
  }
  
  softSerial.read();
  
  // initiate OBD-II connection until success
  softSerial.println("begin obd.init()");
  
  while (obd.init() != true)
  {
    softSerial.print("obd.init = [FALSE] \t");
    // blink LED until obd.init() returns true?
    if (pinState == 0)
    {
      digitalWrite(13, HIGH);
      pinState = 1;
    }
    else
    {
      digitalWrite(13, LOW);
      pinState = 0;
    }   
  };  
  
  
  // alert me that the connection to obd is hopefully open
  softSerial.print("\n\r\n\robd.init() passed\n\r\n\r");
  for (int c = 0; c < 20; c++)
  {
    if (2%c == 0)
    {
      digitalWrite(13, HIGH);
    }
    else
    {
      digitalWrite(13, LOW);
    }
    delay(50);
  }
  
  if(obd.readSensor(PID_RPM, value))
  {
    softSerial.print("\n\rRPM: ");
    softSerial.println(value);
  }
  else
  {
    softSerial.print("\n\rRPM: FAILED");
  }
  
}

Tin foil for the cables and (after an insulator ) around the arduino.

Mark

If it's noise from using the car's power (you'd think they would have designed this with those considerations in mind) you could just bring in GND and the COMM pins - and power the Arduino from the usual means (9V battery) - to troubleshoot this situation.

And, that way, you could hit Reset on the Arduino to see if that makes it come around.
If that worked, I'd look at putting a delay in setup() so that power etc could stabilise before proceeding.

Wired all of the grounds together, computer -> arduino -> UART. Then set the arduino to receive power from the laptop, which hangs around 5.23V on the multimeter. I left the positive lead from the UART free.

Pretty similar situation. Fails to connect until I fiddle with it and reboot the arduino enough times. Then it consistently connects (passes the obd.init() test) and fails to read RPM. If I spam it with characters so that it cycles more quickly, it will alternate between init pass / RPM fail and init pass / RPM pass (gets a reasonable value)...

I really don't know, if wrapping it in tin foil helps, then I wouldn't know how to complete my project with adequate shielding and end up with the system installed in my car.

hii,
i am new to arduino and getting uno r3 in few week

thanks to all for samples, libraries etc.

kindly look at the code and tell
does this code work and display speed and rpm.
Any suggestion is welcomed

#include <LiquidCrystal.h>
#include <Arduino.h>
#include <OBD.h>

COBD obd;

// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
lcd.begin(16, 2);

// start serial communication at the adapter defined baudrate
Serial.begin(DEFAULT_ADAPTER_BAUDRATE);

// initiate OBD-II connection until success
while (!obd.Init());
}

void loop()
{
int obdrpm;
int obdspeed;

obd.ReadSensor(PID_RPM, obdrpm);
// RPM is read and stored in 'obdrpm'

// Text RPM is printed
lcd.print("RPM");

lcd.setCursor(5, 0);

// OBD RPM is displayed in from of RPM
lcd.print(obdrpm);

obd.ReadSensor(PID_SPEED, obdspeed);
// Speed is read and stored in 'obdspeed'

lcd.setCursor(0, 1);

// Text Speed is printed in second line
lcd.print("Speed");

lcd.setCursor(7, 0);

// OBD Speed is displayed in from of Speed
lcd.print(obdspeed);
}

ngoyat,

When looking for help, you're going to have the best luck following the forum posting guidelines and creating a new topic.
How to forum: http://forum.arduino.cc/index.php/topic,148850.0.html

I don't have the Arduino IDE installed right now, nor do I have a copy of the OBD library. At a cursory glance, your code appears to at least have correct syntax. To test the system, I would personally split the screen and the OBD sensors. Have your code output dummy data to the screen using the same format to make sure it works. Then comment out the screen and have the arduino send back real sensor data over USB to verify it as well.

Again, this is the wrong place to post a question. Follow the link above, it provides a very usable guide to getting the help you're looking for on this site.

Good luck,
-BBX