ArduinoArduino UnoExact chip

Two Unos on a CAN bus

Each Uno has an MCP2515 CAN module (mcp_can); their CANH and CANL are one pair. Board 1 sends a count and its knob every half second; board 2 prints each frame and lights its LED when the knob is past half way.

Two Unos on a CAN bus in Argynix Lab

The code

sketch.inoC++
// Board 1: sends a CAN frame every half second through its MCP2515 (CS on pin 10):
// id 0x100, a count and the knob on A0. Board 2 shows what it gets.
#include <SPI.h>
#include <mcp_can.h>

MCP_CAN CAN0(10);
byte count = 0;

void setup() {
  Serial.begin(115200);
  // The common blue module has an 8 MHz crystal: name it, or the bit rate is not 500k.
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 ready");
  else Serial.println("MCP2515 not found: check the wiring");
  CAN0.setMode(MCP_NORMAL);
}

void loop() {
  int knob = analogRead(A0);
  byte data[3] = { count++, highByte(knob), lowByte(knob) };
  byte r = CAN0.sendMsgBuf(0x100, 0, 3, data);
  Serial.print(r == CAN_OK ? "Sent " : "Nobody acknowledged ");
  Serial.print(data[0]);
  Serial.print(", knob ");
  Serial.println(knob);
  delay(500);
}
libraries.txtC++
mcp_can