Skip to main content

can-bus

Interface with CAN bus networks for automotive diagnostics, industrial control, and machine-to-machine communication.

Overview

The can-bus node interfaces with Controller Area Network (CAN) buses used in automotive, industrial automation, and embedded systems. It supports both CAN 2.0A (11-bit) and CAN 2.0B (29-bit extended) frame formats via SocketCAN or MCP2515 SPI modules.

2.0A/B
CAN Standard
1Mbps
Max Speed
CAN
SocketCAN
8
Byte Frames

Hardware Setup

MCP2515 SPI Module

# Add to /boot/config.txt:
dtoverlay=mcp2515-can0,oscillator=8000000,
  interrupt=25
dtoverlay=spi-bcm2835-overlay

# Reboot and bring up interface:
sudo ip link set can0 up type can \
  bitrate 500000

USB CAN Adapter

# Install can-utils:
sudo apt install can-utils

# Bring up USB CAN adapter:
sudo ip link set can0 up type can \
  bitrate 500000

# Test with candump:
candump can0

Properties

Property Type Required Default Description
interface string Yes - CAN interface name (e.g., can0, vcan0)
bitrate number Yes - Bus speed in bps (125000, 250000, 500000, 1000000)
filter string No - Message ID filter in hex (e.g., "0x100-0x1FF")
mask string No "0x7FF" Filter mask in hex for matching message IDs
extended boolean No false Use 29-bit extended frame IDs (CAN 2.0B)
loopback boolean No false Enable loopback mode for testing without hardware

Inputs

Send CAN Frame
{
  "payload": {
    "id": "0x123",
    "data": [0x01, 0x02, 0x03, 0x04],
    "extended": false
  }
}
OBD-II Request
{
  "payload": {
    "id": "0x7DF",
    "data": [0x02, 0x01, 0x0C, 0x00,
             0x00, 0x00, 0x00, 0x00]
  }
}

Outputs

Received Frame
{
  "payload": {
    "id": "0x123",
    "data": [0x01, 0x02, 0x03, 0x04],
    "dlc": 4,
    "extended": false,
    "rtr": false,
    "timestamp": 1705312245123
  },
  "interface": "can0"
}
OBD-II Response (RPM)
{
  "payload": {
    "id": "0x7E8",
    "data": [0x04, 0x41, 0x0C, 0x1A,
             0xF8, 0x00, 0x00, 0x00],
    "dlc": 8,
    "rpm": 1726
  }
}

Use Cases

Vehicle Diagnostics

Read OBD-II diagnostic trouble codes, engine parameters, and vehicle telemetry via the CAN bus interface.

Machine Control

Send and receive control messages for industrial machinery, robotics, and automated production equipment.

Sensor Networks

Collect data from distributed CAN-connected sensors in harsh environments where reliability is critical.

Battery Management

Monitor battery management systems (BMS) in electric vehicles and energy storage systems via CAN messages.

Example: Vehicle Data Logger

Log CAN bus messages from a vehicle OBD-II port.

[
  {
    "id": "can-receiver",
    "type": "can-bus",
    "interface": "can0",
    "bitrate": 500000,
    "filter": "0x7E0-0x7EF"
  },
  {
    "id": "obd-decoder",
    "type": "function",
    "func": "const pid = msg.payload.data[2]; if (pid === 0x0C) { msg.payload.rpm = ((msg.payload.data[3] * 256) + msg.payload.data[4]) / 4; } return msg;"
  },
  {
    "id": "data-store",
    "type": "file",
    "filename": "/var/log/can-data.json"
  }
]

Tips & Best Practices

Termination: CAN bus requires 120-ohm termination at both physical ends of the bus. Most OBD-II ports include built-in termination.

Bitrate Matching: All devices on the CAN bus must use the same bitrate. Common rates are 125kbps, 250kbps, 500kbps, and 1Mbps.

Virtual CAN: Use vcan (virtual CAN) interfaces for development and testing without physical hardware: sudo modprobe vcan

Message Filtering: Use hardware filters to reduce CPU load on busy CAN buses. Filter by ID range to capture only relevant messages.

Related Nodes