Additional Information

Site Information

 Loading... Please wait...

Blog

ESP32 Triple CAN Bus Application Through Adding Two MCP2515 Ports

Posted by Wilfried Voss on

The first question that may arise when talking about accessing the MCP2515 CAN Bus controller per ESP32 may be, "Why would you need an MCP2515 controller when the ESP32 comes with an internal CAN port?" Yes, I found this question in one of the online forums while researching this particular topic. The answer is easy: There are applications that require more than one CAN port, and the only applicable ESP32 resource to do so is the SPI interface, thus the  MCP2515 - Stand-Alone CAN Controller with SPI Interface. As a matter of fact, hypothetically, you can add up to six(!) SPI-controlled CAN ports to the ESP32. However, in this post, we restrict ourselves to a total of three CAN ports.

The other aspect is the implementation of  CAN FD (Flexible Data Rate), which continues to gain popularity. The ESP32 supports on-chip Classical CAN, so accessing the SPI port provides a solution in that direction. One of our new developments involves the creation of a Quad-CAN system (1 x Classical CAN, 3 x CAN FD) based on the ESP32, and this is where "researching" the SPI port comes in. However, during this process I learned more about SPI than I wished for. 

One the major advantages of using the ESP32 in combination with the Arduino IDE (or Visual Studio Code) is the vast software support, specifically through resources such as github.com. Unfortunately, quantity does not necessarily result in quality. I spent numerous hours testing MCP2515 breakout boards and software I found on github. I greatly appreciate the github resources, but more than once I would have liked a word with the developers due to insufficient information. I'm a stickler for well-documented software (comments, comments, and more comments) and well-structured code, and many programming samples are posted as soon as they work under certain, undocumented conditions. In the end, the best code samples came from Cory Fowler, whose code I have been using for years.

MCP2515 Test Hardware

MCP2515 CAN Bus Breakout Board With SPI Interface - 2-Pack

As I wrote before, I spent numerous hours trying to get code working, just to realize that the most popular MCP2515 breakout board will not work with the ESP32. When you browse for MCP2515 breakout boards, the vast majority of results will point to the  MCP2515 CAN Bus Breakout Board with SPI Interface as shown here to the left. The board will work just fine with the Arduino Uno or 2560 but not with the ESP32. I tested this setup with three different ESP32 systems, our ESP32 CAN Bus Module, the ESP32-WROOM-32 Development Board, and our new ESP32 module (see top image). In order to connect all CAN Bus devices, we use our CAN Bus Hub with 7 Ports and DC Power Connection.

I am not a hardware specialist, so feel free to provide feedback, if I state something wrong: The problem is that the board is powered by 5 VDC, which collides the the ESP32's 3.3 VDC signals. The MCP2515 accepts a power range of 2.7 to 5.5 VDC, which would work with the ESP32. However, the TJA1050 CAN Bus transceiver requires 4.75 to 5.25 VDC. That wouldn't be a problem if the board would power the MCP2515 at 3.3 VDC, but that would require an additional DC/DC converter and thus increase hardware costs.

Dual Isolated CAN Bus HAT for Raspberry Pi

As demonstrated in the image on top of this page, I ended up using our Dual Isolated CAN Bus HAT for Raspberry Pi. Yes, the board was designed as a HAT, but you can use the board easily as a dual MCP2515 breakout board. The isolation was not mandatory for the testing, but the MCP2515 CAN controller is powered at 3.3 VDC, which worked with all three ESP32 test hardwares.

As I mentioned previously, the ESP32 SPI interface plays a vital role in this test scenario, and that requires some basic information on SPI. I will also add resources (hyperlinks) that explain the technology in more detail.

The ESP32 SPI Interface

SPI stands for Serial Peripheral Interface, a serial full-duplex and synchronous interface. The synchronous interface requires a clock signal to transfer and receive data. The clock signal is synchronized between one central control ("master") and multiple peripheral devices ("slaves"). Unlike UART communication, which is asynchronous, the clock signal controls when data is to be sent and when it should be ready to read.

Only a master device can control the clock and provide a clock signal to all slave devices. Data cannot be transferred without a clock signal. Both master and slave can exchange data with each other. No address decoding is requiered.

The ESP32 has four SPi buses, but only two are available for usage, and they are known as HSPI and VSPI. As we mentioned earlier, in SPI communication, there is always one controller (also known as a master) that controls other peripheral devices ( also known as slaves). You can configure the ESP32 either as a master or slave.

There are many types of ESP32 boards available. Your ESP32 board might have different default SPI pins, but you can find information about default pins from their datasheet. But if default pins are not mentioned, you can find them using an Arduino sketch (use first link below).

For more information, see:

The ESP32 uses the VSPI connection as a default, meaning if you go with the default signals, you should not run into problems. There are ways to change the pin assignment and switch to HSPI (as explained in the above references), but I haven't explored these scenarios for this application.

Connecting ESP32 to the MCP2515 Board

Again, please be aware that each ESP32 module might have different assignments for the SPI signals, i.e., MOSI, MISO, SCLK, and CS. Even the nomenclature of these signals may differ. But, as demonstrated in the above image, the wiring is fairly straight-forward. The below diagram shows the necessary SPI signals on an ESP32-WROOM-32 development board:

Also, please be aware that the MCP2515 board requires 5 VDC power, which can not be taken from most ESP32 boards, i.e., you might need an external power supply.

