Learn the difference between SPI, I2c and UART protocols. Each one has different strengths and tradeoffs which are appropriate for certain situations.
By Jeremy Cook
In many situations, engineers have settled upon SPI, UART, and I2C for short-distance device communications. While these protocols have all been used for decades, the benefits of SPI vs. I2C, UART vs. SPI, or any other combination, aren't entirely straightforward. Each has its strengths, as well as tradeoffs. This article will quickly outline how each works and where one might serve a project's needs better than another.
UART – Universal Asynchronous Receiver-Transmitter
Implemented in the early stages of computing development, UART's roots date back to the telegraph. When it comes to machine-to-machine communication, UART could be considered the "alpha protocol." In its modern implementation, parallel data may be passed to a UART chip. The chip turns this input into serial data to be transmitted via a single transmit (Tx) line. A single receive (Rx) line allows for data reception from another UART device. Data transmission may be simplex (single direction), half-duplex (one direction at a time), or full-duplex (both directions simultaneously) and takes place between two devices.
UART wiring is straightforward, requiring only connections for Rx and Tx. Speeds vary, depending on setup, but must be coordinated between both devices -- there's no clock signal. While 115200 baud is standard, other speeds can be used, too. This protocol is often implemented to program microcontrollers.
I2C – Inter-Integrated Circuit
Developed by Philips Semiconductor (now NXP) in 1982, I2C has become widely accepted for communication between microcontrollers and peripherals. Data is transferred via a bus consisting of two wires (SDA – serial data and SCL – serial clock) in half-duplex mode. Typically, a single master device is used to signal one or more slave devices. Each has a unique address to allow them to share the same bus. A multi-master system is also possible but less common.
I2C wiring is simple. Only a single common SDA common SCL line needs to run between the master and slave devices. The typical 7-bit addressing scheme allows up to 128 individual entities to be controlled. Pull-up resistors for the SDA and SCL lines may also be implemented.
This bus technology is generally faster than UART. Standard speeds run from 100kbit/s to as high as 5Mbit/s in ultra-fast mode. However, only half-duplex operation is available, and only one slave can be addressed at a time.
SPI – Serial Peripheral Interface
SPI was developed as a communication standard by Motorola in the mid-1980s and is now widely used for data transfer between devices. A single master device may send and receive data from one or more slaves, and data transfer speeds are dependent on the devices used.
SPI is also the most complicated wiring-wise, requiring lines from the master device for serial clock output (SCLK), master out slave in (MOSI), master in slave out (MISO), and CS/SS for chip select/slave select. CS/SS lines will be needed for every slave device; therefore, if a single master device were controlling three peripherals, you'd need to dedicate 6 IO lines for this purpose.
SPI is generally the fastest of these three protocols. With its independent MOSI and MISO lines, it's capable of sending and receiving simultaneously for full-duplex operation. SPI is often the best choice if speed is essential for a peripheral -- for example, in the case of an SD card reader, or a quickly refreshing display.
I2C vs. SPI vs. UART – What is the Appropriate Protocol to Select?
Whether it's UART's tried and true operation, the enormously expandable nature of I2C or the high-speed operation of SPI, one of these protocols will be most appropriate for your situation. However, the choice will often depend on the peripherals used, as many can communicate using only one of these protocols.