The SD card SPI interface for Arduino, ESP32, and STM32 is one of the most universally useful peripherals for data logging, file storage, and firmware loading. This comprehensive guide covers wiring, library usage, and practical tips for reliable SD card operation across all three popular Indian maker platforms.
Table of Contents
- SD Card SPI Protocol Basics
- Wiring Guide for All Three Boards
- Arduino SD Card Code
- ESP32 SD Card with SPI
- STM32 FatFS SD Card
- Reliability Tips for India
- Frequently Asked Questions
SD Card SPI Protocol Basics
SD cards operate in two modes: native SD bus (4-bit) and SPI mode. All microcontrollers use SPI mode for compatibility. The SPI interface uses four signals: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Serial Clock), and CS (Chip Select). SD cards run at 3.3V — connecting directly to 5V Arduino SPI pins can damage the card; always use a level converter or SD module with onboard 3.3V regulation.
Most Indian SD module boards (₹30–80 from local shops) include a 3.3V voltage regulator and level shifters, making them safe with both 5V Arduino and 3.3V ESP32/STM32.
Wiring Guide for All Three Boards
| SD Module Pin | Arduino Uno | ESP32 (WROOM) | STM32F103 |
|---|---|---|---|
| VCC | 5V | 3.3V | 3.3V |
| GND | GND | GND | GND |
| SCK | D13 | GPIO18 | PA5 (SPI1_SCK) |
| MOSI | D11 | GPIO23 | PA7 (SPI1_MOSI) |
| MISO | D12 | GPIO19 | PA6 (SPI1_MISO) |
| CS | D4 (or D10) | GPIO5 | PA4 or PB0 |
Arduino SD Card Code
#include <SPI.h>
#include <SD.h>
const int CS_PIN = 4; // SS/CS pin
void setup() {
Serial.begin(115200);
if (!SD.begin(CS_PIN)) {
Serial.println("SD card initialisation failed!");
return;
}
Serial.println("SD card ready.");
// Write data to file
File dataFile = SD.open("/datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.println("timestamp,temperature,humidity");
dataFile.printf("1710000000,28.5,65.2
");
dataFile.close();
Serial.println("Data written.");
}
// Read file
File readFile = SD.open("/datalog.csv");
while (readFile.available()) {
Serial.write(readFile.read());
}
readFile.close();
}
ESP32 SD Card with SPI
#include "SD.h"
#include "SPI.h"
#define SD_CS 5
#define SD_SCK 18
#define SD_MOSI 23
#define SD_MISO 19
void setup() {
Serial.begin(115200);
SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SD Mount Failed!");
return;
}
// Get SD card type and size
uint8_t cardType = SD.cardType();
Serial.printf("Card Type: %s
",
cardType == CARD_SDHC ? "SDHC" : "SD");
Serial.printf("Card Size: %llu MB
", SD.cardSize() / (1024*1024));
// Append sensor reading with timestamp
File f = SD.open("/sensors.csv", FILE_APPEND);
if (f) {
f.printf("%ld,%.2f,%.1f
", millis(), temperature, humidity);
f.close();
}
}
STM32 FatFS SD Card
STM32 uses the FATFS middleware (configured in STM32CubeMX) for SD card filesystem access. CubeMX generates boilerplate; add these to your user code:
/* STM32 FatFS example */
#include "fatfs.h"
FATFS SDFatFS;
FIL SDFile;
char sdPath[4];
void sdCard_init() {
MX_FATFS_Init(); // Generated by CubeMX
if(f_mount(&SDFatFS, (TCHAR const*)sdPath, 0) != FR_OK) {
Error_Handler();
}
}
void sdCard_write(const char *data) {
UINT bytesWritten;
if(f_open(&SDFile, "data.txt", FA_OPEN_APPEND | FA_WRITE) == FR_OK) {
f_write(&SDFile, data, strlen(data), &bytesWritten);
f_close(&SDFile);
}
}
Reliability Tips for India
- Use Class 10 microSD cards from reputable brands (SanDisk, Samsung) — cheap unbranded cards from Indian street markets cause random failures
- Format cards as FAT32 (not exFAT) for maximum compatibility across all platforms
- Use file.flush() after writes to ensure data is committed before power failure — important given India’s unpredictable power supply
- Capacitors (100 µF electrolytic + 100 nF ceramic) on the SD module power rail reduce noise from India’s noisy 230V supply
- SPI clock: start at 4 MHz for initialisation, increase to 25 MHz after successful init
Frequently Asked Questions
What is the maximum SD card size supported by Arduino?
Arduino’s SD library supports FAT16 (up to 2 GB) and FAT32 (up to 32 GB SDHC). SDXC cards (64 GB+) use exFAT which requires the SdFat library (recommended for large cards).
Why does my ESP32 SD card fail to initialise intermittently?
Common causes: insufficient decoupling capacitors, SPI clock too high at init, card not fully inserted, or power supply glitches (common with cheap Indian chargers). Add 100 µF capacitor to SD module VCC, reduce SPI clock to 4 MHz for init.
Can I use two SD cards simultaneously on one microcontroller?
Yes. Use two separate CS pins. Both cards share MOSI/MISO/SCK. Pull the unused CS high when accessing the other card. Both Arduino SD library and ESP32 SD library support this with separate SD objects.
Add comment