Turning CANFDuino into an OBDII Monitor & Datalogger

OBDII or “on board diagnostics” is a standard connection and communications protocol required by the EPA since 1996. Starting with all vehicles produced after 2008, CAN bus (ISO 15765) became the required communications hardware for OBDII. This means OBDII is a protocol that “sits on top of” CAN bus, that is CAN bus enables OBDII.

OBDII is a transmit/respond type of protocol, and it uses a form of message multiplexing. This means in order to receive data over OBDII from your vehicle something (CANFDuino) needs to ask for it, periodically. It also means that the same message ID is used to transmit and receive data with each message containing a sort of a “sub ID” for both the request and response message. These sub ID’s are called parameter ID’s (PID’s) in the OBDII standard. So the transmit and receive CAN ID’s where the ask and response are sent are typically fixed (TX – 0x18DB33F1 or 0x7DF , RX – 0x18DAF10E or 0x7E8), the PID’s (data byte 3 in the payload) specific what you want to monitor (vehicle speed, throttle position, RPM etc.). A good list specifying the PID’s and more importantly all of the stuff you can monitor and log off of your car is shown here: https://en.wikipedia.org/wiki/OBD-II_PIDs.

The libraries for CANFDuino contain OBDII classes (OBD2.h, OBD2.cpp) that make adding different PID’s (things you want to monitor and log) very easy. You simply add a couple of lines of code for each new PID you want to monitor. The line below, is added to your sketch (*.ino file), this is a class constructor (C++) where you define the name, units, PID, size in bits, sign or unsigned number, current (real-time) data, slope in engineering units, offset in engineering units, the port number and if you are interfacing with an extended ID vehicle or standard ID vehicle (check forums for your vehicle or try both separately to see which works). If you are adding a new PID to the example code provided, when in doubt do this: pick a PID from the example code to study first like Speed or Throttle, then go to the PID wikipedia page above and look at how we arrived at units, PID, slope, and offset, then copy paste and modify. You’ll need to do one more tweak to the OBDII.h file to add the PID shown below.

***** DEFINITIONS FOR OBD MESSAGES ON CAN PORT 0, see https://en.wikipedia.org/wiki/OBD-II_PIDs to add your own ***************/
//char _name[10], char _units[10], OBD_PID pid,  uint8_t OBD_PID_SIZE size, bool _signed, OBD_MODE_REQ mode, float32 slope, float32 offset, cAcquireCAN *, extended ID;

cOBDParameter OBD_Speed(      "Speed "        , " KPH"    ,  SPEED       , _8BITS,   false,   CURRENT,  1,      0,  &CanPort0, false);

Below is snippet showing OBD2.h file PID’s, modify this if you wish to add a new PID.

/**
 * 
 * This enum represents the Parameter ID field for a particular signal per OBD2 protocol
 */
enum OBD_PID
{

	ENGINE_LOAD  = 0x04,
	COOLANT_TEMP = 0x05,
	ENGINE_RPM   = 0x0C,
	SPEED        = 0x0D,
	ENGINE_IAT   = 0x0F,
	ENGINE_MAF   = 0x10,
	THROTTLE_POS = 0x11,
	FUEL_FLOW    = 0x5E
};

Getting Started

Ok, we have covered some basics about OBDII and the code that is provided, the intention of this tutorial is to provide a quick method for getting OBDII vehicle communications and logging working with your CANFDuino.

Step #1: Build & Test Your First Sketch

Before we get started, lets make sure everything works. Follow the instructions under Installing the Library and First Sketch before trying anything with the vehicle.

Step #2: Wire up the OBDII port 

To connect the CANFDuino to your car, you’ll need to wire the unit to the OBDII port on your vehicle (must be 2008 or older to support CAN). You will need a vehicle interface cable (shown below) properly wired to CAN H and CAN L signals to the CANFDuino on port 0. This becomes easy with a flying leads cable shown below and a DB9 to screw terminal device as shown below. Follow the hookup guide to connect CANH, CANL and GND

144502_pinout
OBDII to flying leads cable
DB9 Screw terminals

A note on OBDII to DB9 female cables: these type of cables have no flying leads on the end, and instead go straight to DB9 great for mating to CANFDuino, right? Well, in theory yes if you buy a cable that has CANH mapped to pin 7, CANL mapped to pin 2, and GND to pin 3. Many OBDII to DB9 F cables have CANH mapped to pin 7, CANL mapped to pin 2 and GND mapped to pin 1. Please check the pinouts before you purchase a cable for OBDII or you may spend hours troubleshooting your setup.

Step #4: Format and Install SD Card

The OBDII sample code logs PID values to CSV format on the SD card of CANFDuino (note it will not function without the SD Card). This leverages the Arduino libraries for SD card support, as such, the card should be formatted to FAT16 or FAT32 per the reference library instructions here. Once completed, insert the SD into the CANFDuino slot.

Step #5: Program it

Open the IDE and go to File->Examples->”CANFDuino_OBD2Logger.ino”, note by default this code uses the standard 11bit ID’s which have been tested on Mazda, Toyota, and GM vehicles, the 29bit ID has been tested on Honda and is probably less common than 11bit. Make sure you have selected the proper board and port selected from Tools->Board->CANFDuino and Port->COMxx. Now click upload button, it will verify and upload the code. Once completed go to Tools->Serial Monitor and make sure the baud rate is 115K (for a cleaner static display use PUTTY or Chrome Terminal for real-time monitoring as we used in our CANTerm sketch example linked to in the readme). Plug in your OBDII cable in to your car (under drivers steering wheel, look very low), turn the vehicle on.

Step #6: Check Serial Monitor Messages

First, you will get a successful initialization message for the SD card. The board should be communicating to the car at this point. The easiest way to see that you are getting data is to watch the engine RPM message.

A successful monitoring from PuTTY will look something like this:

OBDII monitoring in PuTTY

Monitoring from the Arduino terminal will look like this:

 System Reset 
Initializing SD card…card initialized.
Engine Speed 0.00 RPM 
Throttle 0.00 % 
Coolant -40.00 C 
Speed 0.00 KPH 
Load 0.00 % 
MAF 0.00 grams/s 
IAT -40.00 C 
Engine Speed 862.50 RPM 
Throttle 16.08 % 
Coolant 59.00 C 
Speed 0.00 KPH 
Load 32.55 % 
MAF 4.17 grams/s 
IAT 25.00 C 
Engine Speed 865.50 RPM 
Throttle 16.08 % 
Coolant 59.00 C 
Speed 0.00 KPH 
Load 32.55 % 
MAF 4.10 grams/s 
IAT 25.00 C

Step #7 Check Logged Data

The OBDII data will be logged to a file called “OBD2data.csv” on the SD card. This format (comma separated value) is commonly viewed in programs like Excel, Google Sheets, notepad or any text viewer. Below is an example of a successful log. Note, if you plan to primarily use CANFDuino as a logger you may want to consider wiring the boot loader bypass jumper per the hookup guide.

logged OBD2 data

Step #8 Modify It!

Add your own PID’s, change your logging rates, displays etc. Fork the repository and further develop the product!

Thank you for your purchase, we wish you success!