Improve your programming with Arduino: SPI communication

Introduction

In this tutorial, you will learn how to Improve your programming with Arduino: SPI communication, Among the most popular communication protocols for connecting peripherals to a microcontroller is the SPI protocol. It is a serial protocol through which you can transfer information between a master and a slave device quickly and efficiently.

Hardware Required

Components#Buy From Amazon
Arduino UNO1Buy Now
74HC595 ICBuy Now
Led 5mm8Buy Now
Resistor 1K8Buy Now
37 in 1 Sensors kit1Buy Now
Jumper WiresfewBuy Now
Breadboard1Buy Now
9v DC Adapter (Optional)1Buy Now

Summary

In this tutorial you will learn how to communicate an Arduino Uno with an 8-bit shift register using SPI communication, to display the data on 8 LEDs.

Mult-SPI-Communication-Arduino-Oled-Diagram

Application example of two SPI devices

SPI means Serial Peripheral Interface or Serial Peripheral Interface, this type of protocol allows high-speed communication to be established between one or more devices. The data transmission is carried out synchronously, that is, the data is sent every pulse of a clock signal. This type of communication allows data to be sent and received simultaneously, although it only allows one device to be the master.

To make this protocol work we have 4 main lines: CLK, MISO, MOSI and CS. CLK is the clock signal that manages the data, MOSI is the master’s output to the slave, and MISO is the master’s input from the slave to receive the data. CS is the output of the master that allows selecting the slave device to which it will send the data. The disadvantage of this protocol is that an output is required for each device to be controlled.

SPI-communication-Diagram

The communication takes the following process

  • The master sends the CS output to 0 to indicate to the slave that communication is going to take place.
  • The CLK clock output is enabled, which will generate a pulse signal that synchronizes the data sent/received.
  • Both the master and the slave read the first bit with the rising edge of the clock signal to transfer the bits, when the falling edge is given the next bit to be sent is prepared.
  • After the information is transferred, CS becomes 1, which ends the data transfer.

Let’s look at the code on Arduino to see the implementation. To use the protocol it is necessary to include the SPI.h library, and in this case we declare a slave selection pin, pin 2. We configure it as an output, establish the communication and within the loop we send the data to the SPI register.

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Installing Libraries

Now when you are Ready to upload the code, to the Arduino Board you will need first to add the Following Libraries in Arduino, If you Learn How to add the library in the Arduino step-by-step guide click on how to install the library Button given Blow

Code

//For more Projects: www.arduinocircuit.com

#include <SPI.h>
int slaveSelect = 2;    // This pin selects the slave to be controlled

int delayTime = 250;     // Pause between each data transfer

void setup() {
  pinMode(slaveSelect, OUTPUT);   // Configure the selector as output
  SPI.begin();                    // Start communication
  SPI.setBitOrder(LSBFIRST);       // The transfer order sends the least significant bit first
                                  // MSBFIRST sends the most significant bit first
}

void loop() {
  for (int i; i < 256; i++) {
    digitalWrite(slaveSelect, LOW);
    SPI.transfer(i);
    digitalWrite(slaveSelect, HIGH);
    delay(delayTime);
  }
}

This is a wiring diagram that we have to make. We will use the shift register 74HC595. As you can see, the SCK and MOSI pins for the Arduino UNO will always be pins 13 and 11, respectively. Since we do not receive data from the slave device we do not use MISO, but that corresponds to pin 12 of the Arduino UNO.

Circuit Diagram

SPI-communication-Arduino-Circuit
SPI-communication-Arduino-Circuit-Diagram

If all went well you should be able to observe the LEDs turning on and off incrementally, according to a binary count. In this case, the red LED is the one with the least weight, so it should blink faster. If you change the format to MSBFIRST the first green LED will now be the one with the best weight.

Look here for the code we just used: Repository

And with this we conclude the tutorial, this example served to quickly understand the communication protocol and implement a quick application, but you can use any SPI device, just follow the same sequence of steps.

Leave a Comment


error: