Sub topic 7
Choosing a clock rate within device and board limits
This chapter assumes you can write a basic Arduino sketch, know what the four SPI signals do, and understand the SPI modes. You will learn how to pick the SCLK frequency you pass to SPISettings, which limits apply, and how a wrong choice shows up.
Why the clock rate is a choice
SPI has no negotiated speed. The controller drives SCLK at whatever frequency you request, and the peripheral either keeps up or it doesn't. Nothing in the protocol reports a failure. A rate that is too high gives you wrong bytes, not an error code.
The usable rate is the lowest of several independent limits:
| Limit | Where it comes from | Where to find it |
|---|---|---|
| Peripheral maximum | The device's internal timing | Datasheet, "SCK frequency" or "fSCK" |
| Controller maximum | The board's SPI hardware and clock source | Board documentation, core source |
| Divider granularity | The hardware only offers certain divisions of the system clock | Library behavior |
| Wiring and signal quality | Wire length, capacitance, noise | Your build |
| Voltage-dependent limits | Many chips are slower at a lower supply voltage | Datasheet tables |
Reading the peripheral's limit
A datasheet states a maximum SCK frequency, often with a table of related timings. The values you need are:
- fSCK (maximum clock frequency). The headline number. Some chips list different maxima for read and write commands, or for different supply voltages.
- tSU and tH (data setup and hold). How long the data must be stable before and after the sampling edge. These are what really cause the limit.
- tV (output valid time). How long after the shifting edge the peripheral's MISO output becomes valid. This delay is why reads fail first at high speed.
- tCSS and tCSH (chip select setup and hold). The minimum time between chip select falling and the first clock edge, and between the last edge and chip select rising.
Read the conditions attached to the number. A flash chip may allow 104 MHz only for a "fast read" command and only at 3.3 V, and a lower rate for the plain read command.
A useful check is the clock period. At 8 MHz, one SCLK cycle is 125 ns, and each half cycle is 62.5 ns. If the peripheral needs 70 ns of MISO valid time, an 8 MHz read cannot work in the half cycle that is available.
The controller's limit and clock dividers
The SPI hardware on most Arduino boards derives SCLK from the system clock by dividing it. On a classic AVR board such as the Uno, the system clock is 16 MHz and the hardware offers divisors of 2, 4, 8, 16, 32, 64 and 128. The fastest SCLK is therefore 8 MHz, and the other options are 4, 2, 1 MHz and so on down to 125 kHz.
When you request a rate, the AVR core picks the fastest divider that does not exceed your request. If you ask for 3 MHz, you get 2 MHz. Other cores differ, and some compute a divider with finer steps. In every case the actual rate can be lower than the requested one, but it should not be higher. If your timing matters, confirm the real frequency on a logic analyzer.
The SPISettings value is a maximum, not a promise:
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop() {
// Request 3 MHz. On a 16 MHz AVR board the hardware runs at 2 MHz.
SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
uint8_t reply = SPI.transfer(0x9F); // example command byte
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.println(reply, HEX);
delay(1000);
}Boards with faster processors, such as ARM or ESP32 boards, allow far higher rates, often 20 MHz or more on the hardware SPI peripheral. On these boards the peripheral's limit is usually the one that binds. Check the core's documentation for the SPI clock source, because some boards share it with other peripherals.
Wiring and board limits
Even when both chips can handle a rate, the connection might not. A fast edge on a long wire causes ringing and reflections, and the extra capacitance of a breadboard rounds the edges. Practical limits for typical setups:
- Short wires on a PCB or a tidy breadboard: several MHz works well. Rates of 10 MHz and above need clean routing.
- Jumper wires of 15 cm or more: stay at a few MHz, and lower for anything longer.
- Several peripherals on one bus: each one adds capacitance and a stub, so the bus handles a lower rate than a single device.
The signal integrity chapter covers termination and level shifting in detail. For now, treat the wiring as a reason to start low.
A practical method for choosing
- Find the peripheral's maximum fSCK in the datasheet, under the conditions that match your supply voltage and command.
- Find the controller's maximum, from the board's clock and divider table.
- Take the smaller of the two as your ceiling.
- Leave margin for your wiring. A common choice is a half to three quarters of the ceiling on a breadboard.
- Start with a slow rate such as 100 kHz to 1 MHz. Confirm that the device answers correctly, then raise the rate in steps.
- Stop at the highest rate that gives correct results across many reads, and then back off one step.
Measuring the effect with a test
A read of a register with a known value is a good test, because wrong timing changes the result. Many devices have an ID register. The sketch below tries several rates and counts mismatches over repeated reads.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t ID_REGISTER = 0x0F; // example address; use your device's ID register
const uint8_t EXPECTED_ID = 0x33; // example value; use the value from your datasheet
const uint8_t READ_FLAG = 0x80; // device-specific
const uint32_t RATES[] = {250000, 1000000, 4000000, 8000000};
uint8_t readId(uint32_t hz) {
SPI.beginTransaction(SPISettings(hz, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(ID_REGISTER | READ_FLAG);
uint8_t value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
for (uint8_t r = 0; r < sizeof(RATES) / sizeof(RATES[0]); r++) {
uint16_t errors = 0;
for (uint16_t i = 0; i < 1000; i++) {
if (readId(RATES[r]) != EXPECTED_ID) {
errors++;
}
}
Serial.print(RATES[r]);
Serial.print(" Hz: ");
Serial.print(errors);
Serial.println(" errors in 1000 reads");
}
}
void loop() {}Zero errors in 1000 reads is a good sign but not proof. Faults from noise can be rare, so test for longer at your final rate, with the real wiring and with other devices active.
Symptoms of a rate that is too high
- Reads return 0xFF or 0x00. The device is not answering in time, or MISO is floating.
- Reads are almost right. Bits are shifted by one place. A value such as 0x66 appears as 0xCC or 0x33. This pattern points to a sampling timing problem. It can also point to the wrong SPI mode, so check the mode before the speed.
- Errors come and go. Results are correct at times and wrong at others. Marginal timing, noise, and crosstalk produce this.
- Writes seem to work but values do not stick. Writes often tolerate more speed than reads, because they do not depend on the output valid time. Check by reading back.
A rate that is too low rarely causes wrong data, but some devices have a minimum clock frequency, or they time out if a command takes too long. Check the datasheet for a minimum if you go very slow.
Speed also depends on more than SCLK
The data rate you get is lower than the clock rate. Each SPI.transfer call has software overhead, and chip select toggling takes time. At 8 MHz, one byte takes 1 µs on the wire, but a single digitalWrite on an AVR board takes a few microseconds. For small transfers, the overhead dominates, and a higher SCLK gains little. The bulk transfers chapter shows how to reduce that overhead.
Exercise
A peripheral's datasheet gives a maximum fSCK of 10 MHz at 3.3 V. You run it from an Uno (16 MHz system clock, divisors 2 to 128) on a breadboard with 20 cm jumper wires, and you call SPISettings(10000000, MSBFIRST, SPI_MODE0). What SCLK frequency does the hardware actually use, and what rate would you choose to start with?
Show answer
The AVR core picks the fastest divider that does not exceed 10 MHz. Divisor 2 gives 8 MHz, which is under the limit, so the hardware runs at 8 MHz. Because the wires are long and the build is on a breadboard, 8 MHz is likely too aggressive. Start at about 1 MHz (divisor 16), confirm correct reads, and raise the rate in steps such as 2 MHz and 4 MHz while testing, stopping one step below the first rate that gives errors.
Exercise
A register read at 4 MHz returns 0xCC where you expect 0x66, but at 500 kHz it returns 0x66. Name two likely causes and one check that tells them apart.
Show answer
Likely causes are a timing problem (the MISO output valid time, or wiring effects, exceeds the available half clock period at 4 MHz) and a wrong SPI mode that only appears to work at low speed. To tell them apart, check the datasheet's mode against the one in SPISettings, then look at SCLK and MISO on a logic analyzer. If the mode is right and MISO changes late relative to the sampling edge at 4 MHz, the rate is too high for the device or the wiring.