Blog >> Blog Details Page

SPI vs. I2C vs. UART: Differences Between These Communication Interfaces

Posted:02:31 PM November 29, 2022 writer: NextPCB

In an embedded system, communication plays an important role. Peripheral expansion is a high-power-consuming and highly complex system without knowing the communication protocols. All of the embedded systems make use of serial communication interfaces to communicate with the peripherals. Among them, the most common communication protocols in microcontroller development are SPI, UART, and IC2. These protocols have the significant benefits of low data loss and high speed. 

This article has compared all three protocols, including their advantages, disadvantages, and uses. Thus, after reading this article, you can choose the correct protocol per your requirement.

What is a Serial Peripheral Interface(SPI)?

Serial Peripheral Interface (SPI) is one of the serial communication interfaces of synchronous types. In an embedded system, they are used for short-distance communication. This is the full-duplex communication protocol. It allows simultaneous data exchange, both transmission, and reception. PIC, ARM, and AVR controllers are some of the controllers that use the SPI interface. 

The master-slave architecture of SPI has a single master device and microcontroller, while the slaves are the peripherals like the GSM modem, sensors, GPS, etc. 

SPI uses four wires, MISO, MOSI, SS, and CLK. Their wires help in the communication interfaces between the master and slave devices. The master devices both read and write the data. SPI serial bus allows multiple slaves to interface with the master device thus, SPI protocol's major benefit is the speed used where speed is crucial. Furthermore, SPI protocol's applications include SD cards, display modules, etc. 

SPI supports two communication interfaces modes; point-to-point and standard mode. In point-to-point mode, a single controller follows the single slave, while in standards mode, a single master controller can communicate with two slave devices enabling the select chip lines. 

SPI Working Principle

Two methods define the SPI working:

  • The first method selects each device using the CS, which is the select chip line. Each of these devices needs the unique Chip Select line. 
  • The second method is daisy chaining. In this method, each device is connected to the other via the data out of one to the data in line or another.

SPI

SPI devices can connect to various unlimited devices. But the hardware select line limits this connection. An SPI interface provides efficient, simple, point-to-point communication without addressing operations, allowing full-duplex communication.

SPI Working Protocol

  • SPI has four communication ports:
    • MISO – master data input, slave data output
    • MOSI – Master Data Output, Slave Data Input
    • SCLK – clock signal generated by the master device,
    • NSS/CS – Slave-enabled signal, controlled by the master device or also called a select chip line
  • An I2C system has only one enable signal, while a multi-slave system needs multiple enable signals.
  • Internally, the SPI interface consists of two shift registers. 
  • The transmitted data is 8 bits. 
  • During slave enable and shift pulse signals, it is transmitted bit by bit. Front bits are high, and back bits are low.
  • CPUs and peripheral devices communicate synchronously over the SPI interface. The master device transmits data bit by bit under the shift pulse.
  • Low bits lie at the back and high bits at the front. It is a faster communication medium than I2C and offers a few Mbps of data transfer speed.

Pros and Cons 

Pros 

  • Supports full duplex communication
  • Simple and fast data transmission rate
  • Offers simple software implications
  • No, start and stop bits allow continuous data transmission with no interruptions
  • MOSI and MISO allow the simultaneous data send and receive operation
  •  The use of a master clock doesn't require the precious oscillators

Cons

  • More use of slave devices complicates the wiring.
  • It has a single master device
  • Limits the number of slave's devices to connect with the Master 
  • No error check mechanism 
  • Don't have an acknowledgment line of data receiving a message

SPI in microcontrollers

  • SPI Driver/Adapter

It is one of the easy tools to control SPI devices. It is compatible with all operating systems. The live logic analyzer displays the SPI traffic on the screen. The operating voltage of the SPI driver is 3.3V -5V.

  • MCP 3008

It is a 10-bit ADC having 8 channels. Moreover, it connects to the Rasberry pi with the help of an SPI serial connection. 

  • SPI seeeduino V 4.2

A master Arduino and a slave Arduino can communicate using SPI serial communication interfaces with Arduino. The main aim is to communicate over a short distance at a higher speed. 

What is I2C?

