Communication protocols: UART and SPI

Electronics are all about interlinking individual circuits to create a larger system. In order for the individual circuits to exchange information/ “talk” with each other it is important that they speak the same language (or technically speaking share a common communication protocol).

Just as we have developed languages that we use for communication, electronic circuits speak in the binary language- a long string of information- made up of only 0’s and 1’s.

But even when they speak the same language, they should have a channel of communication between them (Think of it as the air that helps us talk to each other by transferring the vibrations caused by sound waves). There are hundreds of communication protocols that help circuits talk to each other. They can be broadly categorized into parallel or serial communication protocols.

Parallel vs. Serial protocols 

Parallel protocols transfer small packets of information (bits), simultaneously over multiple channels. Serial protocols stream the data, slowly, one single bit at a time.

Parallel communication is “fast, straightforward, and relatively easy to implement”. But it requires more input/output lines (more I/O pins). Often while working with the Arduino, because of constraints of the number of pins, we use serial communication.

Synchronous vs. Asynchronous serial protocols

A synchronous serial interface always pairs its data line(s) with a clock signal, so all devices share a common clock. This makes for a more straightforward, often faster serial transfer, but it also requires at least one extra wire between communicating devices. Examples include SPI and I2C.

An asynchronous serial interface is one where data is transferred without the support of an external clock signal. An advantage is that it reduces the number of wires and pins required, but extra effort has to be put into ensuring that data is being reliably transferred and received. Example: UART

UART

A universal asynchronous receiver/transmitter (UART) is a block of circuitry responsible for implementing asynchronous serial communication. The Arduino Uno that we use has 1 UART.

UARTs can both send and receive information. While asynchronously transferring data, we need to ensure that the Baud rate (how fast the data is being sent) is synchronized. There are standard baud rates starting from 1200 bits-per-second and going up to 115200 bits-per-second. The higher the baud rate the faster the data is transferred. (**Although if the speed becomes too high there may be errors in transmission.)

It is also important to note that data sent asynchronously is usually broken up into chunks/ packets. Each packet has the main “data”, which is the important information it is carrying. Then there are start bits and end bits, to mark the beginning and end of the packet. Sometimes there might be a parity bit that helps reduce errors while transmitting data over noisy mediums.

Essentially, the UART acts as an intermediary between parallel and serial interfaces. On one end of the UART are eight-or-so data lines (plus some control pins), on the other, is the two serial wires – RX and TX. So for example, on the Arduino, the digital pins send information in a parallel fashion (simultaneously), which is then transmitted by the UART as a steady stream.

While transmitting data, UART must create the data packet and send that packet out the Transmission line (TX) with precise timing (according to the set baud rate). On the receiving end, the UART has to sample the Receiving line (RX) at rates according to the expected baud rate, pick out the extra (stop+start+parity) bits, and spit out the data.

Advantages of UART:

  • It is a simple, well-known and widely used method
  • Variable types and sizes of data can be sent

Disadvantages of UART: 

  • It is designed to help two devices communicate at a time. If more than one device tries to send data through the same serial line, then it could pose a problem. For example, while trying to send data from the Arduino, through the Arduino IDE to Processing, only the serial monitor of one of the applications works at a time.
  • The size of each data packet is limited to 9 bits

SPI

It’s a “synchronous” way of transferring information, which means that it uses separate lines for data and a “clock” that keeps both sides in perfect sync. The clock is an oscillating signal that tells the receiver exactly when to sample the bits on the data line.

In SPI, only one side generates the clock signal (usually called CLK or SCK for Serial ClocK). The side that generates the clock is called the “master”, and the other side is called the “slave”. There is always only one master, but there can be multiple slaves.

When data is sent from the master to a slave, it’s sent on a data line called MOSI, for “Master Out / Slave In”. If the slave needs to send a response back to the master, the master will continue to generate the clock signal, and the slave will put the data onto a third data line called MISO, for “Master In / Slave Out”. The master must know in advance when a slave needs to return data and how much data will be returned.

While working with multiple slaves, we use something called Slave Select. This helps select a slave and tells it to wake up and receive/send data.

Advantages of SPI:

  • It’s faster than asynchronous serial
  • Communication can be established between a master and multiple “slaves”

Disadvantages of SPI:

  • Requires more signal lines (wires) than other communications methods
  • The communications must be well-defined in advance (you can’t send random amounts of data whenever you want)
  • The master must control all communications (slaves can’t talk directly to each other)
  • It usually requires separate SS lines to each slave, which can be problematic if numerous slaves are needed.

Resources

https://learn.sparkfun.com/tutorials/serial-communication

https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi

Basics of UART Communication

 

Leave a Reply

Your email address will not be published. Required fields are marked *