SPI stands for Serial Peripheral Interface and it is a way to send data between microcontrollers and other small devices. It is a synchronous data bus, meaning it uses a clock to regulate the data transfer.
SPI is also Full-Duplex communication meaning we can have data being sent and received simultaneously.
The SPI Master is the one that generates the clock (in our case this will be the Arduino). The SPI restricts us to having 1 Master and multiple slaves, meaning the master controls communication for all the slaves. There are 4 data lines that SPI uses:
- When the Master sends data to the Slave it uses a MOSI (Master out Slave In) Line
- When the Slave wants to send data back to the Master it uses a MISO (Master in Slave Out) Line
- These 2 lines are regulated by the SCK, which is Serial Clock.
- SS Line is the slave select line. It is held high by the master until it has data to be sent. In which case, the Master drops this line low. We call this Active Low logic as the line goes low causing the slave to become active. This is sometimes referred to as an Enable line.
Mode | Clock Polarity | Clock Phase |
SPI_Mode0 | 0 | 0 |
SPI_Mode1 | 0 | 1 |
SPI_Mode2 | 1 | 0 |
SPI_Mode3 | 1 | 1 |
The clock logic regulates the sending of data and it can be configured in 4 possible modes (see table). Clock polarity refers to the logic at which the clock is set when no data is being transferred. The Clock phase represents whether the data will be read by the devices on the rising or falling edges of the clock pulses. We can also arrange the master to send additional clock signals, allowing the slave to return data in the same transfer.
The data transfer sequence (For default active low slave configuration, using default clock phase):
Why would we use SPI?
- Some devices only support SPI.
- SPI is the fastest communication method available on the Atmega328 chip at a crazy 888,888 bytes per second (868KB/s).
- You can do both SPI and I2C simultaneously with ATmega328
Advantages of SPI | Disadvantages of SPI |
Faster than asynchronous | The master device controls everything |
Can utilize simple shift registers | Requires 4 wires minimum, going up the more slaves you have. |
Supports multiple slave | You need to have well-defined parameters for sending and receiving data |
The Project
We are going to use the Serial Peripheral Interface to communicate with a simple Shift Register to control 8 LEDs using the 4 data lines discussed above.
The Shift Register,
entering the world of integrated circuits
The shift register we will be using is the 74HC595N. It is a 16 pin, Serial-In-Parallel-out integrated circuit. This means the package itself has 16 pins and takes a serial input of data, breaks it down and outputs it to multiple pins. We will be using it to control 8 different LEDs using only the SPI pins from our Arduino. The shift register will be our slave device with the Arduino being the Master.
Aside: An integrated circuit is a packaged circuit that comes as a prebuilt circuit that we can interface with to fill a purpose. There are dozens of types of ICs available to consumers, today, however, we will start off using a Shift Register that is a DIP (Dual In-Line Package). Meaning the physical package has 2 parallel rows of pins.
The 74HC595 Shift Register | |||
Pin | Purpose | Name | Arduino Pin |
1 | Data Out Bit 1 | Q1 | |
2 | Data Out Bit 2 | Q2 | |
3 | Data Out Bit 3 | Q3 | |
4 | Data Out Bit 4 | Q4 | |
5 | Data Out Bit 5 | Q5 | |
6 | Data Out Bit 6 | Q6 | |
7 | Data Out Bit 7 | Q7 | |
8 | Ground | GND | |
9 | Serial Data Out | Q7S | |
10 | Master Reset (Active Low) | MR | |
11 | Shift Register Clock Input | SHCP | 13 |
12 | Storage Register Clock Input | STCP | 10 |
13 | Output Enable (Active Low) | OE | |
14 | Serial Data Input | DS | 11 |
15 | Data Out Bit 0 | Q0 | |
16 | Supply Voltage | VCC |
End Of Post