Loading...
Searching...
No Matches
SpiLoopback.ino

SPI loopback test: sends a counter with SPI.transfer() every 500 ms and prints the byte that comes back. With MOSI wired to MISO, every byte sent should come straight back.

Uses PhyduinoSPI::beginTransaction() with 1 MHz, MSBFIRST, SPI_MODE0.

Wiring
D11 (MOSI) to D12 (MISO).
Output
Serial Monitor at 115200 baud, for example:
sent 7 received 7
With D11 and D12 not connected, the received value is usually 0 or 255.
// SPI loopback test: connect D11 (MOSI) to D12 (MISO).
// Every byte sent should come straight back.
#include <SPI.h>
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
static uint8_t value = 0;
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
const uint8_t received = SPI.transfer(value);
SPI.endTransaction();
Serial.print("sent ");
Serial.print(value);
Serial.print(" received ");
Serial.println(received);
value++;
delay(500);
}
@ MSBFIRST
Definition Common.h:48
Arduino SPI on the header pins: SCK = D13, MOSI = D11, MISO = D12.
PhyduinoSPI SPI
SPI on the header: SCK D13, MOSI D11, MISO D12.
SerialUSBClass Serial
USB serial port, the one the Serial Monitor uses.
void loop(void)
Written by the sketch: runs over and over after setup(), on core 0.
void setup(void)
Written by the sketch: runs once after reset, on core 0.
void delay(unsigned long)
Waits a number of milliseconds.