Sub topic 8
Using the Arduino SPI library and SPISettings
This chapter assumes you know basic Arduino sketches, pinMode(), digitalWrite(), hexadecimal numbers, and the SPI signals SCLK, MOSI, MISO, and chip select. It focuses on configuring and using the Arduino SPI library. The peripheral's command format still comes from its datasheet.
The SPI library
The Arduino SPI library controls the microcontroller's hardware SPI peripheral. Hardware SPI shifts bits more consistently and efficiently than manually changing pins in software.
Include the library, configure the chip-select pin as an output, and keep it high when the peripheral is inactive. A high chip-select signal usually means “not selected,” although the datasheet is authoritative.
SPISettings groups the three main transfer settings:
| Setting | Meaning | Example |
|---|---|---|
| Clock speed | SCLK frequency | `4000000` for 4 MHz |
| Bit order | Which bit is sent first | `MSBFIRST` |
| SPI mode | Clock polarity and phase | `SPI_MODE0` |
Create a transaction before selecting the peripheral. End it after releasing chip select.
A register-read example
The following sketch reads register 0x0F. The read flag and register address are device-specific placeholders. Replace them with the values from your peripheral's datasheet.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80;
uint8_t readRegister(uint8_t registerAddress) {
SPI.beginTransaction(
SPISettings(1000000, MSBFIRST, SPI_MODE0)
);
digitalWrite(CS_PIN, LOW);
SPI.transfer(registerAddress | READ_FLAG);
uint8_t result = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return result;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop() {
uint8_t value = readRegister(0x0F);
Serial.print("Register value: 0x");
if (value < 0x10) {
Serial.print('0');
}
Serial.println(value, HEX);
delay(1000);
}SPI is full duplex. Each call to SPI.transfer() sends one byte and receives one byte at the same time. During the first call, the controller sends the command while the peripheral may return an irrelevant byte. The second call sends 0x00, called a dummy byte, only to generate clock pulses so the peripheral can return the register value.
Choosing settings safely
The mode must match the peripheral's timing diagram. SPI_MODE0 means the clock is idle low and data is sampled on the rising edge. Other modes change the idle level, sampling edge, or both.
The clock speed must not exceed the peripheral's specified maximum. Start below that limit when wiring is long or uncertain. MSBFIRST is common, but some devices require LSBFIRST.
Keep chip select low for exactly the command frame required by the device. Raising it too early can cancel a command or reset the peripheral's internal bit counter.
Exercise
Which SPI operation should occur before pulling chip select low, and why?
Show answer
Call SPI.beginTransaction() first. It applies the required clock speed, bit order, and mode before clock pulses reach the peripheral.