SD readers/How the card is talked to/05. SPI mode or SD mode
How the card is talked to · 05 of 11

SPI mode or SD mode

Every card wakes up in SD mode. What the host does with one pin in the first few milliseconds decides whether it stays there or drops into SPI, and once it has dropped it does not come back until the power does.

One card, two protocols

A card is not built for SPI. It is built for the SD bus — a clock, a command line and up to four data lines — and SPI is a compatibility mode it keeps around so that small microcontrollers can use it at all.

It wakes up in SD mode. Every card, every time.

Which protocol the card ends up in
SD.h
Your sketch uses
It latches. Once the card has answered in SPI it stays in SPI until the 3.3 V goes away — so a sketch cannot call SD.begin() and then SD_MMC.begin() and expect the faster one. Unplug the board, or pick one library and stay in it.

The switch is one thing: whether the pin that SD mode calls DATA3 is being held low when the host sends its reset command. Held low, the card decides it is talking to an SPI host and answers as one. Left high — and this board's 10 kΩ resistor leaves it high on its own — the card carries on in SD mode.

That is why the front of the board prints CS on the hole the back prints DATA3. One pin, two jobs, and the job is chosen in the first few milliseconds after power.

What you get in each

SPI is four signal wires and any pins you like on any microcontroller. One data line each way, a chip select, and a library — SD.h — that has worked unchanged for fifteen years.

SD mode is where the card is native. SD_MMC.h on an ESP32 opens it, in 1-bit mode with three signal wires or 4-bit mode with six. No chip select, a higher clock, and on the ESP32-S3 any six free pins.

It latches

This is the part that costs an evening. The decision is made once, at power-up, and nothing your sketch does afterwards changes it. A sketch that tries SPI, fails, and falls back to SD_MMC is testing a card that is already committed.

Cut the 3.3 V and start again. In practice that means unplugging the board — the reset button restarts the microcontroller and leaves the card powered.

When it does not work

Can I use SD.h and SD_MMC.h in the same sketch?

Not on the same card without a power cycle. Once a card has answered in SPI it stays in SPI until its supply is removed, so a later SD_MMC.begin() finds a card that is no longer speaking that language. Pick one library at the top of the sketch and stay in it.

SD_MMC.begin() fails but SD.begin() works

Usually the data lines. In 1-bit mode the card needs CLK, CMD and DATA0 wired and DATA3 left high, which the board's own pull-up does. In 4-bit mode all four data lines must go to the microcontroller — the pull-ups alone are not enough, and a floating DATA3 is the commonest reason a 4-bit mount fails.

Why does SD_MMC not ask me for a chip select pin?

Because SD mode has no chip select. Commands go out on CMD and every card on the bus listens, answering only if the command is addressed to it. Chip select is an SPI idea, and the pin SPI uses for it is the SD bus's fourth data line.

It mounts on a cold boot but not after pressing reset

The reset button restarts the microcontroller without cutting the card's 3.3 V, so the card is still in whichever mode the last run left it in. Unplug the board rather than pressing reset while you are switching between libraries.

Where this goes next

Eleven resistors on the small board, thirteen on the large one, and the one line that deliberately has none.

Why the resistors are there

Edit this page — content/books/sd/spi-mode-or-sd-mode.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum