46 – The SPI bus




    • Understand the problems of the serial communication.
    • Introduce the SPI bus.
    • Show how to connect only one or several slaves to the bus.
    • Pros and cons of SPI Bus versus I2C Bus.



Patience and willingness of spirit because this will be one of those theoretical chapters that, although can be skipped almost without worrying, it is worth reading to understand the concepts behind a communications bus.This chapter is almost a verbatim translation of the English Sparkfun page on the same subject, which can be found at:https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi.

The mistakes that can appear in this chapter are certainly mine, but the good things are directly attributable to Sparkfun.


Arduino UNO



If we already have a asynchronous data transmission protocol, as the serial ports we previously saw, why do we need another serial bus? Is there something wrong with the serial communication that we have not yet told?

Theoretically speaking there is no problem, but you know that the devil is in the details. We said that it was enough to define a common speed to transmit data between two points immediately, but the reality is stubborn.

We have no guarantee that both ends work, in fact, at the same speed, so we might send all the data and the other part could not realize, either because it does not work at the same speed or because it has its head on the clouds.

We have no way to ensure that the other part has received the message, simply we assume that everything went well.

  • This is a good time to mention Murphy’s Law, which says that if something can go wrong, will go wrong and certainly also at the worst time.
  • There are still many people who believe that Murphy was a pessimist, apart from being a funny guy, but he isn’t. Someday he will receive the recognition he deserves.

The first problem that usually appears is that the speeds at both ends are different. End of communication (I am sure that you have experienced this problem between Arduino and the console at some point by now)

The second problem is that the way to set the speed agreement must be done with a different clock at each end. That is, the two clocks have to reach an agreement, Murphy rubs his hands. It’s about time that minor differences between the quartz clocks that synchronize them lead to communication problems.

To correct this problem, in serial communication we agree terms for another aspect that so far had not mentioned. A pair of extra bits are added to the communication as one Start and one Stop bits. (Notice that in order to send 8 bits of data we have now to send 10 bits).

Serial communication

This way, increasing the serial transmission data by a 25% (8 bits / 2), we can synchronize the clocks at both ends using these Start and Stop bits.

But this also means to increase the hardware to decode the signal and synchronize clocks. And making things difficult, why do not we look for a synchronous solution to the problem?

The electronic industry gave some solutions, which include the I2C bus, that we have previously seen, and the SPI bus, that we are going to deal with.




The SPI bus is a 4 wire synchronous communication protocol between electronic devices introduced by Motorola in 1982, which has gained considerable popularity in the industry as a very short distance communication system, usually within the printed circuit board itself.

It is a transmission protocol that achieves very high speeds and was designed to be able to communicate a micro controller with various peripherals and operate at full duplex (A convoluted way of saying that data can be sent and received at the same time).

The SPI bus uses a synchronous solution, because it uses different lines for the data and the Clock. The Clock is a signal that indicates the listener exactly when the data lines must be read, so that the problem of loss of synchronization is uprooted.

Synchronous serial communication

For this reason, it is not necessary to agree terms for the transmission rate, as it will be the Clock which sets the speed, speed that can even vary along the communication without a problem (although, of course, according to the device there will be a speed limit) .

One of the reasons why the SPI bus is so popular is that the hardware receiver can be as simple as a shift register, as we have previously seen, which is a much simpler (and cheaper) solution than a serial communication UART (Universal Asynchronous Receiver Transmitter).




In a SPI bus the part that generates the Clock is called the master and the rest are called slaves (there can be one or several slaves on the bus). The clock signal, CLK or CLOCK, is often called SCK, short for Serial Clock.

When the master sends information, it does using a data line that is normally called MOSI (Master Out Slave In) and if the slave responds, it does so via a line called MISO (Master In Slave Out) following one of those bursting with imagination traditions in technology names.

SPI bus

As it is the master who generates the Clock, you need to know in advance if a slave will return a response and its data length to keep the clock on until the transfer is complete.

This is not usually a problem because when the master asks a sensor, for example, to send a read, we usually know that it
will be 2 bytes long.



There is a last control line, called Slave Select or SS, that is used to select a slave as the addressee of the incoming message or that it should sent a response to a request from the master.

SPI bus SS line

The SS line is normally HIGH and is activated setting it LOW, waking up the selected slave. When the transfer is completed the line rises to HIGH again and the slave is deactivated.




There are two ways to connect multiple slaves to an SPI bus. With a SS line for each slave or in cascade:

In general, each slave requires its own line SS to be activated, and so prevent two slaves from talking at once, because the result would be useless noise. Just activate the corresponding line and the slave will be ready to receive your orders

It is a comfortable system, as long as they are not many slaves, otherwise we will need many lines.

Multiple Slave Select

When the number of slaves grows, usually they are connected in a daisy chain, with one MISO (output), connected to the MOSI (input) as follows. In this case we use only a single SS line, which is shared among all slaves.

This is a typical configuration when the master only sends data but receives nothing back, as in the case of a chain of multiple LED displays (8 × 8 LED Matrices), in which the information to be displayed is sent but there is no data back. In this case we can even disconnect the MISO line.

SPI bus with multiple slaves in a chain

Finally and to close this chapter, we must comment that Arduino, naturally, supports the SPI bus and, surprisingly, it uses a standard library that is called SPI which manages all the complications and the arbitration protocol.




Advantages of the SPI Bus:

  • Full Duplex Communication. It can send and receive data at the same time increasing the speed.
  • It is faster than the I2C bus and the standard asynchronous serial port.
  • The message size can be as large as we want.
  • The hardware required is simple (that is, cheaper).
  • Requires lower consumption and less hardware to setup the bus than the I2C bus.
  • As it is the master the one that provides the clock signal, the slaves do not need oscillators (that is, it is cheaper again).

And as in life nothing is free, its disadvantages are:

  • It needs more lines than the normal I2C bus or the serial port.
  • Communications must be perfectly established beforehand. You can not send messages of different lengths at your will.
  • There is no acknowledge signal from the slave, it is expected to obey and that’s all.
  • There can be only one master and there is almost no possibility of using multiple masters.
  • Works only over very short distances
  • Normally an additional line for each slave is needed and if the number of slaves grows too much it can end up being a problem.




Although recently have appeared some libraries that allow us to choose the SPI control pins in our Arduinos, traditionally these lines were fixed to a certain pins.

That means, that in order to control the SPI bus we need to use those pins unavoidably, but we can choose between two sets of them, including certain digital pins, depending on the model and the table shown below, and using their equivalents in the ICSP (In-Circuit Serial Programming) bus.

These pins are:

Arduino board MOSI MISO SCK SS Slave SS Master
UNO 11, ICSP-4 12, ICSP-1 13, ICSP-3 10 Z
MEGA 51, ICSP-4 50, ICSP-1 52, ICSP-3 53 Z
Leonardo ICSP-4 ICSP-1 ICSP-3 Z z
DUE ICSP-4 ICSP-1 ICSP-3 z 4,10,52
  • Note that although the SPI bus pins change according to the Arduino model we use, they are always the same in the ICSP.
  • It allows you to design valid Shields for all Arduino boards.

The ICSP pins are those on the right when we read the labels on the board: 

Arduino SPI pins
ICSP header





    • We have tried to show that there are occasions in which the serial port has serious limitations.
    • We have introduced the SPI bus and their control lines.
    • We have seen how to connect several slaves to the bus.
    • We have talked about the advantages and disadvantages of this bus with respect to the I2C bus, that we saw in previous chapters, and the standard serial communication.





No Comments

Give a Reply