Site Information

 Loading... Please wait...

ESP32 Sketch: espBerry with Dual MCP2515 CAN Bus HAT

Posted by Wilfried Voss on

espBerry - ESP32 Development Board with Dual Isolated CAN Bus HAT

The following describes the connection and the sketch to control a Dual CAN Bus HAT with the MCP2515 CAN controller per the espBerry development board. However, the description applies to any ESP32 module connected to an MCP2515, may it be one CAN port or two, as long as the wiring is as described in the following. 

For more information on the esbBerry, see

Hardware Connection

The MCP2515 is controlled per SPI (Serial Peripheral Interface), and we are using the Standard ESP32 and RaspberryPi GPIO signals for the espBerry.

MCP2515 Arduino Sketch

For the programming, we use the Arduino IDE with a sketch:  MCP2515 software by Cory Fowler as available through The code applies to any Arduino board plus MCP2515 shield. There is also an example for accessing two MCP2515 ports.

At this point, the code supports the espBerry board with the following HATs:

If you are an experienced programmer, you will be fine with the code from github (as referenced above). We have, however, modified the code slightly to make it work "out-of-the-box."

Click here to download the code.

Our code exchanges CAN data frames between the two ports and prints the data of each received frame. If you are only using one CAN port, you will need an external CAN node to test the application.

In the ESP32_MCP2515.ino file, adjust the following line to you needs:

The output on the Serial Monitor will look like this (Dual CAN Mode):

For settings, such as CAN baud rates and operating modes (e.g., sleep, etc.), see the mcp2515.h file.

espBerry - ESP32 Development Board with Dual Isolated CAN Bus HAT

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