microSD card module

The small SPI breakout with a microSD socket, a 3.3 V regulator and a level shifter, and a 4 MB card in it. SD.h reads and writes files on it.

Aliases: SD card, memory card, data logger.

Beside the board is what is on the card. Before a run that is the files property; while it runs it is the card itself, so a file a sketch writes shows up there as it grows.

Pins

Pin What it does
GND Ground.
VCC Supply, 3.3 to 5 V.
MISO Data from the card, at 3.3 V.
MOSI Data to the card.
SCK SPI clock.
CS Chip select.

On an Uno: CS to 4 (or 10), SCK to 13, MOSI to 11, MISO to 12. On a Mega: SCK to 52, MOSI to 51, MISO to 50 and CS to 53.

Properties

files is what the card holds when the run starts, as JSON: names to text, for example {"HELLO.TXT": "Hello", "LOGS/DAY1.CSV": "t,v"}. Names are 8.3, in any case. inserted says whether the card starts in the socket.

While it runs

Click a file's name to download it. The disk button downloads the whole card as a 4 MB image that any computer can mount. The + button puts a file of your own on the card; before a run it goes into files. Click the board to take the card out, and again to put it back: a sketch has to call SD.begin() again, as with a real one.

What is modeled

The card speaks the SPI mode of the SD specification: CMD0, CMD8, ACMD41, CMD58, the CSD and CID, single and multiple block reads and writes, with R1 status, data tokens, CRCs and the busy time after a write. Behind that is a real FAT16 volume with a partition table.

SD.h has what the tutorials use: SD.begin, SD.open for reading or appending, read, write, print, println, available, peek, seek, position, size, flush, close, SD.exists, SD.remove, SD.mkdir, SD.rmdir, and openNextFile for listing a directory.

What is not modeled

Real cards are slower in ways this one is not: long initialization, and a write that now and then takes a hundred milliseconds while the card erases. High capacity (SDHC) cards, FAT32 and long file names are not here.

Everything goes over SPI a bit at a time, driven by the sketch, so reading a 512-byte sector takes tens of milliseconds on an Uno, where a real SPI port takes well under one. On a faster board each bit waits a little before it is read, so the answer the card has just put on MISO has arrived; that makes a card read on an ESP32-C3 or a Pico slower still. It is the one thing about this card that is slower here than on the bench, and it matters when a sketch also has to keep up with something else, like a GPS: the GPS logger in the gallery writes its lines five at a time for that reason.