Component guide · Beginner · 3 min read
SD Card Modules

Data Logging with Arduino and ESP32
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
A microSD card module lets a microcontroller save files you can open on a computer, such as a CSV of sensor readings. It connects over SPI with four signal wires plus power, and modules for Arduino include a 3.3 V regulator and level shifter. Format the card as FAT32, open the file, write a line and close or flush it often so a power cut does not lose data.
Key specs at a glance
- Interface
- SPI: MOSI, MISO, SCK, CS
- Card voltage
- 3.3 V; 5 V modules include a regulator and level shifter
- Format
- FAT16 or FAT32 (cards up to 32 GB are easiest)
- Current
- Bursts of up to about 100 mA while writing
- Libraries
- Arduino SD, SdFat; ESP32 SD and SD_MMC
- ESP32 option
- SD_MMC 1-bit or 4-bit mode for higher speed on boards like ESP32-CAM
Wiring
On an Uno: MOSI to D11, MISO to D12, SCK to D13 and CS to D4 or D10, with VCC and GND. Use a module with a level shifter for 5 V boards. On an ESP32, use a 3.3 V module without a regulator, or power the regulated module from 5 V, and use the VSPI pins (23, 19, 18, CS on 5).
Other SPI devices can share MOSI, MISO and SCK, but some cheap SD modules do not release MISO, which stops other devices from working. Test them together early.
Logging to a CSV file
Open, write one line, close.
#include <SPI.h>
#include <SD.h>
const int CS = 4;
void setup() {
Serial.begin(9600);
if (!SD.begin(CS)) { Serial.println("Card failed"); while (1); }
}
void loop() {
File f = SD.open("log.csv", FILE_WRITE); // appends on most cores
if (f) {
f.print(millis()); f.print(',');
f.println(analogRead(A0));
f.close(); // saves data to the card
}
delay(1000);
}Reliable logging
- Close or
flush()after each line or every few lines; data in the buffer is lost if power drops. - Use 8.3 filenames (8 characters plus a 3-character extension) with the classic SD library.
- Add a timestamp from an RTC so readings make sense later.
- Some cards are not detected; try a different card or format it with the SD Association formatter.
- For small settings or a few values, EEPROM or the board's flash is simpler.
Common mistakes to avoid
- Using an exFAT-formatted card with a FAT-only library.
- Never closing the file, so data is lost.
- A 3.3 V-only module on a 5 V Uno without a level shifter.
- A module that blocks the shared MISO line.
- Long filenames with the classic SD library.
Frequently asked questions
What size SD card can I use?
Cards up to 32 GB formatted as FAT32 work with most libraries. Larger cards need reformatting or the SdFat library.
Why does SD.begin() fail?
Check wiring, the CS pin, the card format and power. Try another card.
Can I use an SD card with ESP32?
Yes, over SPI or with the faster SD_MMC interface on supported pins.
How often should I save?
Close or flush after every line for safety, or every few seconds if speed matters.
Can the SD card share SPI with a display?
Usually yes, with separate CS pins, as long as the module releases MISO.
Parts for this guide
Terms in this guide
Projects that use it
Put this into practice: 104 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
- SD Association Physical Layer Simplified Specification
- Arduino SD library reference
- SdFat library documentation
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

