Sub topic 13
Wiring, signal integrity and voltage levels
This chapter assumes you know the four SPI signals (SCLK, MOSI, MISO and chip select), the SPI modes, and how to call SPI.beginTransaction with SPISettings. It also assumes you can wire a breadboard and read a pin diagram. The chapter covers the physical layer. A sketch can be correct and still fail because of long wires, a missing ground, or mismatched voltages.
Why SPI is sensitive to wiring
SPI has no error checking, no acknowledgement and no retry. If a bit is corrupted, the receiver shifts in a wrong value and nothing reports it. You only see bad readings, zeros, or 0xFF bytes.
SPI clocks are also fast. At 8 MHz, one clock period is 125 ns. The signal edges must be clean within a small part of that time. A wire that looks like a plain connection at DC behaves like a small antenna and capacitor at these speeds.
Two properties of SPI shape the wiring rules:
- SCLK is an edge-sensitive signal. Every rising or falling edge makes the peripheral shift a bit. One extra edge from noise (a glitch) shifts the whole frame by one bit.
- MOSI, MISO and chip select are sampled at a clock edge. They only need to be stable around that edge, but they must be stable.
Common ground and power
Every device on the bus must share a ground. The logic levels are measured against ground, so a device without a common ground sees an undefined signal.
Follow these rules:
- Connect the GND pins of the controller, every peripheral and any external supply together.
- Run the ground return close to the signal wires, so the loop area between them is small.
- Place a 100 nF ceramic decoupling capacitor between VCC and GND, close to each peripheral's power pins.
- Power high-current parts (SD cards, displays with backlights, radios) from a supply that can deliver the current. A weak USB port or a 3.3 V pin on a board can sag and cause resets or corrupted transfers.
Decoupling matters because a peripheral draws current in short bursts when it switches. Without a local capacitor, that burst pulls the supply voltage down for a moment, which can disturb the logic levels.
Wire length and clock speed
Wire length is the main limit on clock speed on a breadboard or a hand-wired board. A long wire has capacitance and inductance. They round the edges of the clock, and they can make the signal overshoot and ring (oscillate briefly) after each edge. Ringing on SCLK can create extra edges that the peripheral counts as clocks.
Use these working guidelines. They are rules of thumb, not guarantees, so check your own setup:
| Situation | Typical practical limit |
|---|---|
| Short PCB traces (a few centimeters) | Up to the device's rated maximum |
| Breadboard with short jumpers (under 15 cm) | A few MHz, often 4 to 8 MHz |
| Loose jumper wires (15 to 30 cm) | 1 MHz or less is safer |
| Ribbon cable or longer (30 cm or more) | Low clock rate, shielding or buffering needed |
The rule is simple. When transfers are unreliable, lower the clock rate first. If the problem disappears at a lower rate, the cause is timing or signal quality, not your command logic.
Reducing noise and crosstalk
Crosstalk is unwanted coupling of one signal onto its neighbor. SCLK has the fastest edges, so it is the most likely source.
- Keep SCLK away from MISO. A glitch on MISO near an edge is the most damaging to reads.
- Place a ground wire between SCLK and the data wires in a ribbon cable.
- Keep wires for all signals about the same length and route them together.
- Avoid running SPI wires beside motor, relay or switching-supply wires.
For a PCB or a longer cable, add a series resistor of about 22 to 100 ohms near the driver of SCLK. It slows the edge a little and damps ringing. Choose the value by testing. Too large a resistor, together with the cable's capacitance, rounds the edge too much at high clock rates.
Pull-ups, floating lines and chip select
A pin that nothing drives is floating. It can read as 0 or 1 at random and picks up noise.
Two lines need attention:
- Chip select. While the controller boots, its pins are often inputs and float. A peripheral may see a random low level on its CS pin and start listening. Add a pull-up resistor of 10 kohm from each CS line to VCC. This holds the peripheral deselected until your sketch drives the pin. Your sketch should still call
digitalWrite(CS_PIN, HIGH)beforepinMode(CS_PIN, OUTPUT)where the board allows it, or set the level right after. - MISO. A peripheral that is deselected should release (tri-state) MISO. If one does not, two devices can drive MISO at once. Check the datasheet for the MISO behavior of each part. Some parts, such as certain SD card modules, need a buffer or a resistor on MISO for this reason.
SPI does not need pull-ups on SCLK, MOSI or MISO the way I2C does, because the controller drives them with push-pull outputs.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
Serial.begin(115200);
// Set the level first, then switch to output, so CS never glitches low.
digitalWrite(CS_PIN, HIGH);
pinMode(CS_PIN, OUTPUT);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
uint8_t reply = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.println(reply, HEX);
delay(1000);
}On AVR boards, writing HIGH to an input pin enables its internal pull-up, so the order above also avoids a low glitch on the pin. Without a peripheral attached, a floating MISO line often reads as 0xFF or 0x00. That value tells you the peripheral is not responding.
The SPI hardware pin used as SS (pin 10 on an Uno) must be set as an output on AVR boards when the board is the controller. If it is an input and goes low, the hardware drops out of controller mode. Setting CS_PIN to pin 10 as an output, as above, avoids that.
Voltage levels
Logic families use different voltages. A 5 V Arduino Uno drives its outputs near 5 V. Many modern sensors, SD cards and flash chips run at 3.3 V, and some run at 1.8 V.
The danger is in the direction of the signal:
- Controller output to peripheral input (SCLK, MOSI, CS). A 5 V output on a 3.3 V input can damage the peripheral. Check the datasheet for the input's absolute maximum rating. Many 3.3 V parts are not 5 V tolerant.
- Peripheral output to controller input (MISO). A 3.3 V output into a 5 V controller usually works, but only if it meets the controller's minimum high level. A 5 V AVR treats about 3.0 V and above as high when powered at 5 V, which a 3.3 V signal usually meets, but with little margin.
Options for connecting 5 V and 3.3 V parts:
| Method | Works for | Notes |
|---|---|---|
| Voltage divider (two resistors) | 5 V to 3.3 V on one-way lines | Cheap, but slows edges, so use it only at low clock rates |
| Level shifter IC (for example 74LVC245 or TXB0104) | All SPI lines | Pick one rated for your clock rate. Some auto-direction parts struggle with push-pull signals at high speed |
| Use a 3.3 V controller | Whole bus | Simplest. Many boards run SPI natively at 3.3 V |
The same rule applies to every device on one bus. If a 5 V peripheral and a 3.3 V peripheral share MOSI and SCLK, the 5 V logic reaches the 3.3 V part. Use a shifter on the 3.3 V branch, or choose parts with the same voltage.
A wiring checklist
Run this list before you suspect your code:
- All grounds are connected together.
- Each device has the correct supply voltage and a 100 nF capacitor near its power pins.
- SCLK, MOSI and MISO go to the right pins. MOSI on the controller goes to the peripheral's data input. Some datasheets label pins as SDI and SDO from the peripheral's view, so SDI connects to MOSI and SDO connects to MISO.
- Each peripheral has its own CS line, with a pull-up.
- Wires are short, similar in length, and away from noisy wires.
- Logic voltages are compatible in both directions.
- The clock rate in
SPISettingsis low while you test.
Exercise
A 5 V Arduino Uno reads a 3.3 V sensor over SPI. The sensor is not 5 V tolerant. Which of the four lines (SCLK, MOSI, MISO, CS) need level shifting or a divider, and which line normally does not? Explain why.
Show answer
SCLK, MOSI and CS need shifting or dividing, because the 5 V controller drives them and they go into 3.3 V inputs that may be damaged. MISO normally does not need it, because the 3.3 V sensor drives it toward the controller, and a 5 V AVR usually reads 3.3 V as a high level. You should still check the datasheet for the controller's minimum high level.
Exercise
Your SPI read returns 0xFF every time at 8 MHz on a 20 cm jumper wire, and the same sketch works at 500 kHz. Name the most likely cause and two changes you could make.
Show answer
The likely cause is signal quality and timing: the long wire rounds and rings the clock edges at 8 MHz. Two changes are to lower the clock rate in SPISettings and to shorten the wires, keeping a ground return beside SCLK. Adding decoupling near the peripheral and a small series resistor on SCLK can also help.