Raspberry Pi OBD2 Data Logger: Build Your Own Vehicle Performance Monitor

Introduction

For car enthusiasts and DIYers, understanding a vehicle’s performance data is crucial for optimizing driving and vehicle maintenance. Whether you’re hitting the track for autocross or simply want to monitor your daily commute, access to real-time and logged vehicle data can provide invaluable insights. Modern cars, typically from the 1990s onwards, are equipped with On-Board Diagnostics II (OBD2) systems, offering a wealth of sensor data from the engine, transmission, and more. While advanced vehicles like the Corvette and Porsche offer integrated performance data recording systems, these often come at a premium price. Aftermarket solutions exist, but can also be expensive.

Alt text: Integrated OEM performance data logger in a modern car, showing a screen displaying telemetry and potentially camera footage.

The 2020 C8 Corvette’s Performance Data Recorder, for instance, adds nearly $2,000 to the vehicle’s cost. Racelogic’s Vbox and Motec systems are renowned but also carry significant price tags. This article explores a cost-effective and customizable alternative: building your own Raspberry Pi Obd2 Data Logger.

This project, known as “Trackside Pi,” utilizes a Raspberry Pi to create an embedded system that logs and displays vehicle data alongside external sensor information and camera footage. By tapping into the vehicle’s OBD-II port via a Bluetooth transceiver and connecting to external sensors like GPS, accelerometer, and a camera, Trackside Pi offers a comprehensive data logging solution. The system displays real-time data on a PiTFT screen, records footage, and provides a web interface for reviewing logged data and video. This DIY approach offers a powerful, budget-friendly alternative to commercial data loggers, perfect for automotive enthusiasts seeking detailed vehicle insights without breaking the bank.

Design and Testing: Trackside Pi System Overview

System Architecture

The Trackside Pi system is designed for straightforward data acquisition and user interaction. As illustrated below, the core components include:

Alt text: System architecture diagram for Raspberry Pi OBD2 data logger, showing connections between Raspberry Pi, OBD2 interface, GPS, accelerometer, camera, PiTFT display and web interface.

  • Raspberry Pi: The central processing unit, responsible for data collection, processing, logging, and display.
  • OBD-II Bluetooth Adapter: Wireless communication interface with the vehicle’s OBD2 port to retrieve engine and vehicle parameters.
  • GPS Module: Provides location and speed data, enhancing performance analysis with geographical context.
  • Accelerometer: Measures acceleration forces, useful for analyzing braking, cornering, and acceleration performance.
  • Camera: Captures video footage synchronized with sensor data, offering visual context to driving sessions.
  • PiTFT Touchscreen Display: Provides a real-time user interface for data display and system control directly in the vehicle.
  • Push Buttons: Physical controls for navigating the user interface and initiating recording.
  • Web Interface: A Flask-based web server running on the Raspberry Pi, allowing users to review historical data and video logs via a web browser on a computer or smartphone.

This architecture ensures a modular and flexible system, allowing for future expansions and customizations.

Software and Data Flow

The Trackside Pi software is structured into two primary Python applications: app.py and server.py. These applications manage sensor data acquisition, user interface presentation, and data logging and serving.

Alt text: Data flow diagram for Raspberry Pi OBD2 data logger software, illustrating data acquisition from sensors, display on PiTFT, logging to files, and serving data through a web interface.

  • app.py: This application is the heart of the data logger, responsible for:
    • Sensor Data Acquisition: Reading data from the accelerometer, GPS, and OBD-II adapter.
    • PiTFT User Interface (UI): Displaying real-time sensor data and camera view on the PiTFT screen.
    • Data Logging: Recording sensor data and video footage to log files on the Raspberry Pi.
  • server.py: This application provides the web interface functionality:
    • Flask Web Server: Hosts a web application using the Flask framework.
    • Data Serving: Reads logged data files and presents them through a web interface, including data visualizations and video playback.

