Hi. This is a new forum, and for now you can post without registration.
Feel free to ask any questions. We are all here to help each other.

Smart Home Automation project competition

Starting with 2018 we will hold quarterly Smart Home Automation project competitions! There will also be prizes for the winners!
Read more in the official Competition topic.

ESP8266 and MCP2515 CANbus interface

Discuss the popular microcontrollers and SOC. Arduino, ESP8266, Raspberry PI, etc...
User avatar
Posts: 47
Joined: Fri Nov 10, 2017 11:56 am
x 3

ESP8266 and MCP2515 CANbus interface

Post by oTp » Sat Nov 11, 2017 7:34 am

If you are trying to connect your ESP to a CAN enabled device, you will need a CAN controller and a CANbus driver.
There are many ready to use addon boards available. Many of them use the Microchip MCP2515 for the controller and TJA1050 for the CAN transceiver.
Now, the MCP2515 can operate between 2.7V and 5.5V but the TJA1050 requires 5V. As you know ESP8266 is not 5V tolerant and therefore we can not connect the SPI bus directly. One way would be to use the resistor dividers on MISO and interrupt pins, but here is an alternative solution:


Lift the pin 18 (VDD) of the MCP2515 and connect it directly to the ESPs 3.3V VDD. Now MCP2515 will operate at 3.3V, and interface nicely with ESP, and it is 5V tolerant, so it will also play along with the TJA transciever.

And here is a sample code to get you started. It's really simple thanks to the CAN library. Don't forget to copy it to your library folder of your Arduino IDE.

This modified example is taken from the library:

One thing I would like to warn you is the CS pin. It is set to 10 in the examples, but that conflicts with the TX pin of the serial interface. I moved it to PIN 5 and it works great!

Code: Select all

/* CAN Loopback Example
 * This example sends a message once a second and receives that message
 *   no CAN bus is required.  This example will test the functionality 
 *   of the protocol controller, and connections to it.
 *   Written By: Cory J. Fowler - October 5th 2016

#include <mcp_can.h>
#include <SPI.h>

// CAN TX Variables
unsigned long prevTX = 0;                                        // Variable to store last execution time
const unsigned int invlTX = 1000;                                // One second interval constant
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56};  // Generic CAN data to send

// CAN RX Variables
long unsigned int rxId;
unsigned char len;
unsigned char rxBuf[8];

// Serial Output String Buffer
char msgString[128];

// CAN0 INT and CS
#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(5);                               // Set CS to pin 5

void setup()
  Serial.begin(115200);  // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
    Serial.println("Error Initializing MCP2515...");
  // Since we do not set NORMAL mode, we are in loopback mode by default.

  pinMode(CAN0_INT, INPUT);                           // Configuring pin for /INT input
  Serial.println("MCP2515 Library Loopback Example...");

void loop()
  if(!digitalRead(CAN0_INT))                          // If CAN0_INT pin is low, read receive buffer
    CAN0.readMsgBuf(&rxId, &len, rxBuf);              // Read data: len = data length, buf = data byte(s)
    if((rxId & 0x80000000) == 0x80000000)             // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
    if((rxId & 0x40000000) == 0x40000000){            // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
  if(millis() - prevTX >= invlTX){                    // Send this at a one second interval. 
    prevTX = millis();
    byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);
    if(sndStat == CAN_OK)
      Serial.println("Message Sent Successfully!");
      Serial.println("Error Sending Message...");


You can find the MCP CAN library for the ESP8266 here: https://github.com/coryjfowler/MCP_CAN_lib

User avatar
Posts: 6
Joined: Mon Nov 13, 2017 12:12 pm
x 3

Re: ESP8266 and MCP2515 CANbus interface

Post by EV1 » Mon Nov 13, 2017 12:17 pm

This is great! I would love to hook this up to my car, but I'm not sure how safe it is. i don't wont to brick my car by accident :)

Posts: 19
Joined: Fri Nov 10, 2017 5:23 pm
x 6

Re: ESP8266 and MCP2515 CANbus interface

Post by AutoHouse » Wed Nov 15, 2017 9:41 am

Nah, there is "almost" nothing to worry about! :) If you just connect the RX/TX wires of the canbus, you are pretty safe. You can only do some serious damage if you short something to GND or 12V. And the car would probably stop if you mess up the bus. For example by shorting the rx/tx, or adding to small terminating resistor to it. Actually the bus in a car is already terminated, so you don't need any.

Just go for it! :)


Re: ESP8266 and MCP2515 CANbus interface

Post by raumi20 » Sat Feb 17, 2018 7:31 am

i‘ve connected the ESP8266 and MCP2515 as yours. But i cant get it working :(
In Loopback-Mode is all fine but i cant recive/send in Normal-Mode.
Do you have a idea?

User avatar
Posts: 47
Joined: Fri Nov 10, 2017 11:56 am
x 3

Re: ESP8266 and MCP2515 CANbus interface

Post by oTp » Sun Feb 18, 2018 4:12 pm

Do you have the right terminating resistor? 150Ohm should bi fine.
Do you have the filters set up correctly? A had to study those settings for along time before I understood the meaning of flags and masks etc...
Are you testing the communication between two ESPs, or with some other device?


Re: ESP8266 and MCP2515 CANbus interface

Post by Manikandan » Wed Mar 21, 2018 11:00 am

I want to interface the ESP8266 with CAN MCP 2515.But i am getting CAN init failure.If any sample code available please share to me. THanks

Post Reply