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,

- Master In Slave Out (MISO) - The Slave line for sending data to the master,
- Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
- Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the master, and
- Slave Select pin - 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? This and the clock idling 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 three 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 three modes combine polarity and phase. The `SPI.setDataMode()` function lets you set the mode to control clock polarity and phase according to this table:

Mode | Clock Polarity (CPOL) | Clock Phase (CPHA)
--- | --- | ---
0 | 0 | 0
1 | 0 | 1
2 | 1 | 0
3 | 1 | 1

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

On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). MISO, MOSI, and SCK are also available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on the Uno and the Mega.

Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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.

#### Functions

- begin()
- end()
- setBitOrder()
- setClockDivider()
- setDataMode()
- transfer()

#### 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.

©Arduino | Edit Page | Page History | Printable View | All Recent Site Changes