SPI in Arduino Uno

ArduinoSoftware & CodingHardware

SPI stands for Serial Peripheral Interface. It is a common protocol used for communication between microcontrollers and peripherals. SD Card is a popular peripheral that uses SPI for communication. Here are some salient features of SPI −

  • It uses four lines −

    • Clock line (SCK),

    • Master Input, Slave Output(MISO) for master to receive and slave to transmit,

    • Master Output, Slave Input(MOSI) for master to transmit and slave to receive

    • Slave Select(SS) for selecting one among multiple slave with which communication is desired.

  • Note that master is defined as the micro-controller which sends the clock signal

  • It operates in full duplex mode, meaning the master and slave can exchange data at the same time

  • It uses a clock and therefore it is a synchronous communication

  • One master can have multiple slaves. Which slave it is communicating to is determined by the Slave Select (SS) pin. If the SS pin of a slave is low, it means that that particular slave is selected for communication

  • There is no need for Start/Stop bit to indicate start or end of communication, unlike UART

On Arduino Uno, there is SPI support. The following pins are generally used for SPI −

  • MOSI − 11 or ICSP-4

  • MISO − 12 or ICSP-1

  • SCK − 13 or ICSP-3

  • SS − 10

Note that the ICSP pin refers to the 6 exposed pins at the end (see image below) −

Arduino has a built-in SPI library. The important functions of this library are given below −

  • SPI.begin() → Initialize SPI

  • SPI.transfer() → Transfer data to peripheral

  • SPI.beginTransaction() → Begin using SPI port

  • SPI.endTransaction() → End the current transactions (if other libraries are using SPI from interrupts, they will be prevented from accessing SPI until you call this function)

  • SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)) → Change SPI settings while beginning transaction. These settings will remain in place till another call to beginTransaction along with SPISettings alters these settings.

SPI has 4 modes (SPI_MODE0 is mentioned above), each having different configuration for Clock Polarity, Clock Phase, Output Edge and Data Capture. If you are a newbie, you need not concern yourself with these terms. You can go ahead with the default settings. If you wish to dive deep into the details, refer to the Wikipedia page of SPI here.

Also, the Arduino reference on SPI will help − https://www.arduino.cc/en/reference/SPI

You are encouraged to go through the built-in examples related to SPI, which can be accessed from File → Examples → SPI

raja
Published on 30-Jul-2021 14:45:57
Advertisements