Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Communication Buses

CAN, USB, 1-Wire and Other Embedded Buses

Learn how CAN arbitration, bit stuffing and CRC work, get a short tour of USB, LIN, 1-Wire, Modbus and Ethernet, and see how to choose the right bus for a design.

UART, SPI and I2C cover most chip-to-chip links on a circuit board. When the link leaves the board, runs through a noisy car or factory, connects many nodes without a central master, or must plug into a PC, you need something else. This lesson explains the CAN bus in some detail (it is the standard in vehicles and industrial machinery) and then gives a practical tour of USB, LIN, 1-Wire, Modbus and Ethernet. It ends with a table to help you choose.

CAN: a bus built for noise and reliability

CAN (Controller Area Network) was designed at Bosch for cars. Dozens of control units (engine, brakes, doors, dashboard) share one twisted pair, CANH and CANL, with a 120 ฮฉ termination resistor at each end of the cable. Its main ideas:

  • Differential signaling. The data is the voltage difference between the two wires, so noise that hits both wires equally cancels. This is why CAN survives engines and motors.
  • Message-based, not address-based. A frame carries an identifier that says what the message is ("engine speed"), not who it is for. Every node sees every frame and its hardware filters keep only the identifiers it cares about.
  • Multi-master. Any node may start sending when the bus is idle. No central controller is needed.
  • Arbitration without loss. If two nodes start at the same moment, the one with the lower identifier wins and continues; the other backs off without corrupting the frame. No bandwidth is wasted on collisions.
  • Built-in error handling. Every frame carries a 15-bit CRC, receivers acknowledge, and nodes track error counters. A node that misbehaves repeatedly disconnects itself from the bus ("bus-off").

Classic CAN runs up to 1 Mbit/s with data fields of at most 8 bytes; CAN FD raises the data phase speed (commonly to 2 to 8 Mbit/s) and the payload to 64 bytes.

Dominant and recessive bits

CAN uses two bus states. Dominant (logic 0) is when at least one node drives the lines apart. Recessive (logic 1) is when nobody does. If one node sends a dominant bit while another sends a recessive bit, the bus shows dominant: the bus behaves like a wired AND of all senders. Each node listens while it sends. A node that sends recessive but reads dominant knows it has lost arbitration and stops transmitting.

Since the identifier is sent first, most significant bit first, a numerically lower identifier wins, and so a lower number means higher priority. The simulation below arbitrates three nodes that start together.

C CAN arbitration: the lowest ID wins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The "brakes" message (0x123) beats "dash" (0x124) at bit 2, the first place their identifiers differ, and "engine" (0x65A) drops out at the very first differing bit. Designers therefore give the most safety-critical messages the lowest identifiers.

Bit stuffing and CRC

To keep receivers synchronized without a clock, CAN uses bit stuffing: after five consecutive bits of the same value, the transmitter inserts one bit of the opposite value, and the receiver removes it. This guarantees an edge at least every six bits. The check at the end of each frame is a CRC, which detects burst errors with very high probability.

C Bit stuffing and CRC error detection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The test string "123456789" is the conventional check input for CRC algorithms, so you can compare your own implementation with published values (CRC-8 with polynomial 0x07 gives 0xF4, and CRC-16/CCITT-FALSE gives 0x29B1). Hardware CRC units found in many microcontrollers do the same job with no CPU time.

A tour of other buses

USB. A host-controlled, tree-shaped bus for plugging devices into computers. Everything is initiated by the host, which polls devices. A device identifies itself with descriptors during enumeration (class, vendor ID, product ID), and class drivers (CDC for virtual serial ports, HID for keyboards and mice, MSC for flash drives) mean you often need no custom driver. Microcontrollers with a USB device peripheral can act as keyboards, serial adapters or storage; a few can act as hosts. USB uses four wires (power, ground, D+, D-), with speeds from 1.5 Mbit/s (low speed) through 12 (full speed) and 480 (high speed). It is complex, so use a vendor stack rather than writing your own.

LIN. A cheap, single-wire, single-master automotive bus (up to 20 kbit/s) for slow things like mirrors and window switches. It is a UART with a master-scheduled frame.

1-Wire. One data wire plus ground; the devices can even draw power from the data line ("parasitic power"). Each chip has a unique 64-bit ID. The best-known part is the DS18B20 temperature sensor, which lets you hang many sensors on a single pin. It is slow (about 16 kbit/s), and its timing is generated by software in microsecond slots, so it is sensitive to interrupts.

Modbus RTU. A request/response protocol from industrial automation, usually over RS-485. A master asks slave number N to read or write registers and verifies a CRC-16. Simple, ubiquitous in sensors and PLCs, and Modbus TCP carries the same idea over Ethernet.

Ethernet. Full network connectivity, now available on many microcontrollers through an integrated MAC and an external PHY chip, or from a dedicated chip over SPI. It needs a TCP/IP stack (lwIP is common) and a lot more RAM than the buses above.

Wireless. Bluetooth Low Energy, Wi-Fi, Zigbee, Thread and LoRa are usually added as a module that talks to the microcontroller over UART, SPI or I2C (or are built into the chip). From your firmware's point of view they are often just another serial link with a command protocol.

Choosing a bus

Bus Wires Speed (typical) Distance Topology Best for
UART 2 up to ~1 Mbit/s meters (RS-485: 1 km) point to point consoles, modules, GPS
SPI 4 + 1 per slave up to tens of Mbit/s on board master and slaves flash, displays, fast ADCs
I2C 2 100 kHz to 1 MHz on board, short cables multi-device, addressed sensors, RTC, EEPROM
CAN 2 (differential) up to 1 Mbit/s (FD: more) up to ~40 m at 1 Mbit/s, much longer slower multi-master vehicles, industrial control
1-Wire 1 + ground ~16 kbit/s tens of meters master and many slaves many cheap sensors
USB 4 12 Mbit/s to 480 Mbit/s 5 m cable host and devices PC connection

A rule of thumb: stay on the board with I2C or SPI, use UART for simple external modules, use RS-485 or CAN when the wire is long or noisy, and use USB when the other end is a PC.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Two CAN nodes start transmitting at the same time with IDs 0x100 and 0x200. Which wins arbitration?

    A dominant 0 beats a recessive 1, so the lower identifier wins and continues unharmed.
  2. 2. Why does CAN use differential signaling?

    The receiver looks at the voltage difference, so common-mode noise is rejected.
  3. 3. What does bit stuffing guarantee?

    After five identical bits an opposite bit is inserted, ensuring frequent edges for clock recovery.
  4. 4. Which bus lets many cheap sensors share a single data pin, each with a unique 64-bit ID?

    1-Wire devices have factory-set 64-bit IDs and share one data line.

Summary

  • CAN is a multi-master, differential, message-based bus with lossless arbitration (lower ID wins), bit stuffing and a CRC.
  • USB is host-driven and class-based; LIN, 1-Wire and Modbus fill niches for cost, wiring or industrial needs.
  • CRCs detect corrupted data far better than simple sums; use hardware CRC units when available.
  • Choose by distance, speed, noise, number of nodes and what is on the other end.

Next: What Is an RTOS?