Summary
This tutorial shows how to connect a micro SD card module to Arduino UNO, Nano, or ESP32 over SPI, format the card as FAT32, and write and read files. We build a DHT11 and DS3231 data logger that saves timestamped readings to a CSV file for Excel or Google Sheets.

An SD card module talks to Arduino over SPI. On the UNO, CS goes to pin 10, MOSI to pin 11, MISO to pin 12, and SCK to pin 13. The card must be formatted as FAT32 (or FAT16). The built-in SD library then lets us open a .csv file, write comma-separated lines to it, and close it.
What Is an SD Card Module?
An SD card module is a small breakout board that lets a microcontroller read and write files on a micro SD card over the SPI bus. It is the simplest way to add removable, high-capacity storage to an Arduino project.
A micro SD card holds billions of bytes, against the UNO's 1 KB of EEPROM. That makes the micro SD card module Arduino makers' go-to choice for data logging, saving settings, storing audio clips, and recording GPS tracks.
Why not wire the card straight to the Arduino? SD cards run strictly on 3.3V, while the UNO's pins output 5V. The module handles this with two parts:
- 3.3V voltage regulator: Steps the 5V supply down to the 3.3V the card needs.
- Logic level shifter: Converts the Arduino's 5V SPI signals to 3.3V so they don't damage the card.
| Specification | Value |
|---|---|
| Card type | Micro SD and micro SDHC |
| Supply voltage | 4.5V to 5.5V (3.3V regulated on board) |
| Interface | SPI |
| Pins | GND, VCC, MISO, MOSI, SCK, CS |
Micro SD card reader module with on-board 3.3V regulator and level shifter, SPI interface, works with Arduino, ESP32, and Raspberry Pi. Buy the SD Card Module
Components and Supplies
Pinout and SPI Wiring
The SD card module pinout has six pins: two for power and four for the SPI bus. SPI (Serial Peripheral Interface) is a fast, four-wire protocol where the Arduino acts as the controller and the card acts as the peripheral. For a deeper look at how SPI compares with I2C, see our guide on SPI and I2C interfaces.
MISO, MOSI, and SCK are fixed hardware SPI pins, so they differ from board to board, while CS can use any free digital pin (pin 10 is the standard choice). Here's the wiring for the three most popular boards:
| SD module pin | Arduino UNO | Arduino Nano | ESP32 DevKit (VSPI) |
|---|---|---|---|
| VCC | 5V | 5V | VIN (5V from USB) |
| GND | GND | GND | GND |
| MISO | D12 | D12 | GPIO 19 |
| MOSI | D11 | D11 | GPIO 23 |
| SCK | D13 | D13 | GPIO 18 |
| CS | D10 | D10 | GPIO 5 |
A few wiring tips before we power up:
- Power the module from 5V, not 3.3V. Its regulator needs at least 4.5V. On the ESP32, we use the VIN pin, which carries 5V over USB.
- Keep jumper wires short. SPI runs at several MHz, and wires longer than about 20 cm can corrupt data.
- On the UNO, leave pin 10 as an OUTPUT even if CS moves to another pin. The SPI hardware needs it to stay in controller mode.

