# I want to do CAN bus communication between 2 ESP32

**URL:** <https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031>\
**Category:** Programming\
**Created:** [December 17, 2024, 10:37am UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031 "2024-12-17T10:37:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![delta\_667](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@delta\_667](https://forum.arduino.cc/u/delta_667)\
**Post date:** [December 17, 2024, 10:37am UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/1 "2024-12-17T10:37:29Z")

</div>

I want to do CAN bus communication between 2 ESP32. I have seen several references, but because of my limited understanding of programming, I am confused.I use the mcp2515 module, with many references and libraries out there, but I chose to use this library [mcp\_can](https://reference.arduino.cc/reference/en/libraries/mcp_can/) and I also use this program

```arduino
// ESP32 CAN Receive Example - note added INPUT_PULLUP to CAN0_INT
//
// ESP32 connections
// MCP2515 INT to ESP32 GPIO22
// MCP2515 SCK to ESP32 GPIO18 CLK
// MCP2515 SI to ESP32 GPIO23 MOSI
// MCP2515 SO to ESP32 GPIO19 MISO
// MCP2515 CS to ESP32 GPIO5 CS
// MCP2515 GND to ESP32 GND
// MCP2515 VCC to ESP32 3.3V

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string

#define CAN0_INT 22 // for ESP32
MCP_CAN CAN0(5); //

void setup() {
  Serial.begin(115200);
  delay(2000);
  Serial.println("\n\nESP32 CAN MCP2515 shield Send/Receive test - MCP2515 Initialize");
  // Initialize MCP2515 baudrate of 250kb/s and the masks and filters disabled.
  // check crystal frequency!! e.g. Canbus shield is 16MHz MCP2515 is 8MHz
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("CAN Receive - MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
  pinMode(CAN0_INT, INPUT_PULLUP); // Configuring pin for /INT input ***added PULLUP***
  Serial.println("MCP2515 Library CAN Send/Receive Example\n enter space to send a frame");
}

void loop() {
  // check for data received
  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);
    else
      sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
    Serial.print(msgString);
    if ((rxId & 0x40000000) == 0x40000000) { // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {
      for (byte i = 0; i < len; i++) {
        sprintf(msgString, "0x%.2X ", rxBuf[i]);
        Serial.print(msgString);
      }
      Serial.print((char *)&rxBuf+1);
    }
    Serial.println();
  }
  // transmit data when space entered on keyboard
  if (Serial.available()) {
    if (Serial.read() != ' ') return;
    static byte data[8] = { 0x00, 'E', 'S', 'P', '3', '2', 0, 0x08 };
    for (byte i = 0; i < 8; i++) {
      sprintf(msgString, " 0x%.2X", data[i]);
      Serial.print(msgString);
    }

    // send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
    byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
    if (sndStat == CAN_OK) {
      Serial.println(" Message Sent Successfully!");
    } else {
      Serial.println(" Error Sending Message...");
    }
    data[0]++; // increment first byte of data
  }
}

```

So far I'm still having problems with strange receivers. please give input on my problem

---

<div class="post-metadata">

**Author:** ![sherzaad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sherzaad/32/226173_2.png) [@sherzaad](https://forum.arduino.cc/u/sherzaad)\
**Post date:** [December 17, 2024, 10:47am UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/2 "2024-12-17T10:47:05Z")

</div>

> [@delta\_667](#):
>
> ESP32

ESP32 already come with x1 CAN channel!

have a look at this you tube tutorial 😉

[Making a CAN Bus on a breadboard with two ESP32 microcontrollers!](https://www.youtube.com/watch?app=desktop&v=ZH_CCs12ptg)

all relevant material available on the video links.

hope that helps...

---

<div class="post-metadata">

**Author:** ![delta\_667](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@delta\_667](https://forum.arduino.cc/u/delta_667)\
**Post date:** [December 17, 2024, 11:14am UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/3 "2024-12-17T11:14:06Z")

</div>

I've seen this reference before. in this reference it means can channel, namely pins 4 and 0 on the esp32 or is there something else? and dont need mcp2515 just tja1050?

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [December 17, 2024, 12:54pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/4 "2024-12-17T12:54:00Z")

</div>

> [@delta\_667](#):
>
> dont need mcp2515 just tja1050?

have a look at [using-two-esp32-boards-and-2-mcp2515-boards-to-communicate-over-can-not-receiving-anything](https://forum.arduino.cc/t/using-two-esp32-boards-and-2-mcp2515-boards-to-communicate-over-can-not-receiving-anything/1319908/8)  
you can use a pair of MCP2515 modules or TWAI interface driving cjmcu-1051 (or similar) transceivers

---

<div class="post-metadata">

**Author:** ![sherzaad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sherzaad/32/226173_2.png) [@sherzaad](https://forum.arduino.cc/u/sherzaad)\
**Post date:** [December 17, 2024, 1:31pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/5 "2024-12-17T13:31:16Z")

</div>

> [@delta\_667](#):
>
> I've seen this reference before. in this reference it means can channel, namely pins 4 and 0 on the esp32 or is there something else? and dont need mcp2515 just tja1050?

off you asking me this question if you watch the youtube but anyway...

yes, that would be using the ESP32 pins assigned for CAN communication + the tja1050 as the CAN transceiver interface; you would not require to use and MCP2515 using this arrangement.

---

<div class="post-metadata">

**Author:** ![delta\_667](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@delta\_667](https://forum.arduino.cc/u/delta_667)\
**Post date:** [December 17, 2024, 4:08pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/6 "2024-12-17T16:08:58Z")

</div>

with your suggestion it has succeeded but there is still a little error I think in terms of electrical. I connect all the electrical with jumper cables and when reading while I press the cable and sometimes it works. currently I only short terminal j1 where there is a 120 resistor for CANH, CANL. is there anything else that needs to be added in terms of electrical wiring to smooth the transmit and receive?

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [December 17, 2024, 4:14pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/7 "2024-12-17T16:14:39Z")

</div>

sounds like you may have a poor connection  
jumper cables between modules are a source of poor connections and intermittent problems  
check each cable in turn and replace any which cause problems  
the canbus should be terminated with a 120ohm resistor at each end - test between CANH and CANL with a multimeter should read 60ohms - there is usually a jumper on MCP2515 modules to connect the 120ohm resistor

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [December 17, 2024, 4:25pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/8 "2024-12-17T16:25:58Z")

</div>

> [@horace](#):
>
> you can use a pair of MCP2515 modules or TWAI interface driving cjmcu-1051 (or similar) transceivers

Presumably (for testing and code development purposes) you can just cross connect the TWAI interface TX & RX pins between the two ESP32 devices?

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [December 17, 2024, 4:38pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/9 "2024-12-17T16:38:39Z")

</div>

> [@gfvalvo](#):
>
> Presumably (for testing and code development purposes) you can just cross connect the TWAI interface TX & RX pins between the two ESP32 devices?

never attempted that - always connected the ESP32s to transceivers then to other devices with CANBUS interfaces

---

<div class="post-metadata">

**Author:** ![delta\_667](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@delta\_667](https://forum.arduino.cc/u/delta_667)\
**Post date:** [December 17, 2024, 5:23pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/10 "2024-12-17T17:23:40Z")

</div>

i have tried it and it works thanks, i did not change the rx and tx pins in the program to 4 and 0 but still it as 21 22 like in example on website

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 15, 2025, 5:24pm UTC](https://forum.arduino.cc/t/i-want-to-do-can-bus-communication-between-2-esp32/1333031/11 "2025-06-15T17:24:14Z")

</div>

Closed on June 15, 2025, 5:24pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
