- Home
- Controller Area Network (CAN) Development with the ESP32
Controller Area Network (CAN) Development with the ESP32
Note: The following represents an application note in the making. We will add further posts over time until finished.
Controller Area Network (CAN) Development with ESP32
Classical CAN & CAN FD
by Wilfried Voss
Copperhill Technologies
The ESP32 processor, including the various development kits, is perfect for Controller Area Network (CAN) and Internet of Things (IoT) prototyping applications. It provides multiple on-chip resources, including a CAN controller, WIFI, Bluetooth, BLE, UART, SPI, and more.
The onboard CAN controller supports Classical CAN (CC). The wireless functionality is great for the Internet of Things (IoT) or gateway applications. The SPI ports allow additional external hardware, such as up to six CAN controllers (CC or CAN FD) or SD card support for CAN data recording. For the implementation of higher-layer protocols (HLP), such as CANopen, SAE J1939, and NMEA 2000, as well as for gateway applications, the ESP32 offers multiple serial interfaces (UART, USB), interrupt-driven digital inputs for real-time support, and onboard timers. Furthermore, there are digital and analog IOs.
However, in the following, I will focus on basic Classical CAN and CAN FD implementations regarding hardware and software. While the hardware design is straightforward, developing the corresponding software presented a challenge. The internet offers a myriad of code examples, some of them good, some of them not. So, I filtered the good ones while adding more background information and technical details.
I use the popular Arduino IDE for software development and tests to have common ground with most ESP32 programmers.
All these aspects will be discussed in more detail in the following chapters. Wherever necessary, I will point to references with insights that go beyond the scope of this document.
From the Author
For many years, the Arduino Due has been my favorite solution for rapid prototyping. It provides excellent performance, and programming with the Arduino IDE is fun (despite lacking inline debugging for too long). Add to this the vast code resources you find online, and you are all set to create your favorite applications.
However, Arduino hardware is not necessarily the best choice for industrial and automotive applications with Controller Area Networks (CAN) under real-world environmental conditions, such as vibration and temperature. In contrast, the ESP32 processor and its various development kits operate in an extended temperature range, and in most cases, you can copy Arduino code without modification (using the Arduino IDE). Furthermore, besides the vastly greater memory resources and execution speed, the ESP32 offers on-chip features, such as a CAN controller. Add to this WIFI, Bluetooth, and BLE (Bluetooth Low Energy), which are crucial for IoT (Internet of Things).
The following writing reflects my experience working on Controller Area Network (CAN) applications with the ESP32 processor, and I will share hardware and software aspects for those interested. This document comes with sufficient information to start your application development.
However, I would like to emphasize one crucial aspect: Please understand that I cannot provide help for your specific application beyond this document.
Over the years, I have dealt with many newcomers to the CAN Bus technology, some motivated by great product ideas. Others were thrown into a project because they were embedded system programmers. What they all had in common was being overwhelmed by the challenge of learning the technology and developing hardware and software for it. We have all been there, but the harsh reality is that sound engineering requires research, time, and patience, and there are no shortcuts.
And yes, I understand the frustration of exploring new projects. For instance, I chose the ESP32 processor due to its compatibility with the Arduino system and vast resources. Yet, there were too many occasions when I wanted to have a word with the publishers of various examples due to a lack of documentation and poorly written code. However, I decided that my time was better spent working around these shortcomings by improving documentation and code or continuing my search for better examples. The result is this document.
I always consider these aspects when I write and share as much knowledge as possible. However, it makes no sense to repeat published information, so I provide various hyperlinks in this document to enable further research on details not covered here.
I believe the information in this application note and working through the various online resources will help you gain the necessary expertise to tackle the most complex CAN Bus applications.
ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module
This board comes with an onboard ESP32 WROOM-32 WiFi, Bluetooth Classic, BLE Module, and a CAN Bus port with a transceiver. Also, onboard is an RGB LED, IO pins on a 0.1" pad.
Programming is accomplished through the popular Arduino IDE connected to the USB-to-Serial converter with USB-C connector, automatic bootloader and reset.
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. More Information...
Additional ESP32 & CAN Bus Resources on this Website
- SAE J1939 Protocol Stack Sketch for ESP32 Using the Arduino IDE...
- CAN Bus, SAE J1939, NMEA 2000 Programming with ESP32, Supporting USB, Bluetooth, BLE, And WiFi...
- ESP32 Programming - Classic CAN to Bluetooth Gateway...
- ESP32: CAN Bus Programming with MCP2515 and MCP2517FD...
- ESP32: Isolated CAN Bus Repeater and Baud Rate Converter...
- ESP32 UART0, UART1, UART2 Access Using the Arduino IDE...
- Dual-Channel Linear Actuator Control Module with ESP32...
- ESP32, ESP32-S2 - Serial Port, Native USB Access...
- espBerry Project: ESP32 with NMEA 2000 & NMEA 0183 HAT...
- ESP32 Sketch: espBerry with Dual MCP2515 CAN Bus HAT...
- ESP32 with Dual CAN Port Controls Thomson Linear Actuator...
- espBerry Project: ESP32 with CH9102F USB-UART Chip...
- ESP32 Development Board Controls Dual Isolated CAN Bus HAT...
- ESP32 DC Motor PWM Speed Control (Pulse Width Modulation) - The Hidden Facts...
- ESP32 Development Board with NMEA 2000 & NMEA 0183 HAT...
- Extending ESP32 Functionality by Using Raspberry Pi HATs...
- ESP32 Triple CAN Bus Application Through Adding Two MCP2515 Ports...
- ESP32 Project: USB to Bluetooth Gateway...
- ESP32 Based CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway...
- ESP32 – Programming Three Serial Ports (UARTs) Using the Arduino IDE...
- ESP32 Starter and Development Kits for Building Bluetooth and WiFi Enabled IoT Devices...
- CAN Bus Development with ESP32-WROOM32 Development Board...
- Embedded CAN Bus, SAE J1939, NMEA 2000 Development With ESP32...
- Wireless IoT Project Development with the ESP32....
espBerry - ESP32 Development Board with Dual Isolated CAN Bus HAT
The espBerry DevBoard combines the ESP32-DevKitC development board with any Raspberry Pi HAT by connecting to the onboard RPi-compatible 40-pin GPIO header. The espBerry is the perfect solution for Internet of Things (IoT) applications, especially those requiring wireless capabilities. All open-source code samples take advantage of the popular Arduino IDE with its excellent programming capabilities.
The Dual Channel CAN Bus expansion HAT, designed for the Raspberry Pi, supports the full CAN2.0 Standard, and it features multi onboard protection circuits, high anti-interference capability, and reliable operation. As a result, it suits applications such as automotive devices or industrial automation. More Information...
Table of Contents
Overview
CAN Bus Design with the ESP32 Processor
Internal SJA 1000 CAN Controller
Adding CAN Interfaces per SPI Port
CAN Bus Topology and Termination Resistors
The Integrated Development Environment (IDE)
The Arduino IDE
Visual Studio Code
The Test Hardware
The ESP32-WROOM-32 Development Kit
ESP32 Development Kits with Onboard CAN Controller
Other ESP32 Development Kits
CAN Analyzing & Monitoring Device
Controller Area Network (CAN) Applications
Classical CAN Implementation
Internal SJA1000 CAN Controller
External MCP2515 CAN Controller
CAN FD Implementation
Hardware Design
Software Development
Application Library
CAN Bus Troubleshooting
Most Problems Are Related to Wiring Problems
A Word to OBD-II Enthusiasts
Additional Helpful Features
Automatic CAN Baud Rate Detection and Silent Mode
Interrupt Timer Implementation
Gateway and IoT Functionality
Electronics Projects with the ESP8266 and ESP32: Building Web Pages, Applications, and WiFi Enabled Devices
Copperhill Technologies highly recommends using this book for your wireless application 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...
Trademarks
All trademarks or registered trademarks are the property of their respective owners.
Disclaimer
The information provided in this document is provided "as is" without warranty of any kind. Copperhill Technologies Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Copperhill Technologies Corporation be liable for any damages whatsoever, including direct, indirect, incidental, consequential, loss of business profits, or special damages, even if Copperhill Technologies Corporation or its suppliers have been advised of the possibility of such damages.