Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/manish04-mu/TheEmbeddedInsights/llms.txt

Use this file to discover all available pages before exploring further.

By the end of this guide you will have your Arduino UNO wired for SPI communication, the Arduino IDE installed and configured, and the SPI Echo Hello sketch running on real hardware. You will be able to watch the master transmit the string "SPI Demo: Hello from the Master!" byte-by-byte over the SPI bus and read the echo response in the Serial Monitor — or, if you have a logic analyzer, observe the raw SCK, CS, MOSI, and MISO waveforms live. No prior SPI experience is required.

Prerequisites

Hardware Required

  • Arduino UNO (ATmega328P) — any genuine or compatible board
  • USB Type-B cable — for power and sketch upload
  • Breadboard + jumper wires — for connecting a slave device when you are ready
  • Logic analyzer (optional but recommended) — to observe SCK, CS, MOSI, and MISO waveforms

Software Required

  • Arduino IDE 2.x — download the latest release from arduino.cc/en/software
  • The built-in SPI.h library ships with the IDE — no additional library installation is needed
  • A serial terminal (the IDE’s built-in Serial Monitor is sufficient)

Hardware Setup

1

Install Arduino IDE

Download and install the Arduino IDE 2.x from https://www.arduino.cc/en/software. Accept the default installation options. Once installed, connect your Arduino UNO via USB — the IDE should automatically detect the board on a COM/serial port. Confirm this under Tools → Board → Arduino AVR Boards → Arduino Uno and Tools → Port → (your COM port).
2

Wire the SPI pins

The Arduino UNO exposes the ATmega328P hardware SPI peripheral on four dedicated pins. Use the table below as your reference when connecting a slave device:
SPI SignalArduino UNO PinDirectionNotes
CSDigital 10Master → SlavePulled HIGH (inactive) by default; pulled LOW to select slave
MOSIDigital 11Master → SlaveMaster Out Slave In — data from UNO to peripheral
MISODigital 12Slave → MasterMaster In Slave Out — data returned from peripheral
SCKDigital 13Master → SlaveClock generated by the master (UNO)
For the echo test in this guide no external slave is required — the sketch runs as a standalone master and the MISO line simply floats. You will see 0x00 returned for each byte, which is expected behavior.
3

Open a new sketch in Arduino IDE

Launch the Arduino IDE and select File → New Sketch. An empty setup() / loop() template will open. You will replace this with the SPI Echo Hello sketch in the next step.
4

Copy the Hello World sketch

Delete the template content and paste in the complete SPI_Echo_Hello sketch shown in the First Sketch section below. Save the file as SPI_Echo_Hello.ino (File → Save As…).
5

Upload and open the Serial Monitor

Click the Upload button (→ arrow icon) or press Ctrl+U. The IDE will compile and flash the sketch. Once you see “Done uploading”, open the Serial Monitor with Tools → Serial Monitor (or Ctrl+Shift+M). Set the baud rate drop-down at the bottom of the Serial Monitor to 9600 baud. Received bytes will begin printing immediately.

First Sketch

Below is the complete source of SPI_Echo_Hello.ino exactly as it appears in the repository. Read through the comments — they document every pin assignment and logic analyzer channel mapping used throughout this guide series.
SPI_Echo_Hello.ino
#include <SPI.h>

/*
Pin Description: 
CS  - Chip Select         : Pin-> 10
MOSI- Master Out/Slave In : Pin-> 11
MISO- Master In/Slave Out : Pin-> 12
SCK - Clock Signal        : Pin-> 13
*/

/*
Logic Analyser:
CH0: SCK
CH1: Enable/Chip Select
CH2: MOSI
CH3: MISO
*/


#define CS 10     //Chip Select

char sentValue[] = "SPI Demo: Hello from the Master!";

char receivedValue[255];

void setup() {

  Serial.begin(9600);  //Serial mode is used to check the received value in MISO, in case, the logic analyser is not present.

  pinMode(CS, OUTPUT); //Define the Chip Select Pin to Output (Master --> Slave)
  digitalWrite(CS, HIGH);   // Keep slave deselected, usually teh CS is active low (We need to check the slave datasheet for this information.)

  SPI.begin();

}

void loop() {

  SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE2));

  digitalWrite(CS, LOW);          // Select slave

  for (int x = 0; x < strlen(sentValue); x++)
  {
    receivedValue[x] = SPI.transfer(sentValue[x]);
    delay(1);
  }
  

  digitalWrite(CS, HIGH);         // Deselect slave

  SPI.endTransaction();
 
  for (int x = 0; x <37; x++)
  {
    Serial.print("Received: ");
    Serial.println(receivedValue[x], HEX);
  }
  delay(1000);

}

What to Expect

When the sketch is running, the master transmits the null-terminated string "SPI Demo: Hello from the Master!" character by character over MOSI. The SPI.transfer() call is full-duplex — while each byte goes out on MOSI, the hardware simultaneously clocks in a byte from MISO and stores it in receivedValue. The Serial Monitor will print output like this every second:
Received: 0
Received: 0
Received: 0
...
Each value is printed in hexadecimal. If no slave device is connected, MISO floats and the ATmega328P will typically read 0x00 for every byte — this is completely normal for a standalone echo test. When a real SPI slave is wired in, those values will reflect the bytes the slave sends back. Key configuration details from the sketch:
ParameterValueMeaning
Clock speed100000 Hz100 kHz — conservative rate, visible on any analyzer
Bit orderMSBFIRSTMost-significant bit transmitted first
Clock modeSPI_MODE2CPOL=1, CPHA=0 — clock idles HIGH, data sampled on leading edge
CS pinDigital 10Active LOW — asserted before transfer, released after
Head over to /spi/arduino/hello-world for a complete line-by-line breakdown of this sketch, including register-level explanations of what SPI.begin() and SPISettings configure inside the ATmega328P’s SPCR and SPSR registers.
If you have a logic analyzer, connect it to the SPI lines using the channel mapping from the sketch comments: CH0 → SCK (pin 13), CH1 → CS (pin 10), CH2 → MOSI (pin 11), CH3 → MISO (pin 12). Set your analyzer’s protocol decoder to SPI, clock mode 2 (CPOL=1, CPHA=0), MSB first, at 100 kHz. You will see the full "SPI Demo: Hello from the Master!" string framed by CS going LOW and HIGH — exactly matching what the Serial Monitor reports.

Build docs developers (and LLMs) love