SPI protocol

Serial Peripheral Interface or SPI is a synchronous serial communication protocol that provides full – duplex communication at very high speeds. Serial Peripheral Interface (SPI) is a master – slave type protocol that provides a simple and low cost interface between a microcontroller and its peripherals

SPI Interface bus is commonly used for interfacing microprocessor or microcontroller with memory like EEPROM, RTC (Real Time Clock), ADC (Analog – to – Digital Converters), DAC (Digital – to – Analog Converters), displays like LCDs, Audio ICs, sensors like temperature and pressure, memory cards like MMC or SD Cards or even other microcontrollers. We have seen about UART in the previous article. In UART (or any common serial port), where the communication happens over RX and TX line, there is no clock signal i.e. it is an asynchronous communication. In this type of communication, there is no control over the data sent or whether the transmitter and receiver have same data rates.

In order to overcome this, UART uses synchronization bits i.e. start bit and Stop bits and also a pre agreed data transfer speeds (typically 9600 bps). If the baud rates of transmitter and receiver are not matched, the data sent from the transmitter will not reach the receiver properly and often garbage or junk values are received.



What’s Wrong with Serial Ports?

A common serial port, the kind with TX and RX lines, is called “asynchronous” (not synchronous) because there is no control over when data is sent or any guarantee that both sides are running at precisely the same rate. Since computers normally rely on everything being synchronized to a single “clock” (the main crystal attached to a computer that drives everything), this can be a problem when two systems with slightly different clocks try to communicate with each other.

To work around this problem, asynchronous serial connections add extra start and stop bits to each byte help the receiver sync up to data as it arrives. Both sides must also agree on the transmission speed (such as 9600 bits per second) in advance. Slight differences in the transmission rate aren’t a problem because the receiver re-syncs at the start of each byte.

(By the way, if you noticed that “11001010” does not equal 0x53 in the above diagram, kudos to your attention to detail. Serial protocols will often send the least significant bits first, so the smallest bit is on the far left. The lower nibble is actually 0011 = 0x3, and the upper nybble is 0101 = 0x5.)

Asynchronous serial works just fine, but has a lot of overhead in both the extra start and stop bits sent with every byte, and the complex hardware required to send and receive data. And as you’ve probably noticed in your own projects, if both sides aren’t set to the same speed, the received data will be garbage. This is because the receiver is sampling the bits at very specific times (the arrows in the above diagram). If the receiver is looking at the wrong times, it will see the wrong bits.


SPI Working

SPI or Serial Peripheral Interface was developed by Motorola in the 1980’s as a standard, low – cost and reliable interface between the Microcontroller (microcontrollers by Motorola in the beginning) and its peripheral ICs.

Because of its simple interface, flexibility and ease of use, SPI has become a standard and soon other semiconductor manufacturers started implementing it in their chips.

In SPI protocol, the devices are connected in a Master – Slave relationship in a multi – point interface. In this type of interface, one device is considered the Master of the bus (usually a Microcontroller) and all the other devices (peripheral ICs or even other Microcontrollers) are considered as slaves.

In SPI protocol, there can be only one master but many slave devices.

The SPI bus consists of 4 signals or pins. They are

  • Master – Out / Slave – In (MOSI)
  • Master – In / Slave – Out (MISO)
  • Serial Clock (SCLK) and
  • Chip Select (CS) or Slave Select (SS)

Master – Out / Slave – In or MOSI, as the name suggests, is the data generated by the Master and received by the Slave. Hence, MOSI pins on both the master and slave are connected together. Master – In / Slave – Out or MISO is the data generated by Slave and must be transmitted to Master. MISO pins on both the master and slave are ties together. Even though the Signal in MISO is produced by the Slave, the line is controlled by the Master. The Master generates a clock signal at SCLK and is supplied to the clock input of the slave. Chip Select (CS) or Slave Select (SS) is used to select a particular slave by the master.

