RS-485, I2C, SPI. CAN, USB, Ethernet. How do you choose? In this quick guide to serial communication, we will go over the most common communication schemes and how to approach system architecture decisions.
The secret to making sense of it all is the Open Systems Interconnection (OSI) model. It is a framework that divides communication into seven layers, allowing engineers to have a working understanding of complex systems and abstract technical intricacies.
Not all systems implement every layer, and there can be overlap. For embedded systems, we are primarily focused on the data link layer and the physical layer. To choose a communication scheme, there are several factors to consider:
- Distance between devices
- Data transfer rate
- Noise immunity
- Available hardware resources & efficiency
- Power consumption
- Ease of implementation
- Specific application requirements
With this in mind, we can now compare some of the most common interfaces.
UART
UART stands for Universal Asynchronous Receiver/Transmitter and has been around since the 1960s. It is important to note that UART refers to the hardware used for serial communication and is technically not a communication protocol. It operates at the datalink layer and is used to implement protocols such as RS-232, RS-422, and RS-485. These standards have defined electrical (physical layer) characteristics such as common mode voltage range and signal levels. RS-232 is the default protocol for serial communication with a computer. It is easy to implement, point-to-point, and its wiring is typically TX, RX, and GND. Two optional lines (RTS/CTS) can be added for flow control. RS-422 and RS-485 are similar to RS-232 but have key distinctions: multi-point capability, longer range, faster speed, and industrial applications. For more information the importance of differential signaling, see our Mastering Differential Signals blog.
SPI
Serial Peripheral Interface (SPI) is a synchronous protocol meant for processors to talk to peripherals within a board or very short distance. It requires four types of connections, as summarized in the table below.
A bus will have 3 + n wires, where n is the number of slave devices. The high wire count is a disadvantage, but the benefit is that the SPI interface is the easiest to implement in firmware. It is so simple that a bit-bang interface can often be used.
Take a deeper dive into achieving high data rates with SPI in Analog Devices’ blog.
I2C
Pronounced “I squared C” or “I 2 C,” I2C stands for inter-integrated circuit and is sometimes referred to as “two-wire interface.” Like SPI, it is meant for short distance communication within a board or box. Its key features are minimal wires and multiple masters and peripherals. The tradeoff is a more complicated driver – a state machine is needed to handle bus arbitration. Thus, I2C falls under 3 layers of the OSI Model (up to Network).
NXP provides an excellent I2C User Manual that is great for both hardware and software engineers.
1-Wire
1-wire features a single wire for communication and power (…and a second wire for ground of course!). Its implementation is like that of I2C but has an implicit clock at 100Kb/s. It was originally invented for use in a momentary contact environment; the most common application for 1-wire is authentication chips. Even so, it can be used for other specialized applications. The 1-wire data rate is relatively low, but its range is surprisingly high—up to 10m, and up to 100m using a special cable.
CAN Bus
Controller Area Network (CAN) is a serial bus defined by the International Standardization Organization (ISO) in ISO-11898. It was developed for the automotive industry and is now popular in many industrial applications due to its simple two-wire linear bus topology, high electromagnetic interference (EMI) immunity, bus arbitration protocol, and error checking features. ISO-11898 follows the OSI model and thus the physical layer, datalink layer, and network layer are well defined. See the figure below for the standard architecture, taken from Texas Instruments application report SLOA101B.
USB
Universal Serial Bus (USB), as indicated by its full name, was designed to standardize the many serial and parallel ports of computers and their peripherals. This industry standard is exceptionally useful in that it defines power delivery and data exchange. There are too many USB variants to provide an accurate summary, but at its core it consisted of: 2 data signals (a differential pair), 5 volts, and ground. It is asynchronous, asymmetric, and requires significantly more complicated software compared to the previously discussed protocols. A USB cable can be as long as 5 meters without signal integrity issues.
A common source of confusion is the difference between connectors, generation specifications (i.e., USB 1.x, USB2.x), data rates, and power levels. Names are often used interchangeably to reference different specs, so knowing the OSI model is again useful for navigating the evolving standards.
Ethernet
Ethernet is a broad, complicated family of networking technology commonly used for computer LANs or internet. It technically is the data link layer, but having ethernet almost always means having a network layer, transport layer, and operating system included. It is an asynchronous, symmetric, robust protocol capable of the highest speeds. Data rate varies with ethernet type and cable length, but expect speeds on the order of Megabits/s or Gigabits/s.
Parallel Communication
While the protocols discussed up to this point have been serial, it is worth mentioning parallel communication. Put simply, it is many data lines with multiple bits sent back and forth simultaneously. Parallel interfaces are generally more expensive due to the higher number of I/O lines. They are commonly used for applications requiring high data throughput such as external memory and LCDs.
Choosing the Right Communication Protocol
In truth, there are likely many viable solutions for whatever application you may have. A typical microcontroller will always support I2C, SPI, and UART. If you are working with higher level industrial hardware, you are probably going to have Ethernet, RS-485, CAN, or a similar interface. While there are many factors to consider, it ultimately comes down to the application and its core requirements.
Notes
- The number of wires refers to signals, not GND. A wire for ground is always needed.
- An asymmetric protocol has communication controlled by a master device
- Synchronous- is there a clock signal or is the clock implicit?
- 100 Gbps is used for data centers and telecom networks; however, home devices typically have hardware capable of 100Mbps or 1Gbps.
Additional References
https://www.omega.com/en-us/resources/rs422-rs485-rs232
https://www.ti.com/lit/ug/sprugp1/sprugp1.pdf
https://www.digikey.com/en/articles/decoding-the-usb-standards-from-1-to-4