Use an SD card module with Arduino
FunduinoLast updated on
Daten auf einer SD-Karte speichern und wieder auslesen
You need this
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
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
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
Library and example sketch
Write and read a text file
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