Try Embedded

Two Arduinos on a CAN bus

A knob on one Arduino dims an LED on another: each board has its own MCP2515, and frames cross the CAN bus.

Communication · Advanced

In a real product: Car body controller (Automotive): Two ECUs share data over CAN.

Two nodes of a CAN network, each a Uno with its own MCP2515 module, joined by the CANH and CANL pair. One reads a knob and sends it; the other sets an LED's brightness from it and answers. Each board runs its own sketch: sketch.ino on the first, uno2.ino on the second.

What you'll learn

  • Two controllers on one bus: each acknowledges the other's frames, so both can send
  • Choosing identifiers: 0x100 for the knob, 0x101 for the answer
  • Packing a 10-bit reading into two data bytes and back

How it works

Every 100 ms the first board reads A0 (0 to 1023) and sends frame 0x100 with the value in its first two bytes, high byte first. The second board receives it, writes a quarter of it to the LED on pin 9 with analogWrite(), and sends frame 0x101 with the brightness it set. The first board prints the answers it gets, when they change.

Both modules run at 500 kbit/s with an 8 MHz crystal. A frame is only sent once another node acknowledges it, which is why a CAN node alone on a bus reports send errors: here each board is the other's acknowledgement.

Try this

  • Turn the knob while it runs and watch both serial monitors (pick the board in the monitor's list).
  • Change uno2.ino to CAN_250KBPS: both boards' sends fail, as neither understands the other.
  • Make the second board send its own frame when the brightness passes 200, and light pin 7 on the first board.

sketch.ino

// Node 1: sends the knob on A0 as frame 0x100 every 100 ms through an MCP2515 (CS 10) at 500 kbit/s with an 8 MHz
// crystal, and prints node 2's answers (frame 0x101: the brightness it set).
#include <SPI.h>
#include <mcp2515.h>

MCP2515 can(10);
struct can_frame out, in;
unsigned long lastSend = 0;
int lastBrightness = -1;

void setup() {
  Serial.begin(9600);
  can.reset();
  can.setBitrate(CAN_500KBPS, MCP_8MHZ);
  can.setNormalMode();
  out.can_id = 0x100;
  out.can_dlc = 2;
}

void loop() {
  if (millis() - lastSend >= 100) {
    lastSend = millis();
    int knob = analogRead(A0);
    out.data[0] = knob >> 8;
    out.data[1] = knob & 0xFF;
    if (can.sendMessage(&out) != MCP2515::ERROR_OK) Serial.println("Send failed: is another node on the bus?");
  }
  if (can.readMessage(&in) == MCP2515::ERROR_OK && in.can_id == 0x101 && in.data[0] != lastBrightness) {
    lastBrightness = in.data[0];
    Serial.print("Node 2 set the LED to ");
    Serial.println(lastBrightness);
  }
}

uno2.ino

// Node 2: receives frame 0x100 (a knob reading, high byte first), dims the LED on pin 9 to match, and answers with
// frame 0x101 holding the brightness. MCP2515 on CS 10 at 500 kbit/s, 8 MHz crystal.
#include <SPI.h>
#include <mcp2515.h>

MCP2515 can(10);
struct can_frame in, reply;

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  can.reset();
  can.setBitrate(CAN_500KBPS, MCP_8MHZ);
  can.setNormalMode();
  reply.can_id = 0x101;
  reply.can_dlc = 1;
  Serial.println("Waiting for frame 0x100");
}

void loop() {
  if (can.readMessage(&in) != MCP2515::ERROR_OK || in.can_id != 0x100 || in.can_dlc < 2) return;
  int knob = (in.data[0] << 8) | in.data[1];
  byte brightness = knob / 4;
  analogWrite(9, brightness);
  reply.data[0] = brightness;
  can.sendMessage(&reply);
  static int shown = -1;
  if (brightness != shown) {
    shown = brightness;
    Serial.print("Knob ");
    Serial.print(knob);
    Serial.print(", LED ");
    Serial.println(brightness);
  }
}

Lessons behind it