Building a J1939 CAN bus display using an ESP32 and an MCP2515 module is a fantastic project! It involves multiple layers of hardware and software, from low-level SPI communication to high-level J1939 protocol parsing and display rendering.
Here's a comprehensive guide to help you get started:
Project Overview
Goal: Create a display that reads J1939 messages from a CAN bus (e.g., from a truck, tractor, or industrial equipment) and shows relevant data like engine RPM, coolant temperature, fuel level, etc., on a screen.
Key Components:
- ESP32 Development Board: The main microcontroller, providing Wi-Fi, Bluetooth (optional), and plenty of processing power.
- MCP2515 CAN Bus Module (with TJA1050/SN65HVD230 Transceiver): The CAN controller chip that handles the physical layer communication with the CAN bus. It communicates with the ESP32 via SPI. Most readily available modules include the transceiver.
- Display:
- OLED (e.g., SSD1306 0.96"/1.3"): Simple, low power, good for basic text/numbers. Connects via I2C or SPI.
- TFT LCD (e.g., ST7735 1.8", ILI9341 2.4"/2.8"): Color, larger, good for graphics and multiple data points. Connects via SPI.
- Touchscreen LCD (e.g., ILI9341 with XPT2046): Adds user interaction.
- CAN Bus Connection:
- OBD-II Connector: If connecting to a vehicle with an OBD-II port (pins 6 and 14 for CAN High/Low).
- DB9 Connector: Common for industrial/heavy-duty applications.
- Bare Wires: If connecting directly to a CAN bus.
- Power Supply: A 5V power supply for the ESP32 (e.g., USB charger).
Hardware Setup
1. Components & Wiring Diagram
ESP32 Dev Board (e.g., ESP32-WROOM-32)
| ESP32 Pin Name | ESP32 GPIO Pin (Example) | Function |
|---|
| VSPI_SCK | GPIO18 | SPI Clock |
| VSPI_MISO | GPIO19 | SPI MISO |
| VSPI_MOSI | GPIO23 | SPI MOSI |
| GND | GND | Ground |
| 3V3 | 3V3 | Power (for display, if needed) |
| Any Digital Pin | GPIO5 (Recommended) | CS (Chip Select for MCP2515) |
| Any Digital Pin | GPIO2 (Recommended) | INT (Interrupt for MCP2515) |
Note: The default SPI pins for ESP32 are GPIO18, GPIO19, GPIO23. You can use other pins by explicitly defining them in your code if your ESP32 board has different defaults or you prefer other pins.
MCP2515 Module
| MCP2515 Pin Name | Connection to ESP32 | Function |
|---|
| SCK | ESP32 GPIO18 (VSPI_SCK) | SPI Clock |
| SI (MOSI) | ESP32 GPIO23 (VSPI_MOSI) | SPI Master Out Slave In |
| SO (MISO) | ESP32 GPIO19 (VSPI_MISO) | SPI Master In Slave Out |
| CS | ESP32 GPIO5 (or custom) | Chip Select |
| INT | ESP32 GPIO2 (or custom) | Interrupt (optional but recommended) |
| VCC | ESP32 5V (or external 5V) | Power (MCP2515 usually prefers 5V) |
| GND | ESP32 GND | Ground |
| CANH | CAN Bus High (e.g., OBD-II Pin 6) | CAN High Line |
| CANL | CAN Bus Low (e.g., OBD-II Pin 14) | CAN Low Line |
Display (Example: ILI9341 TFT LCD)
| Display Pin Name | Connection to ESP32 | Function |
|---|
| SCK | ESP32 GPIO18 (VSPI_SCK) | SPI Clock |
| MOSI | ESP32 GPIO23 (VSPI_MOSI) | SPI Master Out Slave In |
| MISO | ESP32 GPIO19 (VSPI_MISO) | SPI Master In Slave Out |
| CS | ESP32 GPIO15 (or custom) | Chip Select |
| DC (Data/Command) | ESP32 GPIO4 (or custom) | Data/Command Select |
| RST (Reset) | ESP32 GPIO16 (or custom) | Reset |
| VCC | ESP32 3V3 or 5V (check display spec) | Power (usually 3.3V) |
| GND | ESP32 GND | Ground |
Note: For OLED displays, the wiring will differ (often I2C: SDA, SCL, VCC, GND).
2. Physical Connection Tips
- Breadboard: Use a breadboard for initial prototyping.
- Power: Ensure your ESP32 can provide enough current for both the MCP2515 and the display. An external 5V power supply connected directly to the ESP32's 5V pin or USB port is usually sufficient.
- CAN Bus Termination: If your project is the only device on a small CAN bus segment, ensure there are 120-ohm resistors at both ends of the bus. Most vehicles/machinery already have proper termination.
- Shielded Cables: For CAN bus connections, use twisted-pair shielded cables to minimize noise.
Software Setup
1. Arduino IDE Setup
- Install Arduino IDE: Download from arduino.cc.
- Add ESP32 Board Manager:
- Go to
File > Preferences.
- In "Additional Boards Manager URLs," add:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- Go to
Tools > Board > Boards Manager...
- Search for "esp32" and install "esp32 by Espressif Systems".
- Select Your Board: Go to
Tools > Board > ESP32 Arduino and choose your specific ESP32 board (e.g., "ESP32 Dev Module").
2. Install Libraries
You'll need the following libraries. Go to Sketch > Include Library > Manage Libraries... and search for them:
- For MCP2515:
mcp_can_lib by Seeed Studio (or CAN_BUS_Shield if that's what you find first and it works with MCP2515). This library handles the SPI communication with the MCP2515 chip.
- For Display (choose one based on your display type):
- OLED (SSD1306):
Adafruit SSD1306 and Adafruit GFX Library.
- TFT (ILI9341, ST7735, etc.):
Adafruit ILI9341 (or specific display library) and Adafruit GFX Library. A powerful alternative is TFT_eSPI by Bodmer, which is highly optimized for ESP32 and supports many drivers.
J1939 Protocol Basics (Crucial for Parsing)
J1939 is an application layer protocol that runs over CAN. It defines how data is structured, communicated, and interpreted. Unlike raw CAN, where IDs are arbitrary, J1939 IDs have a specific format.
A J1939 CAN ID (29-bit extended ID) typically contains:
Priority (3 bits) | Reserved (1 bit) | Data Page (1 bit) | PDU Format (8 bits) | PDU Specific (8 bits) | Source Address (8 bits)
- Priority (P): 0-7, lower value means higher priority.
- Data Page (DP): 0 or 1, extends the PGN range.