Skip to main content

Hardware setup

Compatible boards

Any Arduino with SPI support works. The CS pin and SPI pins vary by board:

Board SCK MOSI MISO CS (recommended) Notes Arduino Mega 2560 52 51 50 53 Hardware SPI, most headroom Arduino Uno 13 11 12 10 5V, limited RAM Arduino Nano 13 11 12 10 Same as Uno, compact Arduino Micro 15 16 14 10 5V, USB HID capable Arduino Leonardo 15 16 14 10 Same as Micro ESP32 18 23 19 5 3.3V — use level shifter

For ESP32, the MCP2515 runs at 3.3V — do not connect directly to a 5V MCP2515 module without a level shifter.

Required components

Component Recommended model
Microcontroller ArduinoAny Megaboard 2560from the table above
CAN module MCP2515 + TJA1050 breakout board
Bus terminators 2× 120 Ω resistor
Wiring Twisted pair for CAN_H / CAN_L

Wiring diagram

The MCP2515 communicates with the Arduino over SPI. On the Arduino Mega, the hardware SPI pins are:

Mega
Arduino Mega MCP2515 module
5V VCC
GND GND
pin 52 SCK (SPI clock)
pin 51 SI (MOSI)MOSI
pin 50 SO (MISO)MISO
pin 53 CS (Chip Select)
pin 2 INT (interrupt, optional)

The

Wiring CS Arduino Uno / Nano

Arduino Uno/Nano MCP2515 module 5V VCC GND GND pin is13 configurableSCK in code. This implementation uses pin 53,11 theMOSI hardware SPI pin 12 MISO pin 10 CS on the Mega.pin 2 INT (optional)

Initialization code

Change SPI_CS_PIN to match your board:

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

const int SPI_CS_PIN = 53;10;  // 53 for Mega, 10 for Uno/Nano
MCP_CAN CAN(SPI_CS_PIN);

void setup() {
    while (CAN_OK != CAN.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ)) {
        delay(100);
    }
    CAN.setMode(MCP_NORMAL);
}

MCP2515 oscillator

CAN.begin()Most parameters:MCP2515 modules come with an 8 MHz crystal. Some come with 16 MHz — check the crystal printed on your module and change the parameter accordingly:

ParameterCrystal Value
DescriptionParameter MCP_ANY8 MHz No mask filter (receive all)MCP_8MHZ CAN_250KBPS 250 kbps Standard J1939 speed for heavy vehicles MCP_8MHZ 816 MHz Crystal frequency on the moduleMCP_16MHZ

Installing the library

Install mcp_can by Cory Fowler from the Arduino IDE library manager, or clone directly:

bash
cd ~/Documents/Arduino/libraries
git clone https://github.com/coryjfowler/MCP_CAN_lib

Important

Always place a 120 Ω resistor between CAN_H and CAN_L at each end of the bus. Without terminators, signal reflections will make the bus unstable.