Inter-integrates circuit (I2C) is the serial communication protocol. This protocol is effective for the sensors and modules but not for the PCB device communications. The bus transmits information bidirectionally between connected devices using only two wires. With them, you can connect up to 128 devices to the mainboard while maintaining a clear communication pathway between them. They are ideal for projects that require many different parts to work together (e.g., sensors, pins, expansions, and drivers). Also, I2C speed depends on the speed of data, the quality of the wires, and the amount of external noise. I2C also uses two-wire interfaces for connecting low-speed devices, live ADC converters, microcontrollers, I/O interfaces, etc. 

Working Principle of I2C

I2C has two lines: SCL and SDA, serial clock line, and serial data line acceptance port, respectively. The clock line CL is for synchronizing the data transmission. Data bits are sent and received through SDA lines. 

The master device transmits the data and thus generates the clock signal. This clock signal opens the transferred device, and any address devices are considered slave devices. 

On the bus, master and slave devices, transmission and reception, do not always follow the same relationship. Data transfer direction depends on the time. The Master must address the slave before sending data to the slave if it wants to send data to the slave. This will stop the data transfer. At the same time, the Master should address the slave if it wants to receive the data from the slave devices. Finally, the receiver terminates the receiving process by receiving the data sent by the slave.

Additionally, the host generates the timing clock and completes data transfers. Power supply connections must also be made through pull-up resistors. Both lines operate at high power levels when the bus is idle.  

I2C

I2C Working Protocol 

Data Transmission Method

The master devices send the signal to each of the slaves connected. This is carried out by switching SDA and SCL lines from a high to a low voltage level. 

The master device is responsible for sending 7 or 10 bits of the address, including the read/write bit, to the slave for communicating.

The slave compares the address, and if it matches, the ACK acknowledgment bit returns. This would switch the SDA line low for one bit, else there is no change in the SDA line, leaving it high.

The Master also transceives the data frame, and receiving device acknowledges the successful transmission by sending an ACK bit. The master device sends a stop signal to stop the data transmission, where the SCL switch is high before the SDA switch to high. 

Clock Synchronisation

Data transmission requires clock signals from every Master on the SCL line. Data in I2C transmissions remain valid only during the high period of the clock. 

Mode of Transmission

Quick mode and high-speed mode are the two data transmission modes. 

Quick mode

Devices and transceive data rate at 400kbit/s. They must sync with this transmission to slow it down and extend the SCL signal's low period. 

High-speed mode

In this mode, information is transmitted by 3.4 Mbps. It has backward compatibility with the standard mode. This mode transmits the data ta higher data compared to previous modes. 

Pros and Cons

Pros

  • It supports various master devices. 
  • It offers multi-slave and multi-master communication.
  • This protocol is flexible and adaptable too.

Cons

  •  I2C is a bit slower protocol because of the need for pull-up resistors.
  • It takes more space
  • The architecture is more complex with the increasing number of devices.
  • This protocol is half-duplex, which is quite problematic and requires different devices for complete communication.

I2C in Microcontroller

  • Raspberry Pi's 4 channel 16 bit ADC

It is the Seeed product that is compatible with the Rasberry Pi. This 16-bit ADC is used when a more precise ADC is required in the circuit. 

  • I2C Driver/Adapter

It is an open-source tool that is easy to use. Usually, it is used for controlling I2C devices. It is compatible with all OS. It offers a built-in color screen that provides the live dashboard of I2C activity. Thus, when an I2C drive connects to the I2C bus, it displays the traffic on the screen. Besides, it can help to debug the I2C issues and troubleshoot them. 

  • I2C Arduino

I2C Communication interfaces between two Arduino boards is also possible. It is used for short-distance communication interfaces and uses the synchronized clock pulse. This I2C Arduino is used while communicating with the other sensors and devices that need to send the information to the Master.  

  • PCF 8574

It provides the general purpose remote I/O expansion through two bidirectional I2C buses. 

What is UART?

Universal Asynchronous Reception and Transmission (UART) is the serial communication interfaces that provide the communication interface of the host with that of the auxiliary device. This protocol enables full-duplex serial communication. UARTs are chips designed for asynchronous communication.

Using start and stop bits, it transmits data bits in order from the smallest to the largest. Driver circuits control the electric signaling levels external to the UART. 

Thus, this UART protocol supports bi-directional, serial, and asynchronous data transmission. Through data lines 0 and 1, connected to digital pins 0 and 1, this interface consists of two data lines, transmit and receive.