Since the clock is generated by the Master, the flow of data is controlled by the master. For every clock cycle, one bit of data is transmitted from master to slave and one bit of data is transmitted from slave to master. This process happen simultaneously and after 8 clock cycles, a byte of data is transmitted in both directions and hence, SPI is a full – duplex communication. If the data has to be transmitted by only one device, then the other device has to send something (even garbage or junk data) and it is up to the device whether the transmitted data is actual data or not. This means that for every bit transmitted by one device, the other device has to send one bit data i.e. the Master simultaneously transmits data on MOSI line and receive data from slave on MISO line. If the slave wants to transmit the data, the master has to generate the clock signal accordingly by knowing when the slave wants to send the data in advance. If more than one slave has to be connected to the master, then the setup will be something similar to the following image.

Even though multiple slaves are connected to the master in the SPI bus, only one slave will be active at any time. In order to select the slave, the master will pull down the SS (Slave Select) or CS (Chip Select) line of the corresponding slave.

Hence, there must by a separate CS pin on the Master corresponding to each of the slave device. We need to pull down the SS or CS line to select the slave because this line is active low.


SPI Hardware

The hardware requirement for implementing SPI is very simple when compared to UART and I2C. Consider a Master and a single Slave are connected using SPI bus. The following image shows the minimal system requirements for both the devices

From the image, the Master device consists of a Shift Register, a data latch and a clock generator. The slave consists of similar hardware: a shift register and a data latch. Both the shift registers are connected to form a loop. Usually, the size of the register is 8 – bits but higher size registers of 16 – bits are also common.

During the positive edge of the clock signal, both the devices (master and slave) read input bit into LSB of the register. During the negative cycle of the clock signal, both the master and slave places a bit on its corresponding output from the MSB of the shift register.

Hence, for each clock cycle, a bit of data is transferred in each direction i.e. from master to slave and slave to master. So, for a byte of data to be transmitted from each device, it will take 8 clock cycles.


Applications of SPI

  • Memory: SD Card , MMC , EEPROM , Flash
  • Sensors: Temperature and Pressure
  • Control Devices: ADC, DAC, digital POTS and Audio Codec.
  • Others: Camera Lens Mount, touchscreen, LCD, RTC, video game controller, etc.



  • SPI is very simple to implement and the hardware requirements are not that complex.
  • Supports full – duplex communication at all times.
  • Very high speed of data transfer.
  • No need for individual addresses for slaves as CS or SS is used.
  • Only one master device is supported and hence there is no chance of conflicts.
  • Clock from the master is configured based on speed of the slave and hence slave doesn’t have to worry about clock.


  • Each additional slave requires an additional dedicated pin on master for CS or SS.
  • There is no acknowledgement mechanism and hence there is no confirmation of receipt of data.
  • Slowest device determines the speed of transfer.
  • There are no official standards and hence often used in application specific implementations.
  • There is no flow control.


                                                                                       SPI pin out in Arduino

The image below shows the SPI pins present Arduino UNO (in red box).

SPI Line

Pin in Arduino


11 or ICSP-4


12 or ICSP-1


13 or ICSP-3



Using SPI in Arduino

Before start programming for SPI communication between two Arduino’s. We need to learn about the SPI library used in Arduino IDE.

The library <SPI.h> is included in the program for using the following functions for SPI communication.

  1. SPI.begin()

USE: To Initialize the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.

  1. SPI.setClockDivider(divider)

USE: To Set the SPI clock divider relative to the system clock. The available dividers are 2, 4, 8, 16, 32, 64 or 128.


  1. SPI.attachInterrupt(handler)

USE: This function is called when a slave device receives data from the master.

  1. SPI.transfer(val)

USE: This function is used to simultaneous send and receive the data between master and slave.

So now let’s start with practical demonstration of SPI protocol in Arduino. In this tutorial we will use two Arduino one as master and other as slave. Both Arduino are attached with a LED & a push button separately. Master LED can be controlled by using slave Arduino’s push button and slave Arduino’s LED can be controlled by master Arduino’s push button using SPI communication protocol present in Arduino.









WhatsApp us whatsapp