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

Libraries / Data and time
Arduino library · Data and time

# SdFat: faster SD card files with more control

SdFat is the library the Arduino SD library was originally built from, and Bill Greiman still maintains it. People move to it for speed (it can clock the SPI bus higher and use dedicated SPI), for FAT32 and exFAT on big cards, long file names, and for finer control over how files are opened and synced, which matters for fast loggers.

Open the example in the editor See the calls

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

## What SdFat does

Setup is more explicit than SD.h. You make an SdFat (or SdFs, which handles FAT and exFAT) object and call begin() with an SdSpiConfig that names the chip select pin, whether the SPI bus is shared, and the clock speed. Files are File32 for FAT or FsFile for either, and open() takes POSIX-style flags such as O_RDWR | O_CREAT | O_AT_END.

For a logger, open the file once, write lines, and call sync() every so often instead of closing and reopening: that keeps the directory entry up to date without the cost of a full open each time.

Library Manager name | SdFat

Author | Bill Greiman

Include | #include <SdFat.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Runs, with a gap the note below names.

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 <SdFat.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: It runs on SD.h’s FAT16 driver, so names are 8.3 and exFAT, rename() and truncate() are not here.

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.

### SdFat sd

The card and file system. SdFs sd; works with FAT and exFAT cards in upstream SdFat.

### sd.begin(SdSpiConfig(10, DEDICATED_SPI, SD_SCK_MHZ(16)))

Starts the card on chip select 10 with the SPI bus to itself at 16 MHz. Use SHARED_SPI when other devices sit on the bus.

### sd.initErrorHalt(&Serial)

Prints why begin() failed, with an error code, and stops. Handy while wiring a new module.

### file.open("log.txt", O_RDWR | O_CREAT | O_AT_END)

Opens a File32 or FsFile for reading and writing, creating it and positioning at the end for appending. Returns false on failure.

### file.println(value)

Writes a line through Print, as with Serial.

### file.sync()

Writes buffered data and the file size to the card without closing, so a logger can keep the file open safely.

### file.fileSize()

The file’s length in bytes.

### sd.ls(LS_SIZE)

Lists the root directory to Serial, here with each file’s size.

## Example: Write a log file with SdFat and list the card

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

sketch.ino · Arduino Uno R3

### Wiring

- Arduino Uno R3 10 to microSD card module CS
- Arduino Uno R3 11 to microSD card module MOSI
- Arduino Uno R3 12 to microSD card module MISO
- Arduino Uno R3 13 to microSD card module SCK
- Arduino Uno R3 5V to microSD card module VCC
- Arduino Uno R3 GND to microSD card module GND

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### sd.begin() fails at 50 MHz but the card is fine

Long jumper wires and breadboards cannot carry the fastest SPI clocks. Lower SD_SCK_MHZ() to 16 or 4; once it works, raise it step by step to find what the wiring allows.

### 'File' is ambiguous or redefined when SD.h is also included

SdFat and SD.h both declare a File type. Include one of them, not both, and use File32 or FsFile with SdFat so the type is clear.

### In Mokxi a long file name or rename() does not work

Mokxi runs SdFat on the SD library’s FAT16 driver, so names are 8.3 and exFAT, rename() and truncate() are missing. Keep names short in the simulator; on a real board upstream SdFat handles all three.

## Keep going

### Learn

- Arduino SD Card Module: A GPS Data Logger
- 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 SdFat without installing anything

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

Start building free

Open the editor
