For anyone who would like to interface with their car, here is a guide to do so.
For this you need a few things:
- A car supporting OBD2 (most cars since 1996 have this).
- A laptop/computer running Python 2.7.3
- An OBD connector (available online – I have an ELM327 bluetooth one which cost under €10)
So now that you have those, lets get started:
Connecting to the OBD2 connector:
Whether you are using a Bluetooth or USB OBD2 connection, it is a serial connection. To talk to it through python you can use pyserial.
You can list all serial ports on OSX or Linux by typing the following in the terminal:
ls /dev/tty*
(“ls” above is short for “list the following with file name” and the “/dev/tty” is the start of the file path of any serial port).
Take note of the serial device you are using – you will need it below.
- Download pyserial and install on your machine.
- Open the terminal (command prompt on Windows).
- Type
python
to start Python. - Type
import serial
to make use of Pyserial - Now connect to the serial device. To do this you will need the name of the serial device you are using (see above). Type the following:
ser = serial.Serial('NAME_OF_SERIAL_DEVICE', 38400, timeout=1)
- Now you’ve connected to the OBD so you can start sending commands. For example, to measure speed type:
ser.write("01 0D \r")
A full list of OBD commands can be obtained here. - The elm327 device returns values in HEX. To read the value you just requested in Python type
speed_hex = ser.readline().split(' ')
- Convert the HEX to decimal by using:
speed = float(int('0x'+speed_hex[3], 0 ))
- Finally output to the screen:
print 'Speed: ', speed, 'km/h'
Now you should see the speed of your vehicle appear on screen.
Any issues/questions shout below in the comments and I will try to help out!
My next project is to get all of this onto a Raspberry Pi so I can monitor the car’s ECU on the go.
Perfect very helpful. I am actually working on the same project. Trying to use pyODB2 library but it was over complicating things.
Hi there. Any news about your project using the raspberry pi? I would be very interested how can raspberry comunicate with a bluetooth elm327 using python.
I can point you in my direction. http://kd8bny.blogspot.com
I have been trying to get this to work on my motorcycle. Any command I send just gets echoed back when I use readline(). From what i have read elsewhere this means that the ECU did not understand the command. Any suggestions?
Is an “ERR” Echoed back also?
Interesting. How do you interface with a com port in windows? My reader connects to com3, for example.
Pyserial works perfectly fine on windos too. Just enter ‘COM3’ as the NAME_OF_SERIAL_DEVICE.
The information seems to be sided with reading information from the ECM, is there anything for sending codes, such as initiating a crankshaft position relearn ?
I have elm327 wifi device. How can I connect to it? NAME_OF_SERIAL_DEVICE???
Similar question to Jim Killick, can I use this to set my Throttler body alignment?
This is for a 2000 1.8T passat.
I have wifi OBD2 device too. How can I connect to it? NAME_OF_SERIAL_DEVICE???
i don’t understand, if i have a Bluetooth device, i should write below instruction in window command? but it does not work.
ser = serial.Serial(‘COM3’, 38400, timeout=1)