Use an SD card module with Arduino

FunduinoLast updated on

Daten auf einer SD-Karte speichern und wieder auslesen

Mit einem SD-Karten-Modul kann der Arduino Daten dauerhaft speichern – auch dann, wenn er zwischendurch vom Strom getrennt wird. Das ist zum Beispiel praktisch, wenn du Messwerte eines Sensors über einen längeren Zeitraum aufzeichnen möchtest.

In dieser Anleitung schließt du das Modul an den Arduino an, legst mit einem Sketch eine Textdatei auf der SD-Karte an, schreibst Text hinein und liest ihn anschließend wieder aus.

Circuit diagram with SPI SD card module on the Arduino UNO: CS to D5, SCK to D13, MOSI to D11, MISO to D12
Schematic: SD card module on the Arduino UNO

You need this

MicroSD card module - SPI, TF card reader, 5 V MicroSD card module - SPI, TF card reader, 5 V 1,72 € *Item no. F23106642About the product — MicroSD card module - SPI, TF card reader, 5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 3 parts together 14,58 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Connect the SD card module

The SD card module has six pins:

  • CS – SD card select pin, connected to pin D5 on the Arduino
  • SCK – Data transfer synchronization pin, connected to pin D13 on the Arduino
  • MOSI – Data transfer pin (Master Out Slave In), connected to pin D11 on the Arduino
  • MISO – Data transfer pin (Master In Slave Out), connected to pin D12 on the Arduino
  • VCC – Power supply pin, connected to the 5V pin on the Arduino
  • GND – Ground pin, connected to the GND pin on the Arduino
Circuit diagram with SPI SD card module on the Arduino UNO: CS to D5, SCK to D13, MOSI to D11, MISO to D12
Schematic: SD card module on the Arduino UNO

Library and example sketch

Once the wiring is in place, you can start working on the sketch. The library for the SD card module is already included in the Arduino software, so you don't need to install anything extra.

For data transfer, you can use the ReadWrite example sketch. To make it easier to understand, we translated it into German, made a few minor changes, and added comments.

There are two things you need to keep in mind: You can only work with one file on the SD card at a time—this applies to both reading and writing. If you want to store values in different files, you need to close each file after writing to it. Also, the module can only access SD cards formatted as FAT32.

Arduino software menu with the ReadWrite example sketch for the SD library
You can find the ReadWrite example sketch in the Arduino software under Examples → SD

Write and read a text file

The sketch creates the file test.txt, writes three lines of sample text to it, closes it, and then reads it back character by character into the Serial Monitor. Everything happens in setup - loop stays empty so the process runs only once.

Program code – write and read a text file
#include <SPI.h>
#include <SD.h>  // In this sketch, we use the SD library
File Textdatei;  // Here, the variable "Textdatei" is declared as a File (German: Datei).
void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card");
  if (!SD.begin(5)) {                                 // If the SD card cannot be found ...
    Serial.println("Initialization failed!"); // ... an error message should be displayed.
    return;
  }
  Serial.println("Initialization complete");     // ... otherwise, this message.
  Textdatei = SD.open("test.txt", FILE_WRITE);        // Here, the text file is created: It should be named "test" and created in the ".txt" format.
  if (Textdatei)                                      // If the text file was found ...
  {
    Serial.println("Writing to text file ...");      // ... a message should appear in the Serial Monitor ...
    Textdatei.println("Funduino GmbH");               // ... and the text file should then be filled.
    Textdatei.println("1, 2, 3, 4, 5");
    Textdatei.println("a, b, c, d, e");
    Textdatei.println();
    Textdatei.close();                                // The text file is then closed again ...
    Serial.println("Completed.");                 // ... and another message is displayed.
    Serial.println();
  }
  else                                                // If NO text file can be found ...
  {
    Serial.println("Could not read text file"); // ... an error message appears.
  }

  // NOW THE TEXT FILE IS READ
  Textdatei = SD.open("test.txt");                    // The text file on the SD card is opened again ...
  if (Textdatei)
  {
    Serial.println("test.txt:");                      // ... and the file name is displayed.
    while (Textdatei.available())                     // The file is then read until ...
    {
      Serial.write(Textdatei.read());                 // ... no more data is found.
    }
    Textdatei.close();                                // The text file is then closed again.
  }
  else                                                // If test.txt cannot be found ...
  {
    Serial.println("Could not open text file"); // ... an error message appears.
  }
}
void loop()  // The loop remains empty.
{
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.