Site Information

 Loading... Please wait...


J1939 Protocol Stack Sketch for ESP32 Using the Arduino IDE

Posted by Wilfried Voss on

ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module

The ESP32 is a series of low-cost, low-power system-on-chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations and includes built-in antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules.

Furthermore, the processor provides the means to easily and quickly create applications like CAN/J1939 gateways, CAN Bridges, J1939 ECUs, J1939 Data Logger, and many more. However, the Controller Area Network (CAN) port on the ESP32 provides only TTL levels, and you will need additional hardware (transceivers) to make them hardware-compliant with the CAN Bus standard ISO-11898.

For that reason, I am using our  ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module. This board comes not only with an onboard ESP32 WROOM-32 WiFi, Bluetooth Classic, BLE Module, but also a CAN Bus port with a transceiver.

CAN Bus Driver Software

Naturally, in order to implement an SAE J1939 protocol stack, you need to have a good CAN Bus driver software. I chose the  ESP32 Protocol Demo on However, I made some minor modifications for better usability under SAE J1939, most prominently the implementation of CAN Bus error detection. The modifications are:

  • Added CANError to:
    • CAN.c
    • CAN.h
    • ESP32CAN.cpp
    • ESP32CAN.h
  • CAN.c:
    • Renamed to CAN.cpp
    • CAN.cpp -> Corrected declaration of CAN_read_frame_phy
    • CAN.cpp -> CAN_isr -> Added cast to interrupt = MODULE_CAN->IR.U;

ARD1939 - SAE J1939 Protocol Stack for Arduino

The  ARD1939 SAE J1939 protocol stack for Arduino was originally developed for the Arduino Uno and Arduino Mega 2560. In the meantime, I added versions for the Arduino Due and the ESP32.

SAE J1939 ECU Programming & Vehicle Bus Simulation with ArduinoAll technical information about ARD1939, the SAE J1939 protocol stack for Arduino, plus detailed description of implementation and available function calls is included in my book SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino.

