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.

The Embedded Insights examples use the Arduino SPI library included with the Arduino IDE. This page documents every function and constant used across the sketches, with parameter types, return values, and usage notes. All examples target the Arduino UNO with CS on pin 10, MOSI on pin 11, MISO on pin 12, and SCK on pin 13.

SPI.begin()

Initializes the SPI bus in master mode. Internally configures SCK (pin 13), MOSI (pin 11), and SS (pin 10) as outputs and sets the default clock, bit order, and data mode. Note that SPI.begin() does not configure the user-defined CS pin — you must call pinMode(CS, OUTPUT) and digitalWrite(CS, HIGH) yourself in setup() before calling SPI.begin(). Parameters: none Returns: void
SPI.begin();

SPISettings

Constructor that encapsulates all SPI bus configuration into a single object. Always pass an SPISettings instance to SPI.beginTransaction() — never set clock speed or data mode globally, as SPISettings ensures safe use alongside other libraries.
SPISettings(100000, MSBFIRST, SPI_MODE0)
clockSpeed
unsigned long
required
Bus clock frequency in Hz. The Arduino UNO supports up to 16,000,000 (16 MHz). All The Embedded Insights sketches use 100,000 (100 kHz) to stay comfortably within typical logic analyzer sampling limits.
bitOrder
uint8_t
required
Transmission bit order. Use MSBFIRST to send bit 7 first (most significant), or LSBFIRST to send bit 0 first (least significant). Most SPI slaves expect MSBFIRST; consult the slave datasheet.
dataMode
uint8_t
required
Clock polarity and phase setting. One of SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3. Each mode determines the clock idle state (CPOL) and the edge on which data is sampled (CPHA). See the Data Mode Constants table below.

SPI.beginTransaction(settings)

Acquires the SPI bus with the configuration specified in settings. This must be called before asserting the CS pin — doing so in the correct order prevents bus contention when multiple libraries share the SPI hardware. All subsequent calls to SPI.transfer() use the clock speed, bit order, and data mode captured in settings. Parameters:
settings
SPISettings
required
An SPISettings object created with the desired clock speed, bit order, and data mode.
Returns: void
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
digitalWrite(CS, LOW); // assert CS after beginTransaction

SPI.transfer(value)

Transfers one byte over the SPI bus using full-duplex communication. While value is shifted out on MOSI, one byte is simultaneously shifted in from the slave on MISO. The function blocks until all 8 bits have been clocked out and back. Parameters:
value
byte / uint8_t
required
The byte to transmit on MOSI. In the sketches this is typically a literal hex value such as 0x62 or a character from a string.
Returns: byte — the byte received from the slave on MISO during the same clock cycle. Returns 0x00 when no slave is connected (MISO line floats or is pulled LOW).
byte received = SPI.transfer(0x62);

SPI.endTransaction()

Releases the SPI bus, allowing other code or interrupt-driven libraries to use it. Deassert (pull HIGH) the CS pin before calling endTransaction() — the slave must be deselected before the bus is relinquished so that no partial transaction is left open. Parameters: none Returns: void
digitalWrite(CS, HIGH); // deassert CS before endTransaction
SPI.endTransaction();

Data Mode Constants

The SPI data mode determines two things: the clock polarity (CPOL — idle state of SCK) and the clock phase (CPHA — which clock edge latches data). Choose the mode that matches your slave device’s datasheet.
ConstantCPOLCPHADescription
SPI_MODE000Clock idles LOW; data sampled on rising edge
SPI_MODE101Clock idles LOW; data sampled on falling edge
SPI_MODE210Clock idles HIGH; data sampled on falling edge
SPI_MODE311Clock idles HIGH; data sampled on rising edge

Bit Order Constants

ConstantBehavior
MSBFIRSTBit 7 (most significant bit) sent first
LSBFIRSTBit 0 (least significant bit) sent first

Complete Transaction Pattern

Every sketch in The Embedded Insights follows the same five-step structure. Copy this pattern as your baseline and adjust the SPISettings arguments to match the target slave:
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
digitalWrite(CS, LOW);                    // assert CS — select slave
byte received = SPI.transfer(sentValue);  // full-duplex byte exchange
digitalWrite(CS, HIGH);                   // deassert CS — release slave
SPI.endTransaction();                     // release the SPI bus
Always call SPI.beginTransaction() before asserting CS, and deassert CS before calling SPI.endTransaction(). Reversing this order can corrupt ongoing transfers if another library uses the SPI bus inside an interrupt.

Build docs developers (and LLMs) love