In this tutorial, it is shown how to read and write SD cards utilizing the “MicroSD Card Adapter” module and Arduino Uno. An major advantage of the module is that it can be used with the SD library that comes with the Arduino IDE. The SD library makes initialization, reading, and writing of the card very easy.
List of materials:
How to connect the “MicroSD Card Adapter” module to the Arduino Uno?
The module comes with a voltage regulator. Therefore, the Arduino’s 5V and 3.3V pin can be used for the voltage supply. The module communicates via SPI (Serial Peripheral Interface) to the Arduino Uno. The following table shows the complete pin layout.
MicroSD Card Adapter Pin –> Arduino Uno Pin:
- CS –> 4
- SCK –> 13
- MOSI –> 11
- MISO –> 12
- VCC –> 5V
- GND –> GND
How to program the SD card reader?
As mentioned before, reading and writing an SD card is very simple when the standard SD library of the Arduino IDE is used. Make sure to use the latest version of the SD library (Sketch -> Include Library -> Manage Libraries -> Search for “SD”). For example, in my case, version 1.1.0 did not work with the module. Fortunately, version 1.1.1 did work without any problems. Moreover, the SD card must be formatted as FAT16 or FAT32. If something does not work as expected, a good start for debugging is always to upload CardInfo example of the library (File -> Examples -> SD -> CardInfo) to the Arduino and read the messages of the serial monitor.
In this tutorial’s code, a random number between 0 and 9 is written to an SD card. In particular, the number is written to a file named “file.txt”. Next, the content of “file.txt” is read. At the end of the loop function, a delay of 5 seconds is added. Please note that when the Arduino is started, it is checked whether a file named “file.txt” exists (see setup function). If so, the file is deleted.
// (c) Michael Schoeffler 2016, http://www.mschoeffler.de #include <SD.h> //Load SD library int chipSelect = 4; //chip select pin for the MicroSD Card Adapter File file; // file object that is used to read and write data void setup() { Serial.begin(9600); // start serial connection to print out debug messages and data pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode if (!SD.begin(chipSelect)) { // Initialize SD card Serial.println("Could not initialize SD card."); // if return value is false, something went wrong. } if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted Serial.println("File exists."); if (SD.remove("file.txt") == true) { Serial.println("Successfully removed file."); } else { Serial.println("Could not removed file."); } } } void loop() { file = SD.open("file.txt", FILE_WRITE); // open "file.txt" to write data if (file) { int number = random(10); // generate random number between 0 and 9 file.println(number); // write number to file file.close(); // close file Serial.print("Wrote number: "); // debug output: show written number in serial monitor Serial.println(number); } else { Serial.println("Could not open file (writing)."); } file = SD.open("file.txt", FILE_READ); // open "file.txt" to read data if (file) { Serial.println("--- Reading start ---"); char character; while ((character = file.read()) != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor Serial.print(character); } file.close(); Serial.println("--- Reading end ---"); } else { Serial.println("Could not open file (reading)."); } delay(5000); // wait for 5000ms }
If everything works correctly, the serial monitor should show a similar output as shown in the following screenshot:
Video Tutorial
Hello,
at the moment I´m working at a projekt with an Arduino Pro Mini. I´m also using the SD-Card Adapter you´ve showed.
Is it possible for you to send me the Fritzing-file of the SD-Card Adapter you created? That would be great.
Thanks in advance.
Yours sincerely
Tizian
I just sent you an eMail ;)
Hello,
the same thing :
Hello,
at the moment I´m working at a projekt with an ESP32 with NEO 6M GPS I´m also using the SD-Card Adapter you´ve showed.
Is it possible for you to send me the Fritzing-file of the SD-Card Adapter you created?
That would be great.
Hi, I´m working in a project too but I´m working with a PT100 and a RTD converter, a “MAX31865”, and saving my temperature value´s in a Micro SD Card and I can´t find the Micro SD Adapter file for fritzing and in my research I find this publication, would it be possible to you to send me the fritzing file ? it would be very helpful.
Thanks in advance
I just sent you an email.
Hi Mr. Schöffler,
could you also send me the fritzing file for the MicroSD Card-Module I’m currently working on my Master Thesis about this Topic and would really appreciate it. Thank you.
I am in the same situation, I couldn’t find the micro sd adapter file for fritzing and I find this publication.
Can you help me too?
Thanks
hai, when we plugged off the power supply and reconnect back, the new file will replace the old one, how we want to create new file without the old one being deleted?
Thank you for a nice description. One correction: the statement “Therefore, the Arduino’s 5V and 3.3V pin can be used for the voltage supply” is not correct. The SPI Reader Micro Speicher SD TF Karte Memory Card Shield Module fuer Arduino requires 5V.
Hello, my SD card will work 1 out of 50 tries. there is always an initialization error. I made sure that my SD cards type is FAT32 and the CS PIN is 4 (I’m using Arduino UNO).
I would appreciate it if you could help me.
Thank you.
[…] To be honest, the tutorial’s setup of an Arduino and sensors won’t make any real sense. It fulfills only one purpose: to collect some data that can be used to do statistics. I chose to use two ultrasonic sensors of type HC-SR04. In particular, an Arduino is plugged into the center of a breadboard. One ultrasonic sensor is plugged into the left part of the breadboard and the other sensor is plugged into the right part. As a result, the left sensor measures the distance to the left side and the right sensor measures the distance to the right side. I won’t cover the wiring in detail since I already wrote a tutorial that shows how to connect an HC-SR04 to an Arduino. In addition, I wire an SD card read/writer to the Arduino. Again, I won’t go into details here as I also wrote a tutorial about wiring an SD card reader to the Aruino. […]
[…] Le principe : L’arduino choisit un nombre entre 1 et 9, ouvre un fichier texte puis écrit le nombre et le lit. Ce programme vient de www.mschoeffler.de. […]
hi ..
can you give me a code how to delete a data inside files
what i mean….sample.txt
and the data inside let say:
1
2
3
4
5
if i want to delete 1 and 2 3 4 5
will be save again in sample.txt
pls send me a reply …any help will be appriaceted
i buy u a cup of coffe or case of beer
In what degree are you free to decide the pins you’re using?
Can I just pick any pin (i.e. pin 8 or 10 instead of 4) and change the code a bit or do I have to use a “~” pin for MOSI?
I understand that the “~” pins have more functions but I dont understand when you need those pins.
Thansk in advance for your time!
hi great video! however can I delete certain line of text in the text file example
abcd
efgh
ijkl
i just want to delete abcd and so on…can this be done? thank you very much?
I got CLK instead of SCK.
What to do????
Hallo Michael,
“How to program the RFID reader?” sollte wohl
How to program the sd-card reader?
Gruß Helmut
Danke, ist korrigiert!
Good day, Michael.
Great tutorial and if works great. Thank you very much. I would require the SD Card Fritzing file too, please! Hahaha! Thank you for writing back to me. I would have another question for you.
I want to store several ‘short’ and ‘float’ variables to the file and being able to retrieve them one at a time, sequentially without having to read the whole file at once. Something like the following sequence.
open file,
write variable a
write variable b
write variable c
close file
…
wait 500ms
…
open file,
read variable a
variable x = variable a
close file
…
open file,
read variable a … discard variable a
read variable b
variable y = variable b
close file
…
and so on. my problem is that my file is getting bigger and bigger every minute, as a new series of data gets added to it. But for the purpose if my application, only the last 384 series on variables would be needed et reconstitute a graphic. I can manage the pointers and indexes at will, for it the file contains 5000 entries, I can discard the first few and keep only the last 384 variables. But I need a way to get them in the same sequence they were written, of course.
Can I do that ? Could you please show me just how I coud achieve this function ?
Thank you for your generous help. Until then, I wish you have a great day.
Hello! First of all thank you for the tutorial, it was clear and helpfull. I am using the sd card to store data from sensors during the flight of a model rocket. I am doing also the fritzing sketch of my project, but I have not been able to find the sd card adapter part anywhere. Would it be possible for you to share it with me?
Thank you in advance
Hi, I´m working on a project too but I´m working with a SI7021”, and saving my temperature and humidity value´s in a Micro SD Card and I can´t find the Micro SD Adapter file for fritzing so is it possible to send me the fritzing file ? it would be very helpful.
If i can write the data with respect to time and if power loss happen how can i continue to write a data to the SD card with earlier data.
hi i connected sd card interfacing with Arduino uno and i given pin description MOSI 11, MISO 12, SCK 13, CS 10, iam try to save the ultrasonic sensor values in sd card excel format if any body can know this please reply me thank you
Could not initialize SD Card
Hallo Michael,
ich habe einProblem mit meinem Micro SD Adapter: Er lief schon einmal einwandfrei, meine Daten wurden erfolgreich von dem Arduino Nano auf die Micro SD Karte geschrieben. Ich habe mein Projekt draußen verwendet, also hat es Tag/Nacht Schwankungen gemessen. Nach zwei Tagen hat die Aufzechung der Daten aufgehört, seitdem kann ich die SD-Karte zwar immer noch am PC benutzen, allerdings nicht mit dem Arduino (Initialisierung schlägt fehl).
Hast du eine Idee, was das Problem sein könnte?
Viele Grüße und danke für das Tutorial.
Hello,
at the moment I´m working at a projekt with an Arduino Pro Mini. I´m also using the SD-Card Adapter you´ve showed.
Is it possible for you to send me the Fritzing-file of the SD-Card Adapter you created? That would be great.
Thanks in advance.
Yours sincerely
Belle
Hi Mr. Schöffler,
could you also send me the fritzing file for the MicroSD Card-Module I’m working on a student project and this would be amazing.
Vielen Dank und schöne Grüße!
Hello,
Your program is working correctly with a Micro SD Card adapter HW-125. (Read/Write with file.txt)
Can you help me because I need a special answer..
I doesn’t arrive to have a correct connexion with a MP3-TF-16P so I buy a Micro SD Card Adapter HW-125.
Can I play music MP3 or Wav with it ? Have-you a program or a sample ?
Regards
JJ