Universal Asynchronous Receiver Transmitter or UART is one of the oldest and most used protocols.
- It uses one line for transmitting data and one for receiving data. These are usually referred to as the TX and RX lines.
- Most often 8-bit data is transferred, as follows:
- 1 start bit (low level),
- 8 data bits
- 1 stop bit(high level)
- The low-level start bit and high-level stop bit mean that there’s always a high to low transition at the start of the communication. This is a defining feature of UART
- The microcontrollers who want to communicate via UART have to agree on the transmission speed, the bit rate, as they have only the start bit’s falling edge to understand the start of the message.
- Communication may be simplex or half-duplex
Serial Peripheral Interface or SPI is an interface bus commonly used to send data between microcontrollers and small peripherals such as shift registers, sensors and SD cards.
- This uses a clock, hence synchronous
- It uses four lines:
- Clock line (SCK),
- Master Input, Slave Output(MISO) for the master to receive and slave to transmit,
- Master Output, Slave Input(MOSI) for the master to transmit and slave to receive
- Slave Select(SS) for selecting one among multiple slaves with which communication is desired.
- Note that master is defined as the micro-controller which sends the clock signal
- SPI operates in full-duplex mode
Inter – Integrated Circuit or I2C can handle 128 slaves using just 2 lines.
- It uses only two lines: One for data (SDA) and one for clock (SCL)
- The slaves are not selected via a slave select line but via address bits.
- The first byte sent by the master contains a seven-bit address and a read/ write bit indicating whether the next bytes will come from the master or should come from the slave.
- After each byte, the receiver must send a 0 to acknowledge the reception of the byte
- If the master wants to receive the data, it only generates clock pulses. The slave has to make sure that the next bit is ready when the clock pulse arrives
- The data and clock lines are pulled up, i.e. two resistors pull the bus to a high level and the devices only send low levels. If they want to send high level, they simply release the bus.
- Atmel uses the word TWI (two-wire interface) which is the same as I2C, so any AVR device will not have I2C in the documentation but will have TWI. The Arduino library for I2C is appropriately called Wire.
Comparison of the protocols
|Communication Type||Half Duplex||Full Duplex||Half Duplex|
|Number of Lines||2||4||2|
|Type of Communication||Asynchronous||Synchronous||Synchronous|
|Need for start/ stop bit||Yes||No||Yes (start/stop sequence required)|
|Slave Selection Method||–||SS Line||Address of the Slave|
- SPI is the fastest among the 3 protocols
- SPI has lower power requirements than I2C
- I2C requires only two pins for multiple slaves. SPI requires at least 4 pins, with one additional SS pin for each additional slave.
References and suggested reading:
- Wikipedia pages of the protocols are strongly recommended