Go back

SPI Loopback Test: Master the Protocol with Velxio

Introduction

Serial Peripheral Interface (SPI) is a synchronous serial communication protocol widely used to connect microcontrollers to sensors, displays, and memory chips. In this tutorial, we’ll build an SPI loopback test using the Velxio simulator. The loopback test sends bytes via SPI and reads back the response, confirming that the protocol works correctly. Since there’s no physical slave device, the simulator returns the sent byte, making it a perfect way to learn SPI without any hardware.

Circuit Walkthrough

Let’s start by setting up the circuit. The only component we need is an Arduino Uno, which we’ll place on the canvas.

Circuit diagram of spi-loopback on the Velxio simulator canvas

To add the Arduino Uno, open the component picker by clicking the ”+” icon on the canvas toolbar.

Velxio component picker modal browsing parts by category

Search for “Arduino Uno” and place it on the canvas. The Arduino Uno has dedicated SPI pins:

In this loopback test, we don’t need any external wiring because the SPI signals are internal to the Arduino. However, we must ensure that the SS pin (pin 10) is configured as an output and driven high initially, then low during communication. The code handles this.

Code Walkthrough

Now let’s examine the code. Open the code editor by clicking the “Code” tab in the editor toolbar.

Velxio editor toolbar with compile and run controls

The code is written in Arduino C++ and uses the built-in SPI.h library. No external libraries are needed.

// SPI Loopback Test
// Sends bytes via SPI and logs the exchange.
// Without a physical slave, the emulator returns the sent byte.

#include <SPI.h>

#define SS_PIN 10

void setup() {
  Serial.begin(9600);
  Serial.println("========================");
  Serial.println("  SPI Protocol Test");
  Serial.println("========================");
  Serial.println();

  pinMode(SS_PIN, OUTPUT);
  digitalWrite(SS_PIN, HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV16);

  Serial.println("SPI initialized.");
  Serial.print("Clock divider: 16 (");
  Serial.print(F_CPU / 16);
  Serial.println(" Hz)");
  Serial.println();

  // Send test pattern
  Serial.println("Sending test pattern via SPI:");
  byte testData[] = {0xAA, 0x55, 0xFF, 0x00, 0x42, 0xDE, 0xAD, 0xBE};

  digitalWrite(SS_PIN, LOW);  // Select slave

  for (int i = 0; i < sizeof(testData); i++) {
    byte sent = testData[i];
    byte received = SPI.transfer(sent);

    Serial.print("  TX: 0x");
    if (sent < 16) Serial.print("0");
    Serial.print(sent, HEX);
    Serial.print("  RX: 0x");
    if (received < 16) Serial.print("0");
    Serial.print(received, HEX);

    if (sent == received) {
      Serial.println("  (loopback OK)");
    } else {
      Serial.println();
    }
  }

  digitalWrite(SS_PIN, HIGH);  // Deselect slave

  Serial.println();
  Serial.println("SPI test complete.");
}

void loop() {
  delay(1000);
}

Let’s break down the key parts:

Key Concepts

Common Pitfalls and Debugging Tips

Suggested Extensions

Try It Yourself

Ready to dive in? Open the live example in Velxio and run the simulation. You’ll see the serial output showing each transmitted and received byte, confirming the loopback.

Open the SPI Loopback Test in Velxio

Happy simulating!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Master Serial, I2C, and SPI on Arduino with Velxio
Next Post
I2C EEPROM Read/Write with Arduino: A Complete Guide