UART can easily handle the synchronization management problem between serial devices and computers. The UART transmits data asynchronously, which means the sending UART doesn't synchronize its output with the receiving UART. UARTs send data packets without a clock signal by adding start and stop bits. So that the receiving UART knows when to start reading the bits, these bits define the beginning and end of the data packet. 

Working Principle of UART

UART

UART works in three various ways; 

  • Simplex: In a simplex system, data is transmitted in only one direction.
  • Half-duplex: In a half-duplex system, data is transmitted in both directions but not simultaneously.
  • Full-duplex: In a full-duplex system, data is simultaneously transmitted in both directions.

After the successful connection, data is transmitted from the TX pin of the transmitting UART to the RX pin of the receiving pin of UART. UART transmits serial data from the master device to the receiving UART by converting it from parallel. UART has no clock signal thus, UART transmits start and stops bits to represent the beginning and end of a message.

The functioning of the UART is possible only if the baud rate or the speed of data transmission of two UARTs syncs with each other. Further the baud rate difference of more than 10% is unacceptable as it makes the data useless. UART transmits data in the form of packets. Each of these data packets has 1 start bit, depending on the UART second, the 5 to 9 data bits, the parity bit for informing the receiver if the data is changed or not, and finally, the stop bits(1 or 2). 

In summary, the UART data transmission includes

  • Initially, transmitting UART gets the data from the data bus in parallel form.
  • After receiving data by transmitting UART, it configures the bit by adding star, parity, and stop bits in the data frame.

Transmititng Uart

  • Then, UART transmits this entire data packet into receiving UART and UARTs sample data at the predetermined baud rate.

UART transmits this entire data packet into receiving UART

  • UARTs receiving data frames discard the start, parity, and stop bits.
  • Finally, UARTs on the receiving end convert serial data into parallel and transmit it to a data bus which completes the data transmission in UART.

UARTs on the receiving end convert serial data into parallel

 UART working protocol

Data Transmission and Receiving

  1. Initially, UARTs need data buses, such as CPUs, to receive data for transmission.
  2. UART then adds three bits called start, parity, and stop bits. 
  3. Next, it adds the three bits, namely, the start bit, parity bit, and stop bit. The TX pin will transmit the three data packets to the receiving UART.
  4. Finally, when the transmitting UART exhausts its data, data transmission stops. 

Interrupt Control

It aims to send the buffer content automatically. It is used in events like

  • Parity Error
  • Frame Error
  • FIFO Overflow Error
  • Receiving Timeout
  • Transmitting and Receiving

FIFO Operation

UART module contains two of the 16-byte FIFOs for data transmission and receiving. The UART receives 4 data when the FIFO triggers an interrupt at 1/4. 

In transmitting FIFO, the data transmission process starts immediately after entering data. While this process executes, all the data continues to enter this FIFO until its full. After then, this transmitting FIFO sends data until it's empty. An extra slot adds after data is clear in the transmitting FIFO.

In receiving FIFO, hardware stores the data, which is an automatic process. Thus there should be enough space so that there is no problem with storage in receiving FIFO.

There is no chance of data loss in this FIFO as any issue is cross-checked in sending and receiving the files. 

 Loopback

A UART has an internal loopback function that sends data from TX to RX, which can be used for diagnostics or debugging.

Serial Infrared Protocol

  • There is an encoder and decoder module for IrDA Serial Infrared (SIR) on UART. SIR modules translate asynchronous UART data to semi-duplex serial interfaces for IrDA.
  • This device provides the digitally encoded output and decoded input to a UART. IrDA SIR physically connects the UART to an infrared transceiver.

 Pros and Cons

Pros

  • It only uses two lines or wires helping to clean circuitry
  • It doesn't require a clock signal.
  • It has a parity bit for error checking.
  • It is an easy and widely used method of communication.

Cons

  • It has a limited data size of 9 bits only.
  • It doesn't support multiple Master and slave devices.
  • The baud rate or the data transmission speed should be below 10% or each other.
  • The data transmission rate is lower compared to the remaining protocols. 

Example of UART in microcontroller

USB to UART 5V

Designed to simplify USB-to-serial communication, these USB-to-serial UART interfaces provide a USB-to-serial connection. Using a USB host controller efficiently reduces the number of external components while using the minimum amount of USB bandwidth. Based on the CH340 USB bus converter can convert USB files into serial files. It can be used for USB to UART 5V converters.

USB CP2102 Serial Converter

