Show minor edits - Show changes to markup
| Uno or Duemilanove | 13 or ICSP-4 | 11 or ICSP-1 | 12 or ICSP-3 | 10 | - |
| Uno or Duemilanove | 11 or ICSP-4 | 12 or ICSP-1 | 13 or ICSP-3 | 10 | - |
Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52). Pin 78 is an SS pin connected to the on-board external data flash memory, used to store variables while the board is powered off.
Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52).
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
| SPI_MODE0 | 0 | 0 |
| SPI_MODE1 | 0 | 1 |
| SPI_MODE2 | 1 | 0 |
| SPI_MODE3 | 1 | 1 |
(:cell width=35%:)
(:cell width=10%:)
(:cell width=35%:)
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,
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:
and one line specific for every device:
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. The SPI.setClockDivider() allows you to change the clock speed (default is 4MHz), and the SPI.setDataMode() function lets you set the mode to control clock polarity and phase according to this table:
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:
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.
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.
| MOSI | MISO | SCK | SS | |
| Uno / Duemilanove | 13 - ICSP-4 | 11 - ICSP-1 | 12 - ICSP-3 | 10 |
| Mega1280 - Mega2560 | 51 - ICSP-4 | 50 - ICSP-1 | 52 - ICSP-3 | 53 |
| Leonardo | ICSP-4 | ICSP-1 | ICSP-3 | - |
| Due | ICSP-4 | ICSP-1 | ICSP-3 | 4, 10, 52 |
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.
| Arduino Board | MOSI | MISO | SCK | SS (slave) | SS (master) |
| Uno or Duemilanove | 13 or ICSP-4 | 11 or ICSP-1 | 12 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 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.
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off.
The SPI library can be used on the Due with the standard methods available to other Arduino boards based on AVR microcontrollers, or in the extended mode. This mode extends the standard methods, by using the features of the SAM3X microcontroller. This enables automatic handling of the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).
(:cell width=10%:)
(:cell width=35%:)
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.
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:
Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52). Pin 78 is an SS pin connected to the on-board external data flash memory, used to store variables while the board is powered off.
(:cell width=10%:)
(:cell width=35%:)
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. The SPI.setDataMode() function lets you set the mode to control clock polarity and phase according to this table:
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. The SPI.setClockDivider() allows you to change the clock speed (default is 4MHz), and the SPI.setDataMode() function lets you set the mode to control clock polarity and phase according to this table:
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off.
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:
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. The SPI.setDataMode() function lets you set the mode to control clock polarity and phase according to this table:
| Uno / Duemilanove | 13 | 11 | 12 | 10 |
| Mega1280 / Mega2560 | 51 | 50 | 52 | 53 |
| Leonardo | ICSP | ICSP | ICSP | ICSP |
| Due | ICSP | ICSP | ICSP | 4, 10, 52 |
| Uno / Duemilanove | 13 - ICSP-4 | 11 - ICSP-1 | 12 - ICSP-3 | 10 |
| Mega1280 - Mega2560 | 51 - ICSP-4 | 50 - ICSP-1 | 52 - ICSP-3 | 53 |
| Leonardo | ICSP-4 | ICSP-1 | ICSP-3 | - |
| Due | ICSP-4 | ICSP-1 | ICSP-3 | 4, 10, 52 |

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. Differently to the other boards the Due provides only 4 pins for the Slave Select line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off.
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.
The Arduino Due differently to the other Arduino boards provides only four pins for the Slave Select (SS) line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.
With the Due the SPI library can be used in two different ways, with the standard methods shared with the other Arduino boards based on AVR microcontrollers, or in the extended mode, that extend the standard methods, that consists in using the feature of the microcontroller (SAM3X) that automatically handle the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).
| Mode | Clock Polarity (CPOL) | Clock Phase (CPHA) |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
| Mode | Clock Polarity (CPOL) | Clock Phase (CPHA) |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
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.
The following table display on which pins the SPI lines are broken out on the different Arduino boards:
| MOSI | MISO | SCK | SS | |
| Uno / Duemilanove | 13 | 11 | 12 | 10 |
| Mega1280 / Mega2560 | 51 | 50 | 52 | 53 |
| Leonardo | ICSP | ICSP | ICSP | ICSP |
| Due | ICSP | ICSP | ICSP | 4, 10, 52 |
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. Differently to the other boards the Due provides only 4 pins for the Slave Select line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off.
(:tableend:)
(:tableend:)
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). 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 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.
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.
| Mode | Clock Polarity (CPOL) | Clock Phase (CPHA)''' |
|---|
| Mode | Clock Polarity (CPOL) | Clock Phase (CPHA) |
|---|
(:table:)
(:cell width=55%:)
(:table border=1 cellpadding=0 cellspacing=0:) (:cellnr:) Mode (:cell:) Clock Polarity (CPOL) (:cell:) Clock Phase (CPHA) (:cellnr:)0 (:cell:)0 (:cell:)0 (:cellnr:)1 (:cell:)0 (:cell:)1 (:cellnr:)2 (:cell:)1 (:cell:)0 (:cellnr:)3 (:cell:)1 (:cell:)1 (:tableend:)
| Mode | Clock Polarity (CPOL) | Clock Phase (CPHA)''' |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
(:cell width=10%:)
(:cell width=35%:)
(:tableend:)
This library allows you to communicate with SPI devices, with the Arduino as the master device.
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,
To write code for a new SPI device you need to note a few things:
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:
(:table border=1 cellpadding=0 cellspacing=0:) (:cellnr:) Mode (:cell:) Clock Polarity (CPOL) (:cell:) Clock Phase (CPHA) (:cellnr:)0 (:cell:)0 (:cell:)0 (:cellnr:)1 (:cell:)0 (:cell:)1 (:cellnr:)2 (:cell:)1 (:cell:)0 (:cellnr:)3 (:cell:)1 (:cell:)1 (:tableend:)
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.
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). 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 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.