This software architecture ensures efficient data handling and provides user-friendly access to both real-time and historical performance data.

Sensor Communication and Integration

Accelerometer: Tracking G-Forces

For measuring acceleration, the Adafruit MMA8451 triple-axis accelerometer is employed. This sensor communicates with the Raspberry Pi via the I2C protocol.

Hardware Setup: The accelerometer is connected to the Raspberry Pi’s power, ground, SCL, and SDA pins. The sensor’s address pin is pulled high, setting it to its default I2C address.

Software Implementation:

  1. Enable I2C: Enable the I2C bus in the Raspberry Pi configuration menu and reboot.
  2. Install Adafruit CircuitPython Library: Install Adafruit’s CircuitPython library, which simplifies hardware interaction.
    sudo pip3 install adafruit-circuitpython-mma8451
  3. Code Adjustment for Reliability: Initially, the sensor readings were unreliable. Adding a 1-second delay after initializing the I2C device resolved this issue, allowing the sensor time to start transmitting data correctly.

The accelerometer data is logged at 400Hz, providing high-resolution acceleration data. Readings are converted to g-forces and aligned with the automotive standard coordinate system (X-forward, Z-downward).

GPS: Location and Speed Tracking

The Sparkfun GPS-13740 module is used for GPS data, communicating over the GPIO serial port.

Hardware Setup: Connect the GPS module’s power and TX pin to the Raspberry Pi’s 3.3V and RXD pins respectively.

Software Implementation:

  1. Disable Serial Interface: Disable the serial interface in raspi-config to prevent the OS from using the serial0 port for console communication.
  2. Serial Port Configuration: The GPS module is accessible on /dev/ttyS0, not /dev/ttyAMA0.
  3. NMEA-0183 Protocol Parsing: The GPS module outputs data in the NMEA-0183 protocol. The “pynmea2” Python library is used to parse this data.
    pip install pynmea2
  4. Data Acquisition: The pynmea2 library allows easy extraction of latitude, longitude, speed, and other GPS parameters. For Trackside Pi, latitude and longitude are logged at approximately 2Hz.

OBD-II Communication: Vehicle Telemetry

Communicating with the OBD-II port via Bluetooth presented several challenges, primarily due to the reliability of inexpensive OBD-II adapters. The Veepak OBDII scanner was initially used but proved problematic.

Hardware Setup: A Veepak OBDII Bluetooth scanner (or similar ELM327-based adapter) is plugged into the vehicle’s OBD-II port.

Software Implementation:

  1. Enable Bluetooth: Enable Bluetooth in raspi-config.
  2. Install Bluetooth Packages: Install the bluez package and the python-obd library.
    sudo pip3 install obd
  3. Bluetooth Pairing: Use bluetoothctl to scan, pair, and trust the OBD-II Bluetooth device. Disconnect after pairing to free it for serial communication.
  4. Serial-over-Bluetooth Connection: Use rfcomm to bind the OBD-II adapter’s MAC address to /dev/rfcomm0, creating a serial communication channel.
  5. Code Modification for Adapter Reliability: Initial attempts to read OBD-II data using the python-obd library resulted in errors, often related to timeouts and ECU connection failures. Debugging revealed that the Raspberry Pi was sending commands too quickly, before responses from the OBD-II adapter were received. To address this, a 50-millisecond delay was added to the .__send() function in the elm327.py script of the python-obd package. This delay allowed sufficient time for responses and stabilized communication.

Synchronous vs. Asynchronous Communication: The python-obd library supports both synchronous and asynchronous communication. Trackside Pi uses asynchronous communication to continuously monitor specified OBD channels and retrieve the latest readings. This approach provides a smoother user interface refresh rate compared to synchronous methods, which can be slower and more prone to errors due to blocking queries.

OBD-II Adapter Notes: The connection issues encountered with the Veepak adapter are likely due to counterfeit ELM327 chips, specifically version 1.5, which are known to have firmware problems. More recent ELM327 chips (version 2.2 and later) are recommended for more reliable performance.