RS-232 designs can easily be updated to USB using this highly-integrated USB to UART bridge controller. USB connectivity is provided to UART-equipped devices. Arduino/Seeeduino boards can be upgraded by computer with this USB CP2102 Serial Converter.

UART Seeduino V4.2

The Arduino boars contain at least one serial port that usually communicates the digital pins Tx and Rx through USB. It is compatible to work with Arduino board. This board can be programmed through a MicroUSB cable. 

Base Shield V2

There is no doubt that Arduino Uno is the most popular Arduino board. However, it can be a bit frustrating once you need to connect many sensors and LEDs to the board, and your jumper wires are tangled. This product eliminates the need for breadboards and jumpers. Adding all the grove modules to the Arduino Uno is convenient since the baseboard has grove connectors.

Difference between SPI, I2C, and UART

Speed

SPI protocol is faster among all three communication interfaces, whereas UART is the slowest.

Number of Devices

SPI can take an unlimited number of devices as long as hardware complexity is not a problem. At the same time, I2C can take 127 devices, but UART support just 2 devices.

Number of wires

For connecting Master and slave devices, SPI has 4 wires, I2C has 2 wires, and the UART has only one. 

Complexity

UART beats both SPI and I2C in terms of complexity. It is simple compared to the remaining two protocols, whose complexity increases with the number of devices. 

Communication Types

UART is one of the asynchronous communication interfaces types with no clock signal, whereas I2C and SPI are synchronous protocols. 

Features UART SPI I2C
Full Form Universal Asynchronous Receiver/Transmitter Serial Peripheral Interface Inter-Integrated Circuit
Pin Designations

TxD: Transmit Data

RxD: Receive Data

SCLK: Serial Clock

MOSI: Master Output, Slave Input

MISO: Master Input, Slave Output

SS: Slave Select

SDA: Serial Data

SCL: Serial Clock

Data rate The maximum data rate supported is about 230 Kbps to 460 Kbps. Usually supports about 10 Mbps to 20 Mbps I2C supports 100 kbps, 400 kbps, and 3.4 Mbps. Some variants also support 10 Kbps and 1 Mbps.
Type of communication interfaces Asynchronous Synchronous Synchronous
Distance Lower about 50 feet highest Higher
Master Device One One One or more than One
Clock No use of Common Clock signal. Both devices will use their independent clocks. There is one common serial clock signal between master and slave devices. There is a common clock signal between multiple masters and multiple slaves.
Hardware complexity lesser less more
Protocol For 8 bits of data one start bit and one stop bit is used. Each manufacturer has got their own specific protocols to communicate with peripherals. Hence, a datasheet is a must-to-know read/write protocol for SPI communication to be established.  It uses start and stops bits. It uses an ACK bit for every 8 bits of data which indicates whether data has been received or not. 
Software addressing There is no need for addressing since this is a one-to-one connection between two devices. Any slave connected with the master is addressed by a slave select line. A master device will have 'n' slave select lines for 'n' slaves. All slaves and masters can communicate with each other, and there will be multiple slaves and masters. 

Best communication interfaces among SPI, I2C, and UART

All three communication peripherals have their specific upsides and downsides. The selection of the best protocol for your project depends on your requirements. If you are concerned about the speed, you may go for the SPI, while if you want the simple circuitry with one drive, you may go for the UART communication protocol. 

While if you want to connect numerous devices with no complex array, you can go for the I2C. 

Conclusion

Thus, this article presents communication protocols. Each protocol has its advantages and disadvantages with unique features. Thus, you need to remember that the devices, sensors, or modules should support the communication protocols. 

NextPCB can help you with all PCB-related services. You don't need to worry about your devices' communication protocols. We ensure that you receive PCB with proper communication interfaces per your requirements. Contact us for any questions or request a quote to get started. 

Tag: SPI I2C UART
  • PCB
    Prototype
  • PCB
    Assembly
  • SMD
    Stencil

Dimensions: (mm)

×

Quantity: (pcs)

5
5
10
15
20
25
30
40
50
75
100
120
150
200
250
300
350
400
450
500
600
700
800
900
1000
1500
2000
2500
3000
3500
4000
4500
5000
5500
6000
6500
7000
7500
8000
9000
10000

Other Quantities:(quantity*length*width is greater than 10㎡)

OK

Layers:

Thickness:

Quote now