Source: https://mokxi.com/libraries/sd
Updated: 2026-11-02

Libraries / Data and time
Arduino library · Data and time

# The Arduino SD library: read and write files on a microSD card

SD is the library that ships with the Arduino IDE for SD and microSD cards on the SPI bus. It reads the FAT file system on the card, so a file you write from a sketch opens on a computer afterward, which is the whole point of a data logger: readings with timestamps in a CSV file, one line at a time.

Open the example in the editor See the calls

Built in on Mokxi: #include <SD.h> and it builds, with nothing to install. Free, and no account needed.

## What SD does

The module shares SPI with anything else on the bus: MOSI, MISO and SCK on the board’s SPI pins (11, 12 and 13 on an Uno) and its own chip select pin, which you pass to SD.begin(). Most microSD breakouts have a level shifter and a 3.3 V regulator, so they run from 5 V; the bare card itself would not survive 5 V signals.

File names are the old 8.3 kind, up to eight characters, a dot and three more, and only one file of the library’s buffer is kept in RAM, about 600 bytes on an Uno. Open, write, close: a file that is never closed or flushed may lose its last lines when power goes.

Library Manager name | SD

Author | Arduino, SparkFun

Include | #include <SD.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | microSD card module

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <SD.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "SD" by Arduino, SparkFun from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add SD to lib_deps in PlatformIO. Mokxi follows the calls of release 1.3.0, so a sketch written here builds there too.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### SD.begin(4)

Starts the card with chip select on pin 4 (use 10, or whatever your module is wired to). Returns false when no card answers or it is not formatted FAT.

### File dataFile = SD.open("datalog.txt", FILE_WRITE)

Opens a file for appending, creating it if needed. Without FILE_WRITE it opens for reading. The result is false if the open failed.

### dataFile.println(reading)

Writes a line, with everything Print offers: print(), println() and write().

### dataFile.close()

Writes what is buffered to the card and frees the file. dataFile.flush() writes without closing.

### dataFile.available()

The bytes left to read; loop on it with dataFile.read() to copy a file to Serial.

### SD.exists("datalog.txt")

True when the file or directory is on the card.

### SD.remove("datalog.txt")

Deletes a file. SD.mkdir() and SD.rmdir() make and remove directories.

### File entry = root.openNextFile()

Steps through a directory opened with SD.open("/"), as the listfiles example does; entry.name() and entry.isDirectory() describe each one.

## Example: GPS logger

On the Arduino Mega 2560 R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Mega 2560 R3

Open it in the editor, wired and ready to run · GPS logger: the project page

## Common errors and how to fix them

### initialization failed!

Check the chip select pin passed to SD.begin() matches the wiring, that MOSI, MISO and SCK go to the board’s SPI pins, and that the card is formatted FAT16 or FAT32 (not exFAT, which large cards come with). Try another card: some cheap ones do not answer in SPI mode.

### SD.open() returns false for a file name that looks right

The SD library only does 8.3 names, so "temperature.csv" fails while "temps.csv" works. A name with a folder needs the folder to exist first, created with SD.mkdir().

### The last readings are missing from the file

Data stays in the 512-byte buffer until close() or flush(). Close the file after each write, or flush every few lines, so a reset or a pulled card does not lose them.

## Projects that use SD

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Rocket flight computer A model rocket flight computer: launch, burnout and a filtered apogee that fires the parachute servo, with a self-test, beeps and a flight log on microSD. Temperature data logger with an SD card A DS18B20, a DS3231 clock and a microSD card: every 5 seconds a dated temperature line goes into TEMPLOG.CSV, ready to open in a spreadsheet. RFID attendance system RFID attendance system on an Arduino: tap a card on an RC522, the LCD shows IN, LATE or OUT with the DS3231 time, and a CSV on the SD card logs it.

## Keep going

### Learn

- Arduino SD Card Module: A GPS Data Logger
- Arduino Temperature Data Logger Lab, Step by Step
- SPI Explained: Clock, Data and Chip Select

### Parts

- microSD card module

### More data and time libraries

- QRCode
- Madgwick
- PID
- TimerOne
- MsTimer2
- Low-Power
- RTClib
- DS1307RTC

Every built-in library · Open the editor

## Try SD without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
