GRAPPLING WITH ELECTRONICS
  • Blog
  • About
  • Contact

Reading a car's OBDII port with a Raspberry Pi

5/4/2014

17 Comments

 
I have been planning for a while to try to get information from my car and to associate it to other sensors (e.g. a GPS and an accelerometer). The most natural thing would be to connect an OBD port scanner and a phone. There are several commercial tools of this type and you can find them for a few pounds online. However you will then be constrained to use only the phone's sensors while with the Pi you have free reins to choose the sensors. 

What is the OBD port and how to find it
The OBD (On Board Diagnostics) port is  used by garages to test the electronics of a car. It is mandatory on every car since the 90s. It is easily accessible and generally located under the steering wheel. In order to find the port in my car (a Mercedes Benz B Class), I just googled "OBD port Mercedes B class" and found several pages providing  instructions including pictures and even a couple of videos. 

Finding the OBD scanner
There are several cheap commercial readers available, I decided to go for a bluetooth enabled ELM327

Reading from the OBD scanner in Python
As I am working on a Raspberry PI I needed to develop in Python.  I have found the code developed by martinohanlon which is available on github and that is able to read from the OBD scanner. The code is created for a motorbike but it works for cars as wells. Also it was originally created for a wired scanner. However if you pair the scanner with the Pi as explained below, it will also work with bluetooth. 

Preparing the Pi
The Pi will need a bluetooth scanner. There are several commercial ones. I went for a USB one. Installing it is very easy. Just plug, update the libraries and play.  ModPy provides an excellent tutorial. I then connected a GPS to the pi. A brilliant tutorial is provided by @dmandle.

Pairing  the PI and the OBD scanner
As mentioned in the ModPy's tutorial, the bluetooth OBD adaptor needs pairing with the Pi. To get the address of the adaptor run in the Pi's shell:
#      hcitool scan
 it will return:
#      Scanning ...
#              <bluetooth address> OBDII
#       ...
The scanner will be called OBDII (or similar name). Its bluetooth address will be something like 00:0D:18:3A:67:89.
To pair it with the Pi, I used the following command:
#           echo 1234|bluez-simple-agent hci0 <bluetooth address>
Note: this supposes that the PIN code of the scanner is 1234, which is generally the case. If different, change appropriately.
If the Pi complains that bluez-simple-agent is not found, then go back to the ModPy's tutorial and install bluez (with sudo apt-get install bluetooth bluez-utils blueman).

In some adaptors the bluetooth connection drops after few seconds after pairing. if so,  in a separate shell run
#           sudo rfcomm connect 0 <bluetoothaddress> 1
e.g.  sudo rfcomm connect 0 00:0D:18:3A:67:89 1
The command will regularly poll the scanner so to avoid dropping the connection. Please note that the command will hang the shell, so add an '&' if you do not want that. 

Putting everything together
At this point I just had to make sure that every time the OBD port was polled, the GPS was polled as well. The results were saved as a csv log file. I then created a python script taking the csv file and turning it into a kml file as required by Google Earth. Here is an example of the result (speed is in miles per hour). 
 
Picture

17 Comments
Dominic Threlfall link
23/6/2014 03:26:37 am

Content of this blog is too helpful for mass people. Thanks for submitting this kind of  Blog.

Reply
domantylar link
4/7/2014 12:10:28 am

i have an obdmate om580 scanner ,always using

Reply
entreq grounding link
26/1/2015 10:08:10 pm

However if you pair the scanner with the Pi as explained below, it will also work with bluetooth.

Reply
Fabio Ciravegna
27/1/2015 02:30:38 am

Below?

Reply
Darren
26/8/2015 12:51:34 pm

Might I ask what GPS and accelerometer you used?

Reply
Zinc
26/9/2015 09:45:06 am

Has anyone gotten this to work with ODB-II Mode 8?

Reply
Neal Caffrey link
31/12/2015 10:05:33 am

I always liked the products that use Bluetooth. but, there are some bluetooth Phone không catch of OBD. it's very difficult when you have to find another phone to use it. so I suggest using wire products.

Reply
Christophe
31/1/2016 03:11:09 pm

Hello
I'm trying to use a Raspberry to read an agricultural tractor BUS CAN. It's the first time I use Python and I don't understand exactly how you manage to poll OBD and GPS at the same time. Did you publish your code somewhere ?
Thanks a lot for everything

Reply
Fabio Ciravegna
31/1/2016 04:47:00 pm

To be honest I have not seen the code in years. I have never published it because not of the quality it should be provided with.
I do not understand what the problem is. The GPS must run in a separate thread and so does the OBD reader. They will set fields in an object (one of GPS and one for OBD) that represent the last read value. At regular intervals (e.g. when the OBD values are available), you read the current GPS values and associate them to the OBD ones creating an object with fields from both OBD and GPS. You can find the code of the GPS by Googling around. Dan Mandle has written a good one http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/

Reply
Kevin
20/2/2016 02:25:22 am

The CAN protocol is not supported by all OBD scanners.

Reply
Benjamin Schubert
2/6/2016 02:25:40 pm

Wow this is a really cool idea. You could even do a "heat map" of sorts to see where the bad traffic spots are on your commute or something along those lines.

Reply
jose link
4/12/2016 02:34:43 pm

You could even do a "heat map"

Reply
MAS HELMI WEB ID link
6/12/2016 10:44:15 pm

wow raspberry :o so cool

Reply
Yadwinder Singh
6/4/2017 02:34:38 pm

awesome , helps alott

Reply
oneclicksoft.in link
12/10/2019 05:36:19 am

thank share information its very help full blog

Reply
oneclicksoft.in link
12/10/2019 06:06:26 am

Wow this is a really cool idea. thank share information its very help full blog

Reply
James link
19/10/2020 10:39:54 pm

Very nice info here, have you made any improvements over the years on this?

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Author

    After over 20 years in the UK at the University of Sheffield, I have moved to Università di Torino in Italy as (Full) Professor of Pervasive Computing.  
    My research concerns large scale data acquisition and analysis with a particular focus on mobile applications. I teach mobile computing and advanced Web technologies. 
    These pages are dedicated to random passions like electronics (e.g. using Raspberry Pi and Arduino) and to mobile development. Some of these experiments I do for work, others just for fun. 

    Archives

    October 2022
    May 2022
    November 2019
    January 2016
    April 2014
    January 2014
    December 2013

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.