Arduino sketches (software projects) include a J1939 network scanner, and a simple SAE J1939 to USB Gateway application with associated Windows GUI (Visual Studio C# project). The collection of sketches is concluded by the ARD1939 project, a fully functional SAE J1939 protocol stack for the Arduino Uno and Mega 2560. And while the documentation refers to these processors, the protocol features apply to the ESP32 as well.

ARD1939 for ESP32

The ARD1939 code itself is directly compatible with the ESP32 when used with the Arduino IDE, however, the Arduino Due CAN Bus driver software required some modifications as discussed previously. Also, I needed to adjust the CAN Bus function calls to the same functionality as used for the Uno, Mega 2560, and Due version.

Please note: I had contemplated releasing ARD1939 in form of the original source code but ultimately decided against it, mostly out of respect for those small businesses that make a living from selling SAE J1939 devices and software tools. Instead, I provide a pre-compiled code.

Download the Software

Disclaimer: All Arduino sketches and other code samples and projects as introduced here are free software; you can redistribute and/or modify them. The programs are introduced in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. With downloading these programs, you confirm that these code samples and projects were created for demonstration and educational purpose only.

=>  SAE J1939 Protocol Stack for ESP32 (.zip file)...

Last, but not least, sorry, but there is no technical support for this software. After all, it is free-of-charge.

Further Resources

Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled DevicesElectronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices

Copperhill Technologies highly recommends using this book for your ESP32 projects. Yes, many good books and free online resources are available these days, but this is the book we are using. It made our approach to Bluetooth, BLE, and WIFI a breeze. Programming wireless applications without hassles was fun, and we will share them on this web page.

Projects throughout the book utilize the wireless functionality and processing power of the ESP microcontrollers. Projects are built in the Arduino IDE, so you don't need to download other programming software. In addition, mobile apps are now ubiquitous, making the app build projects of the book very relevant, as are the web page design projects.

In Electronics Projects with the ESP8266 and ESP32, you'll see how easy and practical it is to access information over the internet, develop web pages, build mobile apps to remotely control devices with speech recognition, or incorporate Google Maps in a GPS route tracking app. More Information...

SAE J1939 Diagnostic Device Includes a PGN, SPN Database

The handheld PCAN-Diag FD device from PEAK-System supports diagnosing communication on a CAN (FD) network. In addition, a separately available module extends the functionality to analyze SAE J1939 data traffic. The SAE J1939 Standard describes communication in utility vehicles via CAN Bus. The standard specifies messages and data for transmitting diagnostic and control information using PGNs (Parameter Group Numbers) and [...]

Read More »

SAE J1939: Simulation of Analog and Digital Signals with the JCOM1939 Monitor Software

The JCOM1939 Monitor Software is ideal for monitoring, recording, analyzing, and simulating SAE J1939 data traffic. The system works in combination with our SAE J1939 gateways. This comprehensive and easy-to-use, easy-to-understand Windows software displays not only SAE J1939 data traffic but also scans the network, simulates an ECU (incl. full node address negotiation features), and responds [...]

Read More »

A New Generation of SAE J1939 Gateway, ECU Simulator, and Starter Kit

Like many other businesses, we have to deal with the global shortage of electronic components. For instance, the NXP LPC 17xx processors we used for our SAE J1939 gateways and the starter kit are not available at this time. Even worse, there is no reliable information if/when production resumes. This situation forced us to rethink [...]

Read More »

SAE J1939 Data Acquisition System For Diesel Engines Measures And Records 23 Suspect Parameter Numbers (SPNs)

The Titan S8-CAN by Madgetech (USA) represents a data acquisition system for diesel engines that simultaneously measures and records 23 suspect parameter numbers (SPNs). The portable data logger suits engine, road, and diagnostic testing applications. It connects to an SAE J1939 network via the diagnostic port and supports the monitoring and recording of SAE J1939 messages between Tier 1 [...]

Read More »

Guide To SAE J1939 - Parameter Group Numbers (PGN)

The following is an excerpt from A Comprehensible Guide To J1939 by Wilfried Voss. SAE J1939 is a very ingeniously designed protocol that takes a resourceful advantage of the CAN 29-Bit message identifier. Rather than relying on a myriad of protocol functions, SAE J1939 uses predefined parameter tables, which keeps the actual protocol on a comprehensible level. However, these parameter tables [...]

Read More »

Guide To SAE J1939 - J1939 Message Format

The following is an excerpt from A Comprehensible Guide To J1939 by Wilfried Voss. The main document describing the J1939 message format is SAE J1939/21 – Data Link Layer. J1939/21 defines the use of the CAN data frame (29-bit identifier, Parameter Group Numbers – PGN, etc.) and the transport protocol functions, i.e. a definition of how messages longer than the standard [...]

Read More »

Guide To SAE J1939 - J1939 Characteristics

The following is an excerpt from A Comprehensible Guide To J1939 by Wilfried Voss. SAE J1939 is a higher-layer protocol based on Controller Area Network (CAN). It provides serial data communications between microprocessor systems (also called Electronic Control Units - ECU) in any kind of heavy-duty vehicles.Everything that has to do with CAN is based on maximum reliability with the [...]

Read More »

SAE J1939 Programming with Arduino - PGN Range

This post is part of a series about SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino. Parameters groups are, for instance, engine temperature, which includes coolant temperature, fuel temperature, oil temperature, etc. Parameter Groups and their numbers (PGN) are listed in SAE J1939 (roughly 300 pages) and defined in SAE J1939/71, a document containing roughly [...]

Read More »

SAE J1939 Programming with Arduino - Suspect Parameter Numbers (SPN)

This post is part of a series about SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino. A Suspect Parameter Number (SPN) is a number assigned by the SAE to a specific parameter within a parameter group. It describes the parameter in detail by providing the following information: Data Length in bytes Data [...]

Read More »