ATmega328P/Talking to other parts/32. SPI and the ICSP header
Talking to other parts · 32 of 33

SPI and the ICSP header

SPI is the fast one: a clock the board drives, a wire out, a wire back and a select line, on pins 10 to 13 and again on the six-pin ICSP header. One jumper from 11 to 12 is enough to watch a byte go out and come back.

Your board

Four pins, and the same three again

PinSPI nameJob
10SSselect, as a master: keep it an output
11MOSIdata out of the chip
12MISOdata into the chip
13SCKthe clock, which the chip drives

The six-pin ICSP header, male pins on the right edge, labelled ICSP, carries three of those again. The net list wires its MISO to D12, SCK to D13 and MOSI to D11, and adds 5V, GND and RESET. A programmer uses it to write the chip directly, bootloader and all. Some boards and shields take SPI from these six pins instead of from 10 to 13; they are the same wires either way, so a device on the ICSP header and another on 11 to 13 share one bus.

On the Nano, a programmer's cable, which usually ends in a socket too, needs a 2x3 strip of male header pins between the two, or six jumper wires.

D13 also drives the pin-13 lamp. On the UNO the lamp is driven through an LM358 buffer, so it takes nothing from the clock. The lamp follows the clock too, far too fast to see.

D10 has to stay an output

SPI.begin() makes 10, 11 and 13 outputs and turns on master mode. D10 matters even when nothing is connected to it. Microchip's datasheet (section 18.3.2): in master mode, if SS is an input and something pulls it LOW, the SPI hardware decides another master wants it, clears its own master bit and becomes a slave. SPI stops, with no error.

So D10 stays an OUTPUT. It can still be a device's chip-select line, which is what it is usually used for; it just cannot be an input.

One byte out and in

Eight clocks of SPI.transfer(0xA5)
0 of 8 clocks
The jumper from 11 to 12
Sent
0xA5
In the register
0xA5
transfer() returns
not yet
SPI.transfer(0xA5) puts the byte in one register and clocks it eight times. Each clock sends the top bit out on 11 and takes a bit in from 12 at the bottom.

The chip's SPI has a single 8-bit register. Each clock pulse on SCK sends its top bit out on MOSI and shifts the bit on MISO in at the bottom, so after eight clocks the byte that went out has been replaced by the byte that came in. That is why SPI.transfer(x) always returns a byte: sending and receiving are one operation.

With the jumper from 11 to 12, each bit that leaves comes straight back, and the sketch prints sent 0x2A got 0x2A match for every byte. That checks the clock, the register and both pins, without a device.

Pull the wire

Take the jumper out with the sketch running. Every byte now comes back 0xFF: D12 has nothing driving it and reads its pull-up as a 1, eight times. The sketch turns that pull-up on with pinMode(12, INPUT_PULLUP); the datasheet says the pull-up still works while SPI forces the pin to be an input.

Without that line, D12 floats and the result is whatever the open pin drifts to, usually all 0s or all 1s and sometimes a mix. That is also how a missing device looks in real code: 0xFF or 0x00 from every read usually means nothing answered, not that it sent those values.

The code

SPI_Loopback.ino

Upload it first, then put one jumper wire from 11 to 12 and open the Serial Monitor at 9600 baud. Twice a second it sends the next byte with SPI.transfer() and prints what came back.

// SPI_Loopback.ino — sends a byte out of D11, reads it back on D12
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: upload first, then one jumper wire from 11 to 12.
//         D12 (MISO) is the input; D11 (MOSI) drives it.
//         Pull the wire before you upload a different sketch.
#include <SPI.h>

byte out = 0;

void setup() {
  Serial.begin(9600);
  pinMode(12, INPUT_PULLUP);  // with no wire, MISO reads 1s
  SPI.begin();                // 10, 11 and 13 become outputs
}

void loop() {
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  byte in = SPI.transfer(out);   // 8 bits out, 8 bits in
  SPI.endTransaction();

  Serial.print(F("sent 0x"));
  printHex(out);
  Serial.print(F("  got 0x"));
  printHex(in);
  Serial.println(in == out ? F("  match") : F("  different"));

  out++;
  delay(500);
}

void printHex(byte b) {
  if (b < 0x10) Serial.print('0');
  Serial.print(b, HEX);
}

D12 is the input and D11 drives it, so the wire is a pin-to-pin link and safe. Pull it out before uploading any other sketch, which may make both pins outputs.

When it does not work

Every byte comes back as 0xFF

Nothing is driving D12, so it reads its own pull-up. Check that the jumper is in 11 and 12, not 10 or 13. With a real device, the same 0xFF usually means it is not selected: its CS line never went LOW.

Bytes come back random with no wire

D12 is floating. The sketch turns its pull-up on with pinMode(12, INPUT_PULLUP); a sketch that leaves that line out reads whatever the open pin has drifted to, often 0x00 or 0xFF, sometimes a mix.

SPI stops working after I used D10 for a button

D10 is the chip's SS pin. If it becomes an input and goes LOW, the SPI hardware switches itself to slave mode and stops sending. Keep D10 an OUTPUT in any sketch that uses SPI, and put the button on another pin.

Two SPI devices answer at once and the data is nonsense

Both are selected. Every device on the bus, on the ICSP header or on 11 to 13, needs its own chip-select pin, and only one may be LOW at a time. Set every CS pin HIGH in setup() before the first transfer.

Where this goes next

One build on the board alone: a random wait, a jumper wire to GND, and your best time kept in EEPROM.

Build: a reaction timer →

Edit this page — content/books/atmega328p/spi-and-the-icsp-header.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →