Sometimes, microprocessors such as the Raspberry Pi need to communicate with sensors, devices, or other microprocessors. In the computing world, there are a few standardized communication protocols to make communication with any sensor on any microprocessor fairly simple.
These communication protocols can be categorized into a few separate categories, depending on how they operate.
A serial communication line sends one bit of data at a time, through a single wire or radio frequency. These communication methods are simple, but also relatively slow. A common example is USB, which stands for Universal Serial Bus.
Parallel communication can send multiple bits of data simultaneously, through multiple wires. Some common examples of parallel interfaces are HDMI, PCIe, and DVI.
Communication between devices can be one-way or two-way. A simplex communication protocol only allows communication in one direction. This would mean that only one device sends information, and only one device receives information.
Duplex communication allows simultaneous two-way communication between devices. Both devices are capable of sending and receiving data at the exact same time.
Half-Duplex communication allows two-way communication, but not simultaneously. Only one channel for communication is open, and the devices must share them to either send or receive data.
When communicating at high speeds, it is very useful to accompany data signals with a clock signal. This clock helps keep both devices synchronized, and allows very fast, continuous data transmission. As the name implies, this is known as synchronous communication.
Asynchronous communication does not use a clock signal. Instead, one byte of data is sent at a time, before waiting a short while to send the next byte. The space keeps each byte of data distinct. Both devices use a previously-agreed upon baud rate, or the frequency of communication, per second.
When more than two devices need to communicate with one another, or if you have a "main" device that may need to communicate with several other components, a master-slave configuration is often very useful.
In this setup, one device is deemed the "master," and is responsible for managing the other "slave" devices. The master is usually responsible for sending a clock signal to other devices to keep them in sync, as well as telling other devices when to start listening for commands or when to send data. The slave devices usually only communicate when the master device instructs them to do so.
The Serial Peripheral Interface, or SPI for short (pronounced "spy"), is one of the most commonly used communication interfaces. It utulizes a master-slave paradigm, is synchoronous, is duplex, and as the name states, is also a serial interface. These features make SPI a relatively fast communication protocol, but also slightly more complex.
SPI must use at least 4 separate wires to communicate between devices:
The Serial Clock, or SCLK wire, carries a clock signal from the master device to the slave, to allow for synchronous, high speed communication.
The Master-Out-Slave-In wire, or MOSI wire, carries data from the master to the slave.
The Master-In-Slave-Out wire, or MISO wire, carries data from the slave back to the master.
The Slave-Select wire, or SS wire, is used to enable a particular slave device. When the SS wire has a low signal, the slave is enabled, otherwise it is disabled.
It is possible for multiple slave devices to share the same clock and data bus, to allow for savings. However, each slave device needs its own SS wire directly connecting it to the master device.
When multiple devices are being used on a single data line, but the master only needs to communicate with a single device, it can "select" a slave by altering the SS signals on each device. Then it can send a signal over the shared bus, and the rest of the slaves will ignore the signal.
The Inter-Integrated Circuit interface, or I2C interface (pronounced either "I-Squared-C" or "I-2-C") is another very commonly used interface. Just like SPI, it utilizes a master-slave paradigm, is synchronous, and is a serial interface.
The main difference between SPI and I2C is that I2C is half-duplex, while SPI is duplex. Because of this, I2C is much slower than SPI, however it has two main benefits over SPI: scalability and ease of use.
I2C only uses two wires: A Serial Clock wire (SCK) and a single Serial Data wire (SDA). The serial data wire can send data in both directions, while SPI has two wires dedicated for data transmission.
I2C, just like SPI, can connect several slave devices to a single master. Up to 127, to be exact. However, I2C does not need a chip-select wire, unlike SPI. Rather, it uses an addressing system to keep track of all devices on the bus.
Every slave device is given a pre-configured 7-bit address. Then, every messsage sent from the master is prefaced with the destination address. Every slave device will listen for their address, and only respond to commands sent to them.
A Universal Asynchronous Receiver-Transmitter, or UART, is another commonly used communication protocol. It is duplex, asynchronous, and serial.
Because it is asynchronous, there is no "clock" signal used in a UART connection. Rather, a pre-determined baud rate is set, and transmissions are sent and received at that rate. Each device then uses their own internal clocks to "time" the signal correctly.
Only two wires are used in a UART connection - a transmission line (Tx) and a receiver line (Rx). A "Tx" port on one device connects to the "Rx" port on the other device, and vice versa.
UART is the slowest of the three protocols mentioned before, because of the fact that it is synchronous.