Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers.
In an SPI connection, there is always a master device (usually a microcontroller) that controls the peripheral devices.
SCK − This is the serial clock driven by the master that synchronizes the data transmission.
MOSI − This is the master output/slave input driven by the master.
MISO − This is the master input/slave output driven by the master.
SS − This is the pin on each device that the master can use to enable/disable specific devices.
When a device’s Slave Select pin is low, it communicates with the master. When it’s high, it ignores the master. This allows you to have multiple SPI devices sharing the same MISO, MOSI, and CLK lines.
SPI has four modes of operations:
Mode 0 (the default) − Clock is low (CPOL = 0), and the data is sampled on the transition from low to high (leading edge) (CPHA = 0).
Mode 1 − Clock is low (CPOL = 0), and the data is sampled on the transition from high to low (trailing edge) (CPHA = 1).
Mode 2 − Clock is high (CPOL = 1), and the data is sampled on the transition from high to low (leading edge) (CPHA = 0).
Mode 3 − Clock is high (CPOL = 1), and the data is sampled on the transition from low to high (trailing edge) (CPHA = 1).
The following figure shows the pins in Arduino UNO which are used for SPI communication.
Now, we’ll look at an example of SPI communication between two Arduinos. We’ll be configuring one Arduino as a master and another Arduino board as a slave. Following is the diagram for the connections between both the boards.
Note: The ground connection is common.
The Master unit sends ‘Hello World’ to the slave.
The slave device waits for the data, as soon as data arrives, the process variable becomes true indicating there is data in the buffer. In the main loop, we read this buffer and send it to the serial terminal.
Now let’s look at the functions that we used for the SPI communication between both boards.
Once you’ve compiled and uploaded the respective programs on both the boards, you’ll be able to see ‘Hello World’ on the serial monitor of the slave.
In this blog, we went through SPI communication in an Arduino board, its different modes of operations and saw a simple example that implemented the SPI communications protocol.