Projects / CAN bus between two Arduinos

CAN bus between two Arduinos

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire both modules

    Connect the SPI lines, power and INT as in the table.

  2. NANOMCP251

    Join the bus

    Connect CANH to CANH and CANL to CANL, with a shared ground.

  3. NANO

    Check termination

    Both modules should have their 120 Ω terminator fitted.

  4. NANO

    Upload the sender

    Open the serial monitor and watch the sent values and a result of 0.

  5. NANO9.9 mA

    Upload the receiver

    The LED brightness should follow the pot.

  6. NANO

    Test the filter

    Change the sender ID to 0x200 and see the receiver ignore it.

  7. NANOMCP251

    Break the bus

    Disconnect CANH and watch the sender report errors.

  8. NANOMCP251

    Restore the bus

    Reconnect and see messages resume.

In short

How do you build a CAN bus between two Arduinos?

Two MCP2515 modules send messages over CAN, with filters so each node only sees what it needs. You need Arduino Nano × 2, MCP2515 CAN bus module × 2, Potentiometer 10 kΩ, LED 5 mm red, Resistor 220 Ω and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Each MCP2515 module VCCNano 5VPower
Each MCP2515 module GNDNano GNDGround
Each module CSNano D10SPI chip select
Each module SONano D12SPI data out of the module
Each module SINano D11SPI data into the module
Each module SCKNano D13SPI clock
Each module INTNano D2Interrupt, used on the receiver
Module A CANHModule B CANHBus high
Module A CANLModule B CANLBus low
Nano A GNDNano B GNDCommon ground
Potentiometer wiper (node A only)Nano A0Value to send, ends on 5V and GND
Red LED and 220 Ω resistor (node B only)Nano D6 through the resistor, LED to GNDShows the received value

Code

Install the mcp_can library in Arduino. Upload the sender to node A and the receiver (the commented sketch) to node B. A CAN bus needs 120 Ω across CANH and CANL at each end of the bus; most MCP2515 modules have a jumper or a 120 Ω resistor on board, so make sure it is fitted on both modules. Keep the CANH and CANL wires as a twisted pair. The modules and Nanos run on 5 V, so no level shifting is needed on a 5 V Nano. Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Node A (sender). Library: mcp_can by coryjfowler (Library Manager: "mcp_can").
#include <SPI.h>
#include <mcp_can.h>

MCP_CAN CAN(10);

void setup() {
  Serial.begin(115200);
  // Most blue MCP2515 modules have an 8 MHz crystal. If yours says 16.000, use MCP_16MHZ.
  while (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) != CAN_OK) {
    Serial.println("CAN init failed, retrying");
    delay(500);
  }
  CAN.setMode(MCP_NORMAL);
}

void loop() {
  int v = analogRead(A0);                 // 0 to 1023
  byte data[2] = { (byte)(v >> 8), (byte)(v & 0xFF) };
  byte r = CAN.sendMsgBuf(0x100, 0, 2, data);  // ID 0x100, standard frame, 2 bytes
  Serial.print("sent "); Serial.print(v); Serial.print(" result "); Serial.println(r);
  delay(100);
}

/* Node B (receiver). Upload this as a separate sketch.
#include <SPI.h>
#include <mcp_can.h>

MCP_CAN CAN(10);
const byte LED = 6;

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  while (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) != CAN_OK) { delay(500); }
  // Accept only ID 0x100. The library expects standard IDs shifted left by 16 bits.
  CAN.init_Mask(0, 0, 0x07FF0000);
  CAN.init_Filt(0, 0, 0x01000000);
  CAN.init_Mask(1, 0, 0x07FF0000);
  CAN.init_Filt(2, 0, 0x01000000);
  CAN.setMode(MCP_NORMAL);
  pinMode(2, INPUT);
}

void loop() {
  if (!digitalRead(2)) {                  // INT goes low when a frame waits
    long unsigned int id; byte len = 0; byte buf[8];
    CAN.readMsgBuf(&id, &len, buf);
    if (len == 2) {
      int v = (buf[0] << 8) | buf[1];
      analogWrite(LED, v / 4);
      Serial.print("id 0x"); Serial.print(id, HEX);
      Serial.print(" value "); Serial.println(v);
    }
  }
}
*/

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

sent 512 result 0
sent 513 result 0
sent 512 result 0
sent 513 result 0
sent 512 result 0
sent 511 result 0

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Pot turnedReceiver LED brightness follows
Sender ID changed to 0x200Receiver prints nothing
CANH disconnectedSender reports an error result
Both terminators fittedClean messages at 500 kbit/s

If it does not work

CAN init failed

Check the SPI wiring, CS pin and the crystal setting (MCP_8MHZ or MCP_16MHZ).

No messages received

Check CANH and CANL are not swapped and the grounds are joined.

Garbage at the receiver

Both nodes must use the same speed and the same crystal setting.

Intermittent errors

Check termination at both ends and keep wires short and twisted.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the CAN bus between two Arduinos?

Arduino Nano × 2, MCP2515 CAN bus module × 2, Potentiometer 10 kΩ, LED 5 mm red, Resistor 220 Ω and Jumper wires male to male (40 pcs, 20 cm).

How long does the CAN bus between two Arduinos take to build?

About 120 minutes for a intermediate maker, in 8 steps.

How do I know the CAN bus between two Arduinos works?

Pot turned: Receiver LED brightness follows. Sender ID changed to 0x200: Receiver prints nothing. CANH disconnected: Sender reports an error result.

CAN init failed: what should I check?

Check the SPI wiring, CS pin and the crystal setting (MCP_8MHZ or MCP_16MHZ).

No messages received: what should I check?

Check CANH and CANL are not swapped and the grounds are joined.

Garbage at the receiver: what should I check?

Both nodes must use the same speed and the same crystal setting.

Intermittent errors: what should I check?

Check termination at both ends and keep wires short and twisted.