Serial Communication

### What is Serial Communication?

Serial communication is a method of using signals to send data. It's commonly used when passing data between integrated circuits and microcontrollers. Information is passed bit by bit, literally. There are many different implementations of serial communication. Let's go over some of the most common ones.

### UART

UART, or Universal Asynchronous Receiver-Transmitter, is a very common and cheap way to provide serial communication. UART always has at least two signal wires: a receiving line (RX) and a transmitting line (TX). You will also likely see a power and ground line to match the signaling voltages between the two devices. UART communication is between only two devices. The devices must agree beforehand on a couple of settings in order to communicate. A mismatch in settings will cause a lot of weird symbols to be detected, and thus cause a miscommunication. These settings are usually programmed into the devices ahead of time, but some devices are also configurable. So what are these settings?

Standard UART Hookup

#### Baud Rate

Baud rate is how fast the communication is taking place. A Baud is actually a unit of measurement name after Emile Baudot, who invented a system of transmitting information using the telegraph. Baudot code is similar to Morse code and you can find out more information here.

A baud is defined as the number of symbols that you can send in a second. In the case of serial communication for a microcontroller, it's how many 1s or 0s you can send in a second. It's more costly in terms of system resources to send at higher baud rates. So, that could mean more expensive hardware or more power consumption. Both devices must be on the same baud rate in order to communicate. If there is a mismatch information will be lost or decoded into the wrong symbols.

Baud Rates Visualized

#### Start and Stop Bits

These are symbols found at the beginning and end of a message that tell you if it's the beginning or the end. A UART receiver will be listening for the start symbols and only begin recording a message after a start bit. The stop bits tell the receiver when the message is over.

Both devices in a UART pair must agree on the amount of bits or symbols to use for start and stop. You would use longer start and stop bits when the medium you are using for communication is noisy and you want to be extra sure your message is getting across. That could either mean the information is very important or the channel you are sending on might have a lot of interference that disrupts communication.

#### Parity Bit

A parity bit is a single bit or symbol used for "cheap" error checking. The transmitter of a message will add up the bits in the message and that number will either be odd or even, making the parity bit 1 or 0, respectively. The parity bit tells the receiver if the message sum should be odd or even, providing a simple check for if the message was received correctly. This method of error checking is very simple and can fail if more than one bit in the message is incorrect, but it costs very little in system resources, so it's common to see it implemented. The devices must agree on if parity bits are to be used.

There are two variants of parity bits: even and odd. If the UART is using even parity, the parity bit will be a 0 when the count of bits whose value is 1 is even, and a 1 when the count of bits whose value is 0 is odd. In odd parity, the opposite is true. The parity bit will be a 0 when the count of bits whose value is 1 is odd, and 1 when it is even.

#### Data Bits

These bits are the binary expression of the data you are sending. So, if a sensor is reporting back a value of 26, it would be converted to binary and be transmitted as 11010 . Devices must agree on how many data bits are to be sent.

#### Sending the Data

With all these settings agreed upon, the UART channel can be used between two devices. When data is ready to be sent, it is packaged into a frame. A frame consists of the start bit, then data bits, followed by a parity bit, and, lastly, the stop bits. This frame is sent through one of the device's TX lines (at the speed set by the baud rate) to the other device's RX line, where it is received and processed by the device.

### SPI

SPI or Serial Peripheral Interface is another way of sending data between devices. It differs from UART in that SPI is Synchronous. This means that the devices sync up their clocks and have no need to use start and stop bits or agree on a baud rate. This setup needs more connections, using at least 4 wires to communicate.

#### Clock Line

One device in the SPI setup will generate the clock signal that all other devices will use. This device is known as the master. There is no need for baud rates since all data transfer will be based off this clock signal, which gets it's own wire.

#### MOSI Line

The next line is used for Master-Out-Slave-In (MOSI) communication. This is a data line for the master device to send to slave devices only. If a slave device needs to send data back it will have to do it on another line.

#### MISO Line

MISO stands for Master-In-Slave-Out and is a data line used only for slave devices to send back information.

#### Slave Select Line

The last line is called the Slave Select (SS) line. It is also sometimes called the Chip Select or CS line. It is used to indicate to slave devices that information is about to be sent. SPI can have multiple slave devices, so a separate SS line would be used for each device that the master wants to communicate with. If the SS line is pulled low, the slave device knows to listen, while if it is high, the slave device ignores the message until it is selected again.

Due to the design (multiple lines for communication) SPI can be much faster than UART and can communicate with more than two devices.

Communication needs at least 4 lines and that number increases with more devices being used. This comes with the obvious area trade-off. SPI requires additional data lines for routing, additional pins on the master device for each slave device SS line, and, ultimately, increased physical area to allow for these lines.

### I2C

I2C or Inter-integrated Circuit protocol is a communication protocol that borrows the best bits from UART and SPI. It only uses two wires like UART, but it can support more than 2 devices like SPI. I2C substitutes the Slave Select hardware method for setting up device communication with a software addressing structure. So, instead of a physical wire for chip select/slave select, it is done via software addresses.

#### Lines

I2C has a clock line and a data line. Master to slave and slave to master communication happens over the same data line.

#### Protocol

The master will first send the address of which slave it wants to communicate with over the data line. The slaves all listen on the data line, and if their address isn't called, they stop listening. The master then sends the data it wants to send and the only device still listening for data is the one the master addressed.