Arduino

Reference Language | Libraries | Comparison | Changes

## SPI library

This library allows you to communicate with SPI devices, with the Arduino as the master device. ### A Brief Introduction to the Serial Peripheral Interface (SPI) Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers. With an SPI connection there is always one master device (usually a microcontroller) which controls the peripheral devices. Typically there are three lines common to all the devices: MISO (Master In Slave Out) - The Slave line for sending data to the master, MOSI (Master Out Slave In) - The Master line for sending data to the peripherals, SCK (Serial Clock) - The clock pulses which synchronize data transmission generated by the master and one line specific for every device: SS (Slave Select) - the pin on each device that the master can use to enable and disable specific devices. When a device's Slave Select pin is low, it communicates with the master. When it's high, it ignores the master. This allows you to have multiple SPI devices sharing the same MISO, MOSI, and CLK lines. To write code for a new SPI device you need to note a few things: Is data shifted in Most Significant Bit (MSB) or Least Significant Bit (LSB) first? This is controlled by the SPI.setBitOrder() function. Is the data clock idle when high or low? Are samples on the rising or falling edge of clock pulses? These modes are controlled by the SPI.setDataMode() function. What speed is the SPI running at? This is controlled by the SPI.setClockDivider() function. The SPI standard is loose and each device implements it a little differently. This means you have to pay special attention to the device's datasheet when writing your code. Generally speaking, there are four modes of transmission. These modes control whether data is shifted in and out on the rising or falling edge of the data clock signal (called the clock phase), and whether the clock is idle when high or low (called the clock polarity). The four modes combine polarity and phase according to this table:
Mode | Clock Polarity (CPOL) | Clock Phase (CPHA)
SPI_MODE0 | 0 | 0
SPI_MODE1 | 0 | 1
SPI_MODE2 | 1 | 0
SPI_MODE3 | 1 | 1
The `SPI.setDataMode()` function lets you set the mode to control clock polarity and phase. Every SPI device has a maximum allowed speed for SPI Bus. The SPI.setClockDivider() allows you to change the clock speed to make your device working properly (default is 4MHz). Once you have your SPI parameters set correctly you just need to figure which registers in your device control which functions, and you're good to go. This will be explained in the data sheet for your device. For more on SPI, see Wikipedia's page on SPI. ### Connections The following table display on which pins the SPI lines are broken out on the different Arduino boards:
Arduino Board | MOSI | MISO | SCK | SS (slave) | SS (master)
Uno or Duemilanove | 11 or ICSP-4 | 12 or ICSP-1 | 13 or ICSP-3 | 10 | -
Mega1280 or Mega2560 | 51 or ICSP-4 | 50 or ICSP-1 | 52 or ICSP-3 | 53 | -
Leonardo | ICSP-4 | ICSP-1 | ICSP-3 | - | -
Due | ICSP-4 | ICSP-1 | ICSP-3 | - | 4, 10, 52
Note that MISO, MOSI, and SCK are available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on every board. #### Note about Slave Select (SS) pin on AVR based boards All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative. It is, however, possible to use any pin as the Slave Select (SS) for the devices. For example, the Arduino Ethernet shield uses pin 4 to control the SPI connection to the on-board SD card, and pin 10 to control the connection to the Ethernet controller. #### Extended SPI functionality for the Due The Arduino Due's SPI interface works differently than any other Arduino boards. The library can be used on the Due with the same methods available to other Arduino boards or using the extended methods. The extended methods exploits the the SAM3X hardware and allows some interesting features like: automatic handling of the device slave selection. automatic handling of different device configurations (clock speed, data mode, etc) so every device can have its own configuration automatically selected. Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52). Due Extended SPI usage | #### Functions begin() end() setBitOrder() setClockDivider() setDataMode() transfer() Due Extended SPI usage #### Examples BarometricPressureSensor: Read air pressure and temperature from a sensor using SPI SPIDigitalPot: Control a digital potentiometer using SPI #### See also shiftOut() shiftIn()

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

### Share

### This link has expired.
Please re-subscribe to our Newsletters.

### Subscribe to our Newsletters

Email

Please enter a valid email to subscribe

Arduino Newsletter
Arduino Store Newsletter

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy

###### Newsletter

- ©2015 Arduino
- Copyright Notice
- Contact us
- Careers