All articles

    Logging Data to an SD Card with an Arduino

    Save sensor readings to a CSV file on an SD card: SPI wiring, choosing a module with 3.3 V conversion, the shared-SPI problem, formatting the card as FAT32, the SD and SdFat libraries, writing a line with the date and time, a file per day, storage in numbers, opening the file in Excel, the ESP32, and how not to lose data when the power goes off.

    Updated 5 min read

    Want to know how warm the room was all week, or log a battery's voltage as it drains? EEPROM is far too small for that. The answer is an SD card: gigabytes of space, and a CSV file that opens directly in Excel.

    The Arduino writes a line at intervals to a log.csv file on the card
    The Arduino writes a line at intervals to a log.csv file on the card

    The module and wiring

    SD cards run on 3.3 V. If you'll use an Uno (5 V):

    • Choose a module with a 3.3 V regulator and a level shifter. See level shifting.
    • Some cheap modules have only a regulator and no signal conversion, and they sometimes work and sometimes hang.
    ModuleArduino Uno
    CS10 (or any pin)
    MOSI11
    MISO12
    SCK13
    VCC5V (if it has a regulator)
    GNDGND

    This is SPI. See UART, I2C and SPI. With an ESP32 there's no voltage problem.

    With other SPI devices

    If the card shares the same SPI bus with another module (such as RFID or a display), each device has its own CS pin, and set all the CS pins HIGH at the start of setup before initialising any of them. And some cheap SD modules don't release the MISO line even when not selected, which breaks the other device. If that happens, try another module or keep the card on its own SPI bus.

    Preparing the card

    • FAT32, preferably 32 GB or smaller.
    • Format it on the computer before first use.
    • Cards larger than 32 GB usually come as exFAT, which the ordinary SD library can't read. The SdFat library supports it.

    The code

    #include <SPI.h>
    #include <SD.h>
    
    const int CS = 10;
    
    void setup() {
      Serial.begin(9600);
      if (!SD.begin(CS)) {
        Serial.println("SD failed");
        while (true);
      }
      File f = SD.open("log.csv", FILE_WRITE);
      if (f) { f.println("ms,temp"); f.close(); }
    }
    
    void loop() {
      float t = readTemp();
      File f = SD.open("log.csv", FILE_WRITE);
      if (f) {
        f.print(millis()); f.print(",");
        f.println(t, 1);
        f.close();
      }
      delay(60000);
    }
    • FILE_WRITE appends to the end of the file.
    • close() is the most important line. Without it, the data may not actually be written.
    • The older SD library understands only short file names (8 characters, a dot and 3). Stick to log.csv.

    Note: the header line (ms,temp) will be written again on every restart. If you don't want that, write it only if the file doesn't exist: if (!SD.exists("log.csv")).

    Real time instead of millis

    millis() returns to zero on every restart. Add a DS3231 RTC and write the date and time in the first column. And with an ESP32 you can get the time from the internet (NTP).

    DateTime now = rtc.now();
    char line[40];
    snprintf(line, sizeof(line), "%04d-%02d-%02d %02d:%02d:%02d,",
             now.year(), now.month(), now.day(),
             now.hour(), now.minute(), now.second());
    f.print(line);
    f.println(t, 1);

    This format (year-month-day) is understood by Excel as a date, and sorts correctly if you sort the columns.

    A file per day

    Instead of one file that grows forever, create a file named after the date: 20261201.csv. That's 8 digits, a dot and 3 letters, so it fits the short-name limit, and you can find any day easily.

    Storage in numbers

    A line such as 2026-12-01 14:30:00,24.6 is about 26 bytes. If you write a line every minute:

    • Per day: 1440 lines ≈ 37 KB.
    • Per year: about 14 MB.

    So an 8 GB card is enough for hundreds of years. Space is never the problem; the problem is keeping the file from getting corrupted.

    Opening the file in Excel

    • CSV = values separated by commas. Excel opens it directly.
    • If the columns all end up in a single column, your Windows region settings use a semicolon (;). Open the file via Data → From Text/CSV and choose the separator yourself, or write ; instead of , in the code.
    • From Excel you can make a chart in seconds, or use Google Sheets.

    With an ESP32

    • The default SPI pins are usually 18 (SCK), 19 (MISO), 23 (MOSI) and 5 (CS).
    • There's also a faster interface called SD_MMC on boards such as the ESP32-CAM.
    • And instead of a card you can store on the ESP32's own flash with LittleFS if the data is small, or send it straight to the internet.

    Don't lose your data

    • If the power goes off while you're writing, the file (or the card) can be corrupted.
    • Write once every so often and close the file straight away, instead of leaving it open.
    • If you're writing a lot and fast, leave the file open and call f.flush() from time to time: it writes what's in memory to the card without closing.
    • The card draws a high momentary current (tens of milliamps and more) while writing, so put a capacitor on the module's supply, and don't rely on a weak source. See decoupling capacitors.
    • For a battery project, consider ESP32 deep sleep.
    • Don't remove the card while the device is running. Make a "stop" button that closes the file and switches on an LED, then remove it.

    Common problems

    ProblemUsual cause
    SD failedWiring, the card isn't FAT32, wrong CS, or a module without voltage conversion
    The file is emptyYou forgot close()
    Works on USB but fails on batteryThe supply can't handle the momentary current
    The file won't openA name longer than 8 characters with the SD library
    The other SPI device stoppedThe module is holding MISO, or CS isn't HIGH
    A large card isn't readexFAT; use SdFat or format it as FAT32

    FAQ

    Can I read the file on the Arduino itself? Yes: SD.open("log.csv") without FILE_WRITE, then f.read() character by character until f.available() is zero.

    How long does the card last? A line every minute is very light work for it, and good cards last for years. Buy a card from a known brand, because counterfeit cards lie about their capacity and fail.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.