User Interface Design and Functionality

The user interface (UI) is built using the Pygame library and features two primary views:

  • Camera View: Displays a live video stream from the connected camera.
  • Data View: Presents real-time sensor data readings.

Alt text: Diagram of the App.py UI structure in Raspberry Pi OBD2 data logger, showing event loop, sensor reading threads, UI updating, event handling, and data logging phases.

The data view organizes sensor data into three statuses:

  • Offline (Gray): Indicates a sensor is not currently connected or providing data.
  • Unlogged (White): Sensor data is being displayed but not currently set for logging.
  • Logged (Green): Sensor data is being displayed and will be recorded when logging is active.

Users can toggle the logging status of individual data points by touching them on the PiTFT screen.

Physical Controls: Three push buttons provide hardware control:

  1. Button 1: Switch to Camera View.
  2. Button 2: Switch to Data View.
  3. Button 3: Toggle recording status (start/stop data logging and video recording).

app.py Implementation Details:

  • Multithreading for Responsiveness: app.py utilizes Python threading to manage sensor communication and the PiTFT UI concurrently. This prevents UI freezes caused by time-consuming sensor data acquisition. Three iterations of UI implementation were tested, finally settling on a threaded approach for optimal responsiveness. Initially, sequential calls to sensor routines in the main loop caused UI unresponsiveness. Named pipes were then tried but still resulted in slow UI due to pipe I/O overhead. The final threaded implementation, using shared global variables for data exchange, provided a responsive UI by making sensor communication non-blocking.
  • Efficient UI Updating: The UI updates only when new sensor data is available, minimizing unnecessary screen refreshes.
  • Event Handling: app.py continuously monitors for touch screen input and button presses to handle user interactions, such as toggling data logging and switching views.
  • Data Logging Process: When recording is activated, app.py creates timestamped log files and saves selected sensor data. Video frames are saved as raw images and later converted to MP4 format using ffmpeg.

Web Interface for Data Review

The web interface, developed using the Python Flask framework, provides a convenient way to review logged data and video recordings.

Alt text: Screenshot of Raspberry Pi OBD2 data logger web interface homepage, listing saved recordings with dates and times.

Alt text: Screenshot of Raspberry Pi OBD2 data logger web interface data view, showing video playback and synchronized sensor data charts.

Alt text: Screenshot of Raspberry Pi OBD2 data logger web interface map view, displaying GPS track data on a map.

The web interface features two main pages:

  • Homepage: Lists all saved recordings, allowing users to select and view past sessions.
  • Detailed Recording Page: Displays comprehensive data for a selected recording, including:
    • Video Playback: Integrated video player to watch synchronized driving footage.
    • GPS Map: Visualizes the vehicle’s driven path on a map.
    • Data Charts: Line charts for all logged sensor data (OBD-II parameters, accelerometer, etc.), allowing for detailed performance analysis over time.

Trackside Pi Enclosure Design and Assembly

A custom enclosure for the Trackside Pi was designed using Autodesk Fusion 360, optimized for 3D printing.

Alt text: Internal view of 3D printed enclosure for Raspberry Pi OBD2 data logger, showing compartments for Raspberry Pi and sensor electronics.

The enclosure is a rectangular box with an internal dividing wall separating the Raspberry Pi compartment from the sensor electronics compartment.

Enclosure Features:

  • Raspberry Pi Mounting: The front compartment has bosses for mounting the Raspberry Pi with screws.
  • Cable Management: Openings in the dividing wall allow routing of the camera ribbon cable and GPIO cables to the rear compartment. Jumper cables were used instead of a wide GPIO breakout cable to avoid obscuring the USB power port.
  • Sensor Housing: The rear compartment houses a compact breadboard for the GPS and accelerometer modules, secured with double-sided tape.
  • Camera Mount: The rear lid includes mounting holes for the Raspberry Pi Camera Module.
  • 3D Printing Material: The case was printed in black PLA.

