Site Information

 Loading... Please wait...

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. 

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 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 at the 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 easily use it 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 hardware.

As I mentioned, the ESP32 SPI interface plays a vital role in this test scenario, requiring 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 required.

The ESP32 has four SPi buses, but only two are available for usage, 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 straightforward. 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 cannot 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 through 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, ensure 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 get familiar with the serial connection and which parameters to consider. However, to develop 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 provide 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, so 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. Meany 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; we will share them on this web page.

Throughout the book, projects utilize the ESP microcontrollers' wireless functionality and processing power. 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...