Serial communication protocols play a significant role in technology. These protocols are vital frameworks of an embedded system. They control the transmission of data over the bus. There are several types of serial communication protocols. Each of these protocols has its unique functions.
When creating a development board, there are protocols for relating with active components. In this article, we will be comparing SPI vs I2C vs UART. These hardware interfaces are commonly used in microcontroller development.
What is SPI?
The SPI means Serial Peripheral Interface. It is a protocol that features four wire-based full-duplex communication. The SPI is one of the most common serial protocols well-suited for faster data rates. It uses four wires which are:
- MISO (Master Out Slave)
- SS/CS (Chip Select)
- SCK (Serial Clock Line)
- MOSI (Master In Slave Out)
The SPI interface makes use of the master-to-slave format to regulate several slave devices with a master. Unlike UART that is asynchronous, SPI is synchronous. SPI utilizes a built-in clock from the master. This helps to ensure the slave and master devices are operating on the same frequency.
The serial peripheral interface is specifically designed for the connection of microcontrollers. This interface functions at full-duplex and operates at faster data transmission rates. SPI is commonly used in SD cards and display modules.
SPI has continued to take on several forms over the years. Speed is the greatest evolution in SPI. This protocol is now used in applications featuring speeds greater than 100MHz. SPI can send data in various formats like Quad, Dual, and Single I/O SPI. Data transmissions can be faster if more I/O is being used.
How SPI works
SPI communicates in two different ways. Firstly, it selects every device with a CS line. Each device needs a separate CS line. The second method involves daisy chaining. Here, every device is connected to another via its data out to the data in line. The number of SPI devices you can connect has no limit.
In the SPI, there is master and slave communication. The master always starts the communication. During the communication, data shifts out from the master and then shifts into the slave. SPI supports multi-slave communication.
SPI works in a different way. It makes use of separate lines for data. This protocol features a higher bus range speed. The SPI is a synchronous serial data transfer between the peripheral device and the CPU. This protocol is ideal when you need a fast and reliable interface.
When working with this protocol you need to have the appropriate set of tools. The inclusion of logic analyzers and oscilloscopes is helpful in the development process. An oscilloscope is a great tool to use when designing the SPI bus.
Pros and Cons of SPI
- Supports full-duplex
- Utilizes a master’s clock. So, it doesn’t require precision oscillators in slaves
- Faster data transmission rate
- Features simple software implementation
- Has no stop and start bits
- Features no complex slave addressing system
- There will be complex wiring when more than one slave is in communication
- Utilizes four wires
- Doesn’t acknowledge data receiving
- Doesn’t check errors
- It gives room for a single master
What is I2C?
I2C is one of the serial communication protocols. It means inter-integrated –circuit. This protocol is ideal for modules and sensors. 12C is a bidirectional synchronous serial bus. It needs two wires for data transmission between devices linked to the bus.
I2C protocol is ideal for applications that require various parts. 12C can have several masters and slaves. You will find I2C in consumer electronics and industrial equipment. 12C can maintain a vivid communication pathway while linking up about 128 devices to the mainboard.
This protocol features two pins. These pins are the Serial Clock Line (SCL) pin and the Serial Data Line (SDL) pin. The SDL transfers and receives data. Meanwhile, the SCL functions as a clock. I2C is a protocol that offers support to slow devices. Here, the master can transmit and receive data from the slave. The master controls the clock bus. However, in some cases, slaves can suppress the clock signal.
I2C protocol connects low-speed, short-distance peripherals on circuit boards. This protocol is commonly used in reading hardware sensors and reading memory.
How does I2C work?
In the I2C protocol, the master transmits a start bit from its SDA pin. After this, the seven-bit address chooses the slave. After it receives the address and start bit, the slave transmits an acknowledge bit to the master. The slave listens to the SDA and SCL for any incoming transmissions.
The master will know if the connection makes it to the correct slave once it gets the transmissions. Then, the master chooses which memory it wants to access from the slave. It achieves this by sending eight bits that specify which register to use.
After the address has been received, the slave prepares the select register. This is done before it sends another ‘acknowledge bit’ to the master. The master transmits the data bit to the slave after choosing the specific slave. After sending the data, the slave sends a final acknowledge bit to the master.
I2C connects devices like microcontrollers with peripheral devices in an embedded system. The peripheral devices serve as the slave devices. The microcontroller serves as the master device. All devices must feature a distinct address to detect it on the bus. This address enables the master devices to exchange communication between the two wires.
The relationship between the slave and master devices isn’t constant. The sending and receiving of data depend on the direction of data transmission at the time. The master must address the slave before it transmits data. It must also address the slave if it wants to get data from the slave.
The host receives the information the slave has sent. Then, the receiver ends the receiving process. The host generates the timing clock. It also terminates data transfer. Also, it is important to link up the power supply via a pull-up resistor.
Pros and Cons of I2C
- Features a low signal/pin count even with several devices on the bus.
- Supports several masters.
- Supports multi slave and multi-master communication.
- Uses two wires to create communication among several devices.
- It can adapt to the demands of different slave devices.
- It becomes complicated when there are more master and slave devices
- It has a slower speed
- The interface is half-duplex
What is a UART?
A UART refers to Universal Asynchronous Receiver Transmitter. It is a form of device-to-device digital communication. A UART is a microchip that controls the interface of a computer to its attached serial devices. It is a protocol used for full-duplex serial communication. The UART is a chip designed to carry out asynchronous communication.
This hardware peripheral converts the outgoing and incoming data into the serial binary stream. UART functions when an application doesn’t require high-speed data transmission. This communication device features a single receiver/transmitter. It needs a single wire for data transmission and another wire for receiving data.
With the help of an RS232-TTL converter, you can interface a URT with a personal computer. This is because a UART and an Rs232 don’t need a clock for receiving and transmitting data. The UART frame features a 1or 2 stop bits and 1 start bit for serial data transfer.
The UART features two core components; the receiver and transmitter. The receiver has a control logic, receiver shift register, and a receive hold register. The transmitter features the control logic, transmit hold register, and transmit shift.
The mode of transmission in a UART is in the form of a packet. A packet features a data frame, stop bits, start bit, and a parity bit. The UART enables the receiver to communicate with the auxiliary device. This protocol can deal with synchronization management problems between external serial devices and computers.
How does the UART work?
A UART can function between devices in three different ways. These include the simplex, half-duplex, and full-duplex. In simplex, the transmission of data is in one direction. The half-duplex transmits data in either direction. The full-duplex transmits data in both directions simultaneously.
There is data flow from TX of transmitting UART to RX of receiving UART. The TX is the transmit data while the RX is the receiving data. A transmitting UART will get data from a data bus sent by another component. After the UART receives the information from the data bus, it will create the data packet. It needs to include a stop bit, parity bit, and a start bit to create the packet.
The data packet then transmits at the transmit data pin. Here, the receiving UART will interpret the data packet as its receiving data pin. Data transmission continues until data in the transmitting UART finishes.
For data transmission to occur, the receiver and the transmitter must agree with some configurations. These are:
- Start bit
- Baud speed
- Parity bit
- Stop bit
- Data length
UART requires no clocks. It includes stop and start bits being transferred to indicate the beginning and end of a message. With this, the receiving UART will know when bits should start and stop reading. The receiving UART identifies a start bit and reads the bits at the BAUD rate. This rate is the data transmission speed and is always set to 115,200.
Both the receiving and transmitting UARTs must operate at almost the same baud rate. If the BAUD rate’s difference is above 10%, the bits’ timing may go off. This may render the data useless. You must be certain that UARTs can transfer and receive data from the same packet.
Pros and Cons of Using UART
- It doesn’t need any clock
- UART is very easy to operate
- Features parity bit that enables error checking
- Uses two wires
- The data frame size is limited to 9 bits
- Features low data transmission speeds
- UART can’t use several master systems and slaves
SPI vs I2C vs UART – What are the Differences?
UART, SPI, and 12C are serial communication protocols. However, these serial protocols are different in terms of how they operate. Here, we will be comparing the difference between these three protocols.
For a UART, TxD and RxD are the pin designations. The TxD refers to transmit data while the RxD is the receive data. The pin designations for SPI include MISO, SS, SCLK, and MOSI. The MISO refers to Master Input and Slave Output. SS refers to slave select and SCLK refers to a serial clock. MOSI refers to master output and a slave output. The pin designations for I2C are serial data (SDA) and serial clock (SCL).
Type of communication
UART is asynchronous communication. It is a form of device-to-device digital communication. SPI and I2C are synchronous.
UART doesn’t use a common clock signal. Here, both devices utilize their independent clocks. In an SPI, there is only one common clock signal between the slave and master devices. In I2C protocol, there is a common clock signal between multiple slaves and multiple masters.
UART features a one-to-one connection between devices. It doesn’t need addressing. The SPI protocol uses slave select lines to address any slave linked with the master. For I2C, there are multiple masters and multiple slaves. Also, all masters can communicate with slaves. I2C can allow about 27 slave devices to connect to its interface.
In terms of protocol, the UART uses a stop bit and a start bit for 8 bits of data. I2C uses stop and start bits. For 8 bits of data I2C uses ACK. This indicates if data has been received.
Number of wires
UART features 1 wire while SPI features 4 wires. On the other hand, I2C features 2 wires.
SPI vs I2C vs UART has been discussed in this article. Each of these communication protocols has its unique features. As a user, it is important you choose a protocol that meets your project’s requirements. For instance, the SPI is an ideal choice if you want the fastest protocol. Meanwhile, if you need to connect several devices, the I2C is the best option.