Overview
Over the years, removable computer storage has evolved across several media. Early removable media included magnetic floppy disks and later removable hard disks. In the 1990s removable storage shifted to optical discs (later surpassed by DVD). In the 2000s, advances in flash memory popularized flash-based removable storage, initially in the form of USB thumb drives. Multiple competing memory card formats appeared, including Sony Memory Stick, CompactFlash (CF), xD-Picture Card, Toshiba SmartMedia and multimedia card (MMC); however, Secure Digital (SD) cards ultimately achieved the largest market adoption.
Form Factors
SD cards are available in three main form factors: full-size SD, miniSD and microSD.
Interfaces and Protocols
SD cards support two primary communication protocols: the SD bus and the SPI bus. Some newer cards with extra pins also support UHS-II and PCIe, but the SD bus and SPI are the most common.
Most modern microcontrollers include SPI peripherals, so SPI is a common interface for embedded developers to communicate with SD cards. Some microcontrollers also include SDIO peripherals for the SD bus.
The SD bus can transfer up to 4 bits in parallel (via DAT0-DAT3) plus a separate command (CMD) line, so data transfer using the SD bus is typically 4 to 5 times faster than SPI, which transfers one bit per clock. Cards start in SD bus mode (1-bit) and can be switched to 4-bit mode by issuing the appropriate commands.
By default cards initialize in the SD bus mode (1-bit). They can be set to SPI mode by sending a specific command (CMD0) while holding the CS line low. Because the SD bus CMD pin is the same physical pin as the SPI MOSI pin, and the SD bus CLK pin is the same as the SPI SCK pin, an MCU SPI controller can be used to issue commands.
This article does not attempt to cover the full SD command set or communication protocol. The specification is available from the SD Association, and many developers use existing code libraries rather than implementing a stack from scratch.
Data Storage and Filesystems
SD card read and write operations are block oriented, typically with a block size of 512 bytes. File system schemes such as FAT16 or FAT32 are commonly implemented on cards as a host-level layer above the raw flash. The file system is handled by software on the host (microcontroller or computer), not by the SD card itself. Most developers use existing file system libraries rather than developing their own.
FAT16 was common until 2 GB became the practical upper limit for that format. FAT32 supports much larger volumes. As cards smaller than 2 GB have become less available, many projects have migrated from FAT16 to FAT32.
Reliability and Flash Behavior
SD cards use flash memory internally, so erase operations occur in units of blocks typically several kilobytes or larger. Writing new data may require erasing and rewriting large portions of a block. Because erase-write cycles take time, a power loss during these operations can cause data loss or file system corruption. File system libraries often buffer data in MCU or MPU RAM to reduce the number of erase-write cycles (and thus wear), but this buffering also increases the risk of data loss on power failure or system crashes.
Because flash supports a limited number of erase-write cycles, many SD cards include a controller that implements wear leveling to distribute writes across the media. This is transparent to the user because the controller maps physical addresses to logical addresses.
Industrial SD cards are available that offer higher reliability and durability. These cards include controllers with wear leveling and error correction code (ECC). High-end cards may use SLC (single-level cell) or pSLC (pseudo-SLC) flash instead of MLC (multi-level cell). MLC stores multiple bits per cell, which reduces cost but also reduces endurance and reliability. For remote, safety-critical, or high-reliability applications, consider industrial-grade SD cards.
Voltage Levels
Standard SD cards operate at 3.3 V, so a level shifter is required to connect to 5 V microcontrollers. SDHC and SDXC cards can switch to 1.8 V operation via command, which can be useful for high-speed signaling.
Tools and Simulation
Proteus includes MMC/SD card simulation models with SPI interfaces that can be connected to compatible microcontroller models for simulation. SPI communication between an MCU and an MMC/SD card can also be debugged using SPI debugging instrumentation.