How to interface with your car’s ECU through OBD2 and Python

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.

  1. Download pyserial and install on your machine.
  2. Open the terminal (command prompt on Windows).
  3. Type python to start Python.
  4. Type import serial to make use of Pyserial
  5. 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)
  6. 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.
  7. The elm327 device returns values in HEX. To read the value you just requested in Python type speed_hex = ser.readline().split(' ')
  8. Convert the HEX to decimal by using: speed = float(int('0x'+speed_hex[3], 0 ))
  9. 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.

12 thoughts on “How to interface with your car’s ECU through OBD2 and Python”

  1. 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.

  2. 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?

  3. 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 ?

  4. Pingback: DIY Boost Gauge
  5. 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)

Leave a Reply to Daryl Cancel reply