MCP2515 Software for Arduino

As I mentioned at the beginning of this post, I ended up using an MCP2515 software by Cory Fowler as available though github.com. The code applies to any Arduino board plus MCP2515 shield. There is also an example for accessing two MCP2515 ports.

Make sure, you apply the right CPU board in the Arduino Board Manager, otherwise you may run into problems running the software. Also, assure you assign the right CS (Chip Select) outputs, which you can freely assign according to the available GPIOs.

There is another MCP2515 code available through the Arduino website, but I haven't explored it for this project.

ESP32 System with CAN FD Ports

Testing the ESP32 with additional MCP2515 ports per SPI helped us getting familiar with the serial connection and which parameters to consider. However, for the development of a Quad-CAN system we will switch to CAN FD, resulting in a system with one Classical CAN and three CAN FD ports. CAN FD controllers also support Classical CAN and thus provides more flexibility. Nevertheless, be aware that you cannot operate Classical CAN and CAN FD in the same network (See: CAN FD On A Legacy CAN Bus Network Is Not A Good Idea Due To Compatibility Issues).

CAN FD is slightly more complex than Classical CAN, and that is why starting with Classical CAN (i.e., the MCP2515) was a good start. I will follow up with another post on CAN FD using the MCP2517/18 controller.


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

Host controller with Isolated Classical CAN and CAN FD Interfaces for In-Vehicle Applications

Vecow introduced its EAC-3000 host controller with isolated Classical CAN and CAN FD interfaces, intended for in-vehicle applications, robot control, and automated guided vehicles.Vecow is an American Industrial Systems, Inc. (AIS) company headquartered in Irvine, California, USA. AIS offers in-house electronic and mechanical design, software development, manufacturing, production, and quality control services.The fanless EAC 3000 host [...]

Read More »


Miniature Embedded Cortex-M7 Module for IIot Applications Supports up to Three CAN FD Ports

TQ announced their TQMa117xL system-on-chip (SOC) module to develop space-saving and energy-efficient control systems. The board supports up to three CAN FD interfaces.The module uses the power of the NXP i.MX RT1170 MCU. It comes a compact size of 31 mm x 31 mm (1.2 " x 1.2") and supplies all CPU signals via a total [...]

Read More »


Raspberry Pi Compute Module With CAN FD for Automation and IoT Applications

Kontron announced the release of their Pi-Tron CM4 computer with Linux OS, which utilizes the fourth-generation Raspberry Pi compute module with the 1,5-GHz, 64-bit Broadcom BCM2711 chip and four Arm Cortex-A72 processors. The computer’s steel housing allows for universal use, e.g., for automation and IoT (Internet of Things) applications. Depending on the variant, it provides a 1-GiB, 2-GB, [...]

Read More »


Arduino Portenta H7 - Dual-Core STM32 Processor Board with Two CAN FD Ports and Support for Arduino IoT Cloud

The Portenta H7's central processor is represented by the dual-core STM32H747, including a Cortex M7 at 480 MHz and a Cortex M4 at 240 MHz. The two cores intercommunicate through a Remote Procedure Call mechanism that seamlessly enables calling functions on the other processor. Both processors share on-chip peripherals and can run Arduino Sketches on the Arm [...]

Read More »


DIN Rail Ethernet to CAN/CAN FD Interface Supports SAE J1939, NMEA 2000, CANopen

Kvaser introduced their DIN Rail SE410S-X10 Ethernet-to-CAN(FD) multi-channel interface with additional I/O support through add-on modules.Kvaser supplies advanced CAN Bus solutions to engineers designing and deploying systems in areas as wide-ranging as trucks and buses, petrol-driven and electric cars, industrial automation, avionics, construction equipment, building automation, domestic appliances, marine, medical, military, railway, telecoms, textiles and more.The [...]

Read More »


Programmable Six-Channel Classical CAN And CAN FD Router Supports I/O And Data Logging

The Six-Channel PCAN-Router Pro FD by PEAK Systems connects the data traffic of modern CAN FD and classic CAN Bus networks. Pluggable CAN Bus transceiver modules provide universal adaptation for each CAN-Bus channel to match their individual requirements. In addition, the router comes with an analog input and four digital I/Os. The CAN Bus data traffic can [...]

Read More »


Four Channel CAN Bus to USB Gateway Using The Arduino Due

In the past, I frequently received inquiries regarding the availability of a four-channel CAN Bus gateway. Such devices exist in the marketplace; however, they are usually costly, and, after all, they don't support easy customization or programming. While we at Copperhill Technologies have the ability to create such a gateway, this is primarily a matter [...]

Read More »


Embedded ARM Modules For IoT Applications Support Two CAN FD Ports

The TQMax4xxl modules by TQ Systems represent an LGA (Land Grid Array) based on the Sitara AM243x microcontroller and the AM64xx processor with up to six cores from Texas Instruments. Possible applications include servo motor controllers (for robotics), industrial gateways, data collectors, and small edge servers in production, providing data for the cloud. The module size of [...]

Read More »


ESP32 Project: CAN Bus, SAE J1939, NMEA2000 Converter With IoT Functionality

It was just a few weeks ago that I received some bad news. Due to the worldwide shortage of electronic components, the NXP LPC1754 processor we use for our SAE J1939 ECU Simulator with USB Port will not be available for several months, maybe even well into 2022. Consequently, it was time to rethink the design, and [...]

Read More »