CAN bus between two Arduinos
Build it step by step
0 of 8 doneWire both modules
Connect the SPI lines, power and INT as in the table.
Join the bus
Connect CANH to CANH and CANL to CANL, with a shared ground.
Check termination
Both modules should have their 120 Ω terminator fitted.
Upload the sender
Open the serial monitor and watch the sent values and a result of 0.
Upload the receiver
The LED brightness should follow the pot.
Test the filter
Change the sender ID to 0x200 and see the receiver ignore it.
Break the bus
Disconnect CANH and watch the sender report errors.
Restore the bus
Reconnect and see messages resume.
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
- Development BoardsBuyArduino Nano × 2
- InterfacesBuy
- PassivesBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Each MCP2515 module VCC | Nano 5V | Power |
| Each MCP2515 module GND | Nano GND | Ground |
| Each module CS | Nano D10 | SPI chip select |
| Each module SO | Nano D12 | SPI data out of the module |
| Each module SI | Nano D11 | SPI data into the module |
| Each module SCK | Nano D13 | SPI clock |
| Each module INT | Nano D2 | Interrupt, used on the receiver |
| Module A CANH | Module B CANH | Bus high |
| Module A CANL | Module B CANL | Bus low |
| Nano A GND | Nano B GND | Common ground |
| Potentiometer wiper (node A only) | Nano A0 | Value to send, ends on 5V and GND |
| Red LED and 220 Ω resistor (node B only) | Nano D6 through the resistor, LED to GND | Shows 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.
| Check | Expected |
|---|---|
| Pot turned | Receiver LED brightness follows |
| Sender ID changed to 0x200 | Receiver prints nothing |
| CANH disconnected | Sender reports an error result |
| Both terminators fitted | Clean 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 →
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.
