Learn

Two Arduinos as an SPI master and slave

Advanced

Or see every lesson.

  • 261parts on the bench
  • 31boards running now
  • 1.00xreal time, on every board
An SPI master and slavelive0.000 s 0.00x
Click to open it in the editor
The master sends a counter; the slave answers each byte with twice the byte it got the time before. Press Run and read both boards.

SPI is the fast, simple bus between a microcontroller and the chips around it: displays, SD cards, radios, ADCs. It is four wires. SCK is the clock, which only the master drives. MOSI carries data from the master out to the slave, and MISO carries data from the slave back in. SS, slave select, goes low to tell one slave that the next clocks are for it.

The thing that makes SPI click is that it is a swap. On every clock the master shifts one bit out on MOSI and the slave shifts one bit out on MISO, so after eight clocks each side holds the other’s byte. There is no separate read: the master learns what the slave had to say in the same eight clocks it used to talk.

The project above wires two Unos the way a master and a slave are wired, pin 10 to 10, 11 to 11, 12 to 12 and 13 to 13, plus a shared ground. The master pulls SS low, clocks out a counter one bit at a time in SPI mode 0 (data set while the clock is low, sampled on the rising edge, most significant bit first), and lets SS go. The slave waits for SS, puts its answer bit on MISO before each rising edge, and reads MOSI on it. Its answer is twice the byte it received the time before, so when the master sends 4 it hears 6 back, and you can see the bytes really went both ways.

Both sides are written out by hand rather than handed to the SPI library, and the clock runs slowly, 5 kHz, so the slave keeps up from a plain loop. That is deliberate: every edge of the protocol is in code you can read, step through in the debugger and change. Mokxi does not simulate the Uno’s hardware SPI peripheral as a slave, so a sketch that sets the SPI registers up in slave mode will not answer here; the master side of the SPI library is what drives displays and sensors in the other projects.

Watch pin 13 while it runs: it is the clock, and the Uno’s L LED is wired to it, so the LED flickers with every byte.

Things to try: change the slave’s answer, send two bytes per transaction, or speed the clock up until the slave starts missing edges and see what the master reads then.

Questions

Which pins does SPI use on an Arduino Uno?

Pin 13 is SCK, 12 is MISO, 11 is MOSI and 10 is the usual slave select. A master can use any free pin as the slave select for each extra slave.

Why does this project not use the SPI library?

To show every edge of the protocol in code you can read, and because Mokxi does not simulate the Uno’s SPI hardware in slave mode. The SPI library works as a master in Mokxi, which is how the display and sensor projects use it.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.