Assembly Process:

Assembly is designed to be straightforward:

  1. Mount the Raspberry Pi in the front compartment.
  2. Route camera and GPIO cables to the rear compartment.
  3. Connect jumper cables to the PiTFT and route them to the rear compartment.
  4. Attach the PiTFT.
  5. Mount the GPS and accelerometer on the breadboard in the rear compartment.
  6. Screw the camera module into the rear lid.
  7. Connect the camera module and snap the rear lid onto the enclosure.

A suction cup phone holder is modified to mount the Trackside Pi enclosure to the vehicle’s windshield or dashboard.

Alt text: Protoboard inside Raspberry Pi OBD2 data logger enclosure, showing accelerometer (blue module) and GPS (green module) connections.

Alt text: Rear view of Raspberry Pi OBD2 data logger enclosure with Raspberry Pi, sensors, and PiTFT installed, rear cap removed.

Alt text: Front view of Raspberry Pi OBD2 data logger enclosure without front cap, showing PiTFT screen and push buttons.

Alt text: Front view of Raspberry Pi OBD2 data logger enclosure in white PLA for better contrast, showing overall design.

Alt text: Rear view of fully assembled Raspberry Pi OBD2 data logger, ready for in-car use, breadboard not visible.

Results and Conclusion: A Versatile and Affordable Data Logger

Trackside Pi successfully delivers a versatile and cost-effective data logging and video recording solution for automotive enthusiasts. The system is self-contained, requiring only an OBD-II Bluetooth adapter, the Trackside Pi unit, and power from a car charger port. It is designed to bring data logging capabilities to older vehicles lacking integrated systems, and is compatible with OBD-II compliant cars (1996 and newer).

Alt text: Raspberry Pi OBD2 data logger “Trackside Pi” mounted inside a car on the dashboard, showing PiTFT screen displaying live data.

The system captures essential engine parameters, acceleration data, GPS location, and synchronized video footage, providing a comprehensive overview of vehicle performance. The high-refresh-rate accelerometer data allows for detailed analysis of braking, cornering, and acceleration. GPS logging adds location context and enables track mapping. Combined with OBD-II data and video recording, Trackside Pi offers a valuable tool for track days and performance analysis. Users can quickly review driving sessions with time-synchronized data, all from a compact and affordable system.

Limitations and Future Improvements:

While Trackside Pi is functional, there are areas for future development:

  • Audio Recording: Integrating audio recording was initially planned but faced hardware limitations with the Raspberry Pi’s audio input. Future work could explore USB microphones or software solutions.
  • Audio Playback via Bluetooth: Adding Bluetooth audio playback to the vehicle’s sound system could provide real-time driver feedback or audio cues.
  • Expanded OBD-II Data: Implementing multiple data logging pages to display a wider range of OBD-II channels would enhance data richness.
  • Improved OBD-II Logging Rate: Optimizing the OBD-II communication protocol and adapter for faster data acquisition is desirable.
  • Higher Video Frame Rate: Increasing the video capture frame rate beyond 13 FPS would improve video quality, requiring further optimization of processing resources.

Bill of Materials

Description Link Qty Price
Raspberry Pi Model 3 B 1 Not included in total
Raspberry Pi Camera Module 2.1 1 Not included in total
Bluetooth OBD II Scanner Amazon 1 $11.99
Sparkfun GPS GP-20U7 Mouser 1 $19.04
Adafruit Triple-Axis Accelerometer MMA8451 Mouser 1 $7.95
Suction Cup Phone Holder Amazon 1 $9.48
Bluetooth FM Transmitter and Car Charger Amazon 1 $17.95
Micro USB Cable Amazon 1 $5.26
M2 X 6mm Screws McMaster Carr 1 (pack of 100) $12.05
Total $83.72

Developers

Will Nuñez

[email protected]

Yan Zhang

[email protected]

Source Code

Github Repository

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *