Hello everyone, I am Rose. Welcome to the new post today. Communication between electronic devices is like communication between humans. Both parties need to speak the same language. In electronic products, these languages are called communication protocols. This article will make some comparisons between SPI, UART, and I2C.
Data bits are used to communicate between electronic devices. Bits are binary and can only be one of two values: 1 or 0. Bits are transmitted from one device to another when the voltage is rapidly changed. In a 5V system, "0" communicates through short 0V pulses, while "1" communicates through short 5V pulses.
Data bits can be sent in a serial or parallel format. In parallel communication, data bits are sent over the cable at the same time. The parallel transmission of the character "C" in binary (01000011) is shown in the diagram below:
Bits are delivered one by one across a single wire in serial transmission. The serial transmission of the letter "C" in binary (01000011) is shown in the diagram below:
SPI is a widely used device communication protocol. It has the distinct advantage of being able to send and receive any number of bits continuously and without interruption. Data is transferred in the form of packets with a limited amount of bits in I2C and UART.
The SPI device is split into two parts: a host and a slave system. The host (typically a microcontroller) is the control device, while the slave (commonly a sensor, display, or memory chip) receives instructions from the host.
There are four signal lines in a set of SPI communication: Signal line, master output, slave input, MOSI (Master Output/Slave Input). Signal line, master input, slave output, MISO (Master Input/Slave Output). Clock signal SCLK (Clock). Chip select signal SS/CS (Slave Select/Chip Select).
SPI protocol features
In actuality, the number of slaves is restricted by the system's load capacitance, which limits the master's capacity to flip between voltage levels reliably.
Working principle
Clock signal
Because each clock cycle transmits one bit of data, the data transmission speed is determined by the clock signal's frequency. The SPI communication is always begun by the host because the clock signal is generated by the host setup.
Synchronization refers to any communication technique in which devices share clock signals. Some asynchronous communication does not utilize a clock signal, yet SPI is a synchronous communication mechanism. In UART communication, for example, both sides are set to a pre-configured baud rate, which sets the data transfer speed and timing.
Chip select signal
The slave's CS/SS is pulled down by the host, allowing communication. The chip select line remains high in the idle/non-transmission condition. On the host, there can be numerous CS/SS pins, allowing the host to converse with multiple slaves.
These slave devices can be connected in the following ways if the host only has one chip select pin available:
MOSI and MISO
The master sends data to the slave in a serial fashion via MOSI, while the slave can send data to the master via MISO, and the two can be done simultaneously. SPI is a full-duplex communication protocol in theory.
Steps in the transmission
1. The clock signal from the host
2. To activate the slave, the master pushes down the SS / CS pin.
3. The slave receives data from the master through MOSI.
4. The slave will return the data to the master via MISO if a response is necessary.
The use of SPI has some advantages and cons. If you're choosing between different communication protocols, think about the project needs carefully.
Pros and cons
Advantage
Because SPI communication lacks a start bit and a stop bit, data can be streamed indefinitely; there is no cumbersome slave addressing mechanism like I2C; and the data transfer rate is nearly twice as fast as I2C. At the same time, independent MISO and MOSI lines can send and receive data.
Shortcoming
SPI has four wires (I2C and UART have two), no confirmation of successful signal reception (I2C does), and no error checking (such as parity bits in UART, etc.).
Serial communication is also known as UART (Universal Asynchronous Receiver/Transmitter). It is a physical circuit or standalone IC in a microcontroller, not a communication protocol like SPI or I2C.
UART's primary function is to send and receive serial data. Its best feature is that data is sent between devices using only two wires. The UART premise is simple to grasp, but if you've never read the SPI communication protocol, it's a nice place to start.
Two UARTs communicate directly with each other in UART communication. The sending UART converts parallel data from the control device (such as the CPU) to serial data and sends it serially to the receiving UART. To send data between two UARTs, only two wires are required. The data travels from the sending UART's Tx pin to the receiving UART's Rx pin:
UART communication is asynchronous, which means there is no clock signal. Instead, the data packet is supplemented with start and stop bits. The beginning and end of the data packet are defined by these bits, so the receiving UART knows when to read the data.
The receiving UART will read the start bit at a certain baud rate when it detects it. The baud rate, which is measured in bits per second, is a measure of data transmission speed (bps). The two UARTs must operate at about the same baud rate, with a maximum baud rate difference of 10% between the sending and receiving UARTs.
Working principle
After obtaining parallel data from the data bus, the sending UART will combine it with a start bit, a parity bit, and a stop bit to generate a data packet, which it will output bit by bit via the Tx pin. The Rx pin of the receiving UART is connected to it. Bit by bit, read the data packet.
1 start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and 1 or 2 stop bits make up UART data:
Start bit:
When not transmitting data, the UART data transmission line is normally held at a high voltage level. The transmitting UART pulls the transmission line from high to low in one clock cycle when the transmission begins. When a high voltage to low voltage transition is detected by the receiving UART, it begins reading the data in the data frame at the baud rate.
Data Frame:
The actual data being communicated is contained in the data frame. If the parity bit is employed, the size of the data might range from 5 to 8 bits. The length of the data frame can be 9 bits if the parity bit is not used.
Check Digit:
The parity bit allows the receiving UART to verify whether any data changes have occurred during transmission. After receiving the data frame, the receiving UART will count the number of bits with a value of 1 and determine whether the total number is even or odd, as well as whether the data matches.
Stop bit:
The sending UART drives the data transmission line from a low voltage to a high voltage for at least two bits to signify the end of the data packet.
Transmission steps
1. The sending UART receives data from the data bus in parallel:
2. The starting bit, parity bit, and stop bit are added to the data frame by the sender UART:
3. From the sending UART to the receiving UART, the full data packet is transferred serially. The data line is sampled at a pre-configured baud rate by the receiving UART:
4. The receiving UART discards the data frame's start, parity, and stop bits:
5. At the receiving end, the receiving UART translates the serial data to parallel data and sends it to the data bus:
Pros and cons
Although no communication protocol is flawless, UART excels at what it does. Here are some advantages and disadvantages to help you decide whether they are appropriate for your project:
Advantage
Use only two wires.
There is no need for a clock signal.
Has parity bits for error detection
As long as both sides agree on the data packet's structure,
Method that is well-documented and extensively used
Shortcoming
The maximum data frame size is 9 bits.
Multiple slave systems or multiple master systems are not supported.
Each UART's baud rate must be within ten percent of one another.
Philips invented the I2C bus, which is a simple bidirectional two-wire synchronous serial bus. To send data, only two cables are required. It combines the benefits of SPI and UART in a single device. Numerous slaves can be connected to a single master (such as SPI), or multiple masters can govern one or more slaves. When you want numerous microcontrollers to log data to a single memory card or display text on a single LCD, this is a good option.
Data line for SDA (Serial Data Architecture).
The clock line SCL (Serial Clock).
Because I2C is a serial communication protocol, data is sent one bit at a time along SDA. I2C, like SPI, requires a clock synchronization signal, which is always managed by the host.
Working principle
I2C data is transmitted in the form of numerous msgs, each of which contains the slave's binary address frame and one or more data frames, which include start and stop conditions, read/write bits, and data frames. The ACK/NACK bit is as follows:
Start condition: When SCL is high, SDA switches from high to low.
Stop condition: When SCL is high, SDA switches from low to high.
Address frame: Each slave device has a unique 7-bit or 10-bit sequence that is used to identify the master and slave devices.
Read/write bit: a bit, which is low level if the master is providing data to the slave and high level if the slave is requesting data.
ACK/NACK: An ACK/NACK bit follows each frame in the message. The receiving device will return an ACK bit to indicate that the address frame or data frame was successfully received.
Addressing
Because I2C lacks a chip choose line like SPI, it must rely on another way to confirm a specific slave device, which is addressing.
The master provides each slave the slave address to communicate, and each slave compares it to its own address. It will transmit a low-level ACK bit to the host if the addresses match. If they don't match, no action is taken, and the SDA line remains high.
Read/write a bit
A read/write bit is located at the end of the address frame. It is low level if the master wants to transfer data to the slave. It is high if the host demands data from the slave.
Data Frame
The master can deliver the first data frame after detecting the slave's ACK bit. The data frame is always 8 bits long, and each data frame is followed by an ACK/NACK bit that confirms reception. The master can send a stop condition to the slave after sending all data frames to end the communication.
Transmission steps
1. The host initiates bus communication by switching the SDA line from high to low when the SCL line is high.
2. The host delivers the read/write bit and the 7-bit or 10-bit address of the slave to be communicated with to the bus:
3. Each slave checks the master's address against its own address. The slave sends an ACK bit by pulling the SDA line low if the addresses match. The slave pushes the SDA line high if the master's address does not match the slave's address.
4. Data frames are sent or received by the host:
5. The receiving device sends another ACK bit to the sender after each data frame is transmitted to ensure that the frame was successfully received:
6. The master then switches SCL to high level and subsequently SDA to high level, sending a stop condition to the slave.
Single master VS multiple slaves
Because I2C uses the addressing function, a single master can control numerous slaves. Up to 128 (27) unique addresses can be used with 7-bit addresses. Although 10-bit addresses are uncommon, 1,024 (210) unique addresses can be provided. If you want to link numerous slaves to a single master, use a 4.7K ohm pull-up resistor, such as by connecting the SDA and SCL lines to Vcc:
Multiple masters VS multiple slaves
Multiple masters can be connected to multiple slaves at the same time using I2C. There will be issues if two masters try to send or receive data over the SDA line at the same time. As a result, before sending a message, each host must determine if the SDA line is low or high. If the SDA line is low, it signifies that the bus is being controlled by another host. Data can be sent safely if the SDA line is high. Use a 4.7K ohm pull-up resistor to link the SDA and SCL lines to Vcc if you wish to connect multiple masters to multiple slaves:
Pros and cons
I2C may appear difficult when compared to other protocols. Here are some advantages and disadvantages to help you decide whether they are appropriate for your project:
Advantage
Only use two wires
Support multiple masters and multiple slaves
The baud rate of each UART must be within 10% of each other
Hardware is simpler than UART
Well-known and widely used protocol
Shortcoming
Data transfer rate is slower than SPI
The size of the data frame is limited to 8 bits