Formatting the Card Correctly
The Arduino SD library only reads FAT16 and FAT32, so the card must be FAT32 before first use. Card size decides the factory format:
| Card class | Capacity | Factory format | Works with SD library? |
|---|---|---|---|
| SD | Up to 2 GB | FAT16 | Yes |
| SDHC | 4 GB to 32 GB | FAT32 | Yes (recommended) |
| SDXC | 64 GB and above | exFAT | No, needs reformatting to FAT32 with a third-party tool |
To format the card:
- Insert the micro SD card into a laptop using an adapter or USB card reader.
- Run the free SD Card Formatter from the SD Association, which gives the most reliable results.
- Select the card, choose Quick format, and click Format.
- On Windows, the built-in Format option with FAT32 also works. On macOS, use Disk Utility > Erase > MS-DOS (FAT).
To confirm the card and wiring work, we open File > Examples > SD > CardInfo in the Arduino IDE, set chipSelect to 10, and upload it. The Serial Monitor should print the card type, FAT type, and volume size.
Code: Write and Read a File
First, let's confirm that the Arduino can write a file to the SD card and read it back. The SD and SPI libraries come pre-installed with the Arduino IDE.
Upload this sketch, then open the Serial Monitor at 9600 baud:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // CS pin of the SD card module
void setup() {
Serial.begin(9600);
while (!Serial) { } // wait for Serial on boards with native USB
Serial.print(F("Initializing SD card... "));
if (!SD.begin(chipSelect)) {
Serial.println(F("initialization failed!"));
while (true); // stop here if the card is not found
}
Serial.println(F("done."));
// ---- Write ----
File myFile = SD.open("test.txt", FILE_WRITE); // creates the file or appends to it
if (myFile) {
myFile.println("Hello from Robocraze!");
myFile.close(); // close() saves the data to the card
Serial.println(F("Wrote to test.txt"));
} else {
Serial.println(F("Error opening test.txt for writing"));
}
// ---- Read ----
myFile = SD.open("test.txt"); // opens in read mode by default
if (myFile) {
Serial.println(F("Contents of test.txt:"));
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println(F("Error opening test.txt for reading"));
}
}
void loop() {
// nothing to repeat
}
Code Explanation
In setup(), SD.begin(chipSelect) mounts the card. It returns false if the wiring, format, or CS pin is wrong, and the sketch stops so the error is easy to spot.
The write block opens test.txt with FILE_WRITE, which creates the file or appends to it. Calling close() matters: the library buffers data in RAM and only commits it to the card on close() or flush().
The read block reopens the file and prints it byte by byte until available() returns 0. The F() macro keeps the messages in flash memory, saving the UNO's 2 KB of RAM for the SD library's 512-byte buffer.
These are the SD library functions we use most often:
| Function | What it does |
|---|---|
SD.begin(cs) |
Mounts the card; returns true on success |
SD.open(name, FILE_WRITE) |
Opens a file for writing, creating it or appending to it |
file.print() / file.println()
|
Writes text or numbers to the file |
file.flush() |
Saves buffered data without closing the file |
file.close() |
Saves the data and closes the file |
SD.exists(name) |
Checks whether a file already exists |
The SD library uses the old 8.3 file name format. Names can have at most 8 characters, a dot and a 3-character extension, like DATALOG.CSV. A longer name makes SD.open() fail.
Project: Temperature Data Logger With DHT11 + DS3231 Timestamps
Now, let's build a complete Arduino data logger. Every 5 seconds, it reads temperature and humidity from a DHT11, takes the date and time from a DS3231 real-time clock, and appends one row to DATALOG.CSV on the SD card. The same setup is the starting point for our real-time weather monitoring system using Arduino.
Why add an RTC? millis() resets to zero after every power cut, while the DS3231 keeps accurate time (about ±2 ppm) on its coin cell, so every row carries a real timestamp.
Components Required
| Component | Qty | Role in the project |
|---|---|---|
| Arduino UNO R3 | 1 | Reads the sensors and writes the file |
| SD Card Module | 1 | Stores the CSV file |
| Micro SD card, 4–32 GB, FAT32 | 1 | Storage media |
| DHT11 temperature and humidity sensor | 1 | Measures temperature and humidity |
| DS3231 RTC Module | 1 | Provides date and time for each reading |
| CR2032 coin cell | 1 | Keeps the RTC running when power is off (the module ships without one) |
| Breadboard and jumper wires | 1 set | Connections |
A note on the CR2032: DS3231 modules with the AT24C32 EEPROM, like this one, include a charging circuit (a 200 Ω resistor and a diode) meant for a rechargeable LIR2032 cell. A standard CR2032 is not rechargeable and can swell or leak when charged, so we either fit an LIR2032 or remove the charging resistor before inserting a CR2032.
Our DHT11 vs DHT22 comparison explains when the upgrade is worth it. The code below works with both; just change DHT11 to DHT22.
Circuit Connections
The SD module uses the SPI pins, the DS3231 uses the I2C pins, and the DHT11 needs one digital pin. None of them clash, so all three share the UNO's 5V and GND rails.
| Module | Module pin | Arduino UNO pin |
|---|---|---|
| SD card module | VCC / GND | 5V / GND |
| SD card module | MISO / MOSI / SCK / CS | D12 / D11 / D13 / D10 |
| DS3231 RTC | VCC / GND | 5V / GND |
| DS3231 RTC | SDA / SCL | A4 / A5 |
| DHT11 (3-pin module) | VCC / GND | 5V / GND |
| DHT11 (3-pin module) | DATA | D2 |
With a bare 4-pin DHT11 instead of a module, add a 10 kΩ pull-up resistor between DATA and 5V.

