Built-in project · Arduino Uno R3

Two Arduino Unos as an SPI master and slave

One Uno clocks bytes out over SS, MOSI, MISO and SCK and the other answers in the same eight clocks, with both sides of SPI mode 0 written out in the sketches. The whole build, an Arduino Uno, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Advanced About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $54. On Mokxi they're free.

Two Arduino Unos as an SPI master and slave · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • The four SPI lines and what each one does
  • How master and slave swap bytes in the same eight clocks
  • Clock edges: set data while low, read on the rising edge
  • Using SS to say when a transfer starts and ends
  • Difficulty: Advanced
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

Two Unos talk over SPI with four shared wires: SS on pin 10, MOSI on 11, MISO on 12 and SCK on 13, plus a common ground. Both sides are written by hand, without the SPI library, so each edge of the protocol is visible in the code.

The master pulls SS low, then sends one byte in eight clock pulses while reading a byte back on MISO. It does this every 400 ms with a rising counter. The slave answers with twice the byte it got the time before, so the bytes really travel both ways.

Parts list

2 parts, plus the jumper wires. They retail for about $54; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Arduino Uno R3 (2) pin GNDArduino Uno R3 (1) pin GND
Arduino Uno R3 (2) pin 13Arduino Uno R3 (1) pin 13
Arduino Uno R3 (2) pin 12Arduino Uno R3 (1) pin 12
Arduino Uno R3 (2) pin 11Arduino Uno R3 (1) pin 11
Arduino Uno R3 (2) pin 10Arduino Uno R3 (1) pin 10

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Uno A, the SPI master. It clocks one byte at a time out to Uno B and
// reads one byte back in the same eight clocks: SPI mode 0, MSB first.
//
//   A pin 10 (SS)    ->  B pin 10   low while a byte is moving
//   A pin 11 (MOSI)  ->  B pin 11   master out, slave in
//   A pin 12 (MISO)  <-  B pin 12   master in, slave out
//   A pin 13 (SCK)   ->  B pin 13   the clock (the L LED flickers with it)
//   A GND            --  B GND
//
// Both sides are written out by hand rather than handed to the SPI library,
// so every edge of the protocol is in the code you are reading. The clock is
// slow (5 kHz) so the slave, which is a plain loop, never misses an edge.

const byte SS_PIN = 10;
const byte MOSI_PIN = 11;
const byte MISO_PIN = 12;
const byte SCK_PIN = 13;
const unsigned int HALF_US = 100;  // half a clock period

byte transfer(byte out) {
  byte in = 0;
  for (int bit = 7; bit >= 0; bit--) {
    digitalWrite(MOSI_PIN, (out >> bit) & 1);  // set the data while SCK is low
    delayMicroseconds(HALF_US);
    digitalWrite(SCK_PIN, HIGH);               // both sides sample on the rise
    in = (in << 1) | digitalRead(MISO_PIN);
    delayMicroseconds(HALF_US);
    digitalWrite(SCK_PIN, LOW);
  }
  return in;
}

byte count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SS_PIN, OUTPUT);
  pinMode(MOSI_PIN, OUTPUT);
  pinMode(SCK_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  digitalWrite(SS_PIN, HIGH);
  digitalWrite(SCK_PIN, LOW);
  Serial.println("SPI master: one byte every 400 ms");
}

void loop() {
  count++;
  digitalWrite(SS_PIN, LOW);
  delayMicroseconds(300);  // give the slave time to see SS and load its byte
  byte answer = transfer(count);
  digitalWrite(SS_PIN, HIGH);

  Serial.print("master sent ");
  Serial.print(count);
  Serial.print(", slave answered ");
  Serial.println(answer);
  delay(400);
}
sketch.ino · Arduino Uno R3
// Uno B, the SPI slave. While SS is low it reads MOSI on every rising
// clock edge and puts its own answer on MISO, one bit before each rise.
// Its answer is twice the byte it received last time, so the master can
// see that the bytes really went both ways.

const byte SS_PIN = 10;
const byte MOSI_PIN = 11;
const byte MISO_PIN = 12;
const byte SCK_PIN = 13;

byte answer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SS_PIN, INPUT);
  pinMode(MOSI_PIN, INPUT);
  pinMode(SCK_PIN, INPUT);
  pinMode(MISO_PIN, INPUT);  // let go of MISO until we are selected
  Serial.println("SPI slave: waiting for SS");
}

void loop() {
  if (digitalRead(SS_PIN) == HIGH) return;

  pinMode(MISO_PIN, OUTPUT);
  byte in = 0;
  for (int bit = 7; bit >= 0; bit--) {
    digitalWrite(MISO_PIN, (answer >> bit) & 1);  // ready before the rise
    while (digitalRead(SCK_PIN) == LOW) {
      if (digitalRead(SS_PIN) == HIGH) return;    // the master gave up
    }
    in = (in << 1) | digitalRead(MOSI_PIN);
    while (digitalRead(SCK_PIN) == HIGH) {
    }
  }
  while (digitalRead(SS_PIN) == LOW) {
  }
  pinMode(MISO_PIN, INPUT);

  Serial.print("slave got ");
  Serial.println(in);
  answer = in * 2;
}

Code walkthrough

The key functions of the sketch, and what each one does.

transfer sketch.ino

Sends and receives one byte, most significant bit first. For each bit it puts the bit on MOSI while the clock is low, raises SCK, reads MISO, then lowers SCK. A pause of HALF_US on each half makes the clock slow enough for the slave to follow.

byte transfer(byte out) {
  byte in = 0;
  for (int bit = 7; bit >= 0; bit--) {
    digitalWrite(MOSI_PIN, (out >> bit) & 1);  // set the data while SCK is low
    delayMicroseconds(HALF_US);
    digitalWrite(SCK_PIN, HIGH);               // both sides sample on the rise
    in = (in << 1) | digitalRead(MISO_PIN);
    delayMicroseconds(HALF_US);
    digitalWrite(SCK_PIN, LOW);
  }
  return in;
}

setup sketch.ino

On the master it makes SS, MOSI and SCK outputs and MISO an input, with SS high and SCK low. On the slave every pin starts as an input, so it does not drive MISO until it is selected.

void setup() {
  Serial.begin(9600);
  pinMode(SS_PIN, OUTPUT);
  pinMode(MOSI_PIN, OUTPUT);
  pinMode(SCK_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  digitalWrite(SS_PIN, HIGH);
  digitalWrite(SCK_PIN, LOW);
  Serial.println("SPI master: one byte every 400 ms");
}

loop sketch.ino

On the master it adds 1 to count, pulls SS low, waits 300 microseconds, runs transfer, releases SS and prints both bytes. On the slave it waits for SS low, reads eight bits on each rising clock, then prints what it got and doubles it for next time.

void loop() {
  count++;
  digitalWrite(SS_PIN, LOW);
  delayMicroseconds(300);  // give the slave time to see SS and load its byte
  byte answer = transfer(count);
  digitalWrite(SS_PIN, HIGH);

  Serial.print("master sent ");
  Serial.print(count);
  Serial.print(", slave answered ");
  Serial.println(answer);
  delay(400);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The master prints an answer of 0 every time This project
    Check MISO on pin 12 connects the two boards, and that the grounds are joined.
  • The slave prints nothing This project
    Check SS on pin 10 is wired between the boards. The slave only reads while SS is low.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.