Libraries to Install
Open Sketch > Include Library > Manage Libraries and install:
- DHT sensor library by Adafruit (accept the prompt to also install Adafruit Unified Sensor)
- RTClib by Adafruit
Arduino Data Logger Code
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 data pin
#define DHTTYPE DHT11 // change to DHT22 if needed
const int chipSelect = 10; // SD module CS pin
const unsigned long LOG_INTERVAL = 5000; // time between readings in ms
const char FILENAME[] = "DATALOG.CSV"; // 8.3 file name
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
unsigned long lastLog = 0;
void setup() {
Serial.begin(9600);
dht.begin();
if (!rtc.begin()) {
Serial.println(F("DS3231 not found. Check SDA/SCL wiring."));
while (true);
}
if (rtc.lostPower()) {
// Set the RTC to the time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if (!SD.begin(chipSelect)) {
Serial.println(F("Card initialization failed!"));
while (true);
}
// Write the header row only when the file is new
if (!SD.exists(FILENAME)) {
File f = SD.open(FILENAME, FILE_WRITE);
if (f) {
f.println(F("Date,Time,Temperature_C,Humidity_pct"));
f.close();
}
}
Serial.println(F("Logger ready."));
}
void loop() {
if (millis() - lastLog < LOG_INTERVAL) return; // not time yet
lastLog = millis();
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println(F("DHT11 read failed, skipping this sample"));
return;
}
// Build "YYYY-MM-DD,HH:MM:SS"
DateTime now = rtc.now();
char stamp[20];
snprintf(stamp, sizeof(stamp), "%04d-%02d-%02d,%02d:%02d:%02d",
now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
File f = SD.open(FILENAME, FILE_WRITE);
if (f) {
f.print(stamp);
f.print(',');
f.print(tempC, 1); // one decimal place
f.print(',');
f.println(humidity, 0); // whole number
f.close(); // commit the row to the card
Serial.print(F("Logged: "));
Serial.print(stamp);
Serial.print(',');
Serial.print(tempC, 1);
Serial.print(',');
Serial.println(humidity, 0);
} else {
Serial.println(F("Error opening DATALOG.CSV"));
}
}
Code Explanation
In setup(), the sketch starts each module and stops with a clear message if one fails. If rtc.lostPower() reports no valid time, the clock is set to the compile time, so we upload right after compiling.
The header row is written only when DATALOG.CSV is new, so a reset adds readings below the old ones instead of repeating the column names.
In loop(), a millis() timer triggers a reading every LOG_INTERVAL milliseconds without delay(). Failed DHT11 reads return NaN and are skipped, and snprintf() adds leading zeros so timestamps sort correctly.
Each row ends with println(), and the file is closed straight away. That costs a few milliseconds per reading, but if power is cut, we lose at most one row.

With the Serial Monitor open at 9600 baud, a new line appears every 5 seconds:
Logger ready.
Logged: 2026-11-06,14:05:30,27.0,61
Logged: 2026-11-06,14:05:35,27.0,61
Logged: 2026-11-06,14:05:40,28.0,60
Opening the CSV in Excel or Google Sheets
A CSV (comma-separated values) file is plain text, so every spreadsheet app opens it directly. Here's what DATALOG.CSV looks like inside:
Date,Time,Temperature_C,Humidity_pct
2026-11-06,14:05:30,27.0,61
2026-11-06,14:05:35,27.0,61
2026-11-06,14:05:40,28.0,60
2026-11-06,14:05:45,28.0,60
Before removing the card, we unplug the Arduino. Pulling the card out mid-write can corrupt the file.
Microsoft Excel
- Go to Data > From Text/CSV and select the file.
- Confirm the delimiter is Comma and click Load.
- Select the Time and Temperature_C columns and choose Insert > Line Chart.
Google Sheets
- Go to File > Import > Upload and drag in
DATALOG.CSV. - Set Separator type to Comma and click Import data.
- Select the data and choose Insert > Chart.
DATALOG.CSV from the data logger, plotted as a temperature and humidity line chart (one day of readings)
Troubleshooting: Card Init Failed
"Initialization failed" is the most common SD card error on Arduino, and it almost always comes down to wiring, format, or power. When the SD card is not detected by Arduino, we check these causes in order:
| Symptom | Likely cause | Fix |
|---|---|---|
| Fails on every attempt | MISO and MOSI swapped | On the UNO, MISO goes to D12 and MOSI to D11 |
| Fails with correct wiring | Card formatted as exFAT or NTFS | Reformat as FAT32, or use a 4–32 GB card |
| Never detected | CS pin in code doesn't match the wiring | Match the pin in SD.begin() to the CS wire |
| Not detected despite correct wiring and format | VCC connected to 3.3V | Move VCC to 5V |
| Works sometimes, fails after a reset | Loose contacts or long jumper wires | Reseat the wires and keep SPI wires under 20 cm |
| Fails with other SPI devices connected | Another device holding the MISO line | Set every unused CS pin HIGH before SD.begin()
|
Excerpt
Frequently Asked Questions
1. Which pins does the SD card module use on Arduino UNO?
MOSI goes to D11, MISO to D12, SCK to D13, and CS usually to D10, though any free digital pin works if it's passed to SD.begin(). The Arduino Nano uses the same pins.
2. What format should the SD card be?
FAT32. The SD library reads only FAT16 and FAT32, not exFAT or NTFS. Cards of 32 GB and smaller usually ship as FAT32, and the free SD Card Formatter fixes any that don't.
3. Why does it say card initialization failed?
"Card initialization failed" means SD.begin() couldn't talk to the card. The usual causes are swapped MISO and MOSI wires, a mismatched CS pin, an exFAT card, or the module powered from 3.3V. The troubleshooting table above covers each fix.
4. What size SD card works with Arduino?
Cards from 2 GB to 32 GB work out of the box. Cards of 64 GB and above ship as exFAT, which the SD library can't read, so a 4–32 GB card is the simplest choice.
5. Can we log data every second?
Yes. Setting LOG_INTERVAL to 1000 logs once per second, which is the DHT11's limit. At about 30 bytes per row, that's roughly 2.6 MB per day, so an 8 GB card holds several years of data. For faster logging, we keep the file open and call flush() periodically.
6. Can we use the SD module with ESP32?
Yes. Wire it to the ESP32's VSPI pins (MOSI GPIO 23, MISO GPIO 19, SCK GPIO 18, CS GPIO 5) and power it from VIN. The ESP32 core includes its own SD library, so the same SD.begin() and SD.open() code works.
7. Can we use the SD card module with other SPI devices?
Yes. SPI devices share MOSI, MISO, and SCK, and each gets its own CS pin. We set every unused CS pin HIGH in setup() before calling SD.begin(), so only the SD card answers.
8. How do we stop the SD card from getting corrupted?
Close or flush the file after every write, and switch off the Arduino before removing the card. A stable power supply matters too, since a voltage dip mid-write can damage the file system. On battery power, a 100 µF capacitor across the module's VCC and GND helps.
SD.begin(). The Arduino Nano uses the same pins.SD.begin() couldn't talk to the card. The usual causes are swapped MISO and MOSI wires, a mismatched CS pin, an exFAT card, or the module powered from 3.3V. The troubleshooting table above covers each fix.LOG_INTERVAL to 1000 logs once per second, which is the DHT11's limit. At about 30 bytes per row, that's roughly 2.6 MB per day, so an 8 GB card holds several years of data. For faster logging, we keep the file open and call flush() periodically.SD.begin() and SD.open() code works.setup() before calling SD.begin(), so only the SD card answers.







