In this post we’re going to show you how to use an SD card module with Arduino to read and write files on an SD card.

For an Arduino project with the SD card module read our blog post: Arduino temperature data logger with SD card.
Introducing the SD Card module
The SD card module is specially useful for projects that require data logging.
The Arduino can create a file in an SD card to write and save data using the SD library.
There are different models from different suppliers, but they all work in a similar way, using the SPI communication protocol. The module used in this tutorial is the one shown in figure below (front and back view).

This module works with micro SD card.

Where to buy?
The SD card module is a very cheap and you can find one for approximately $1 – check prices on Maker Advisor.
Pin wiring
The table below shows how you should wire the SD card module to your Arduino
| SD card module | Wiring to Arduino Uno | Wiring to Arduino Mega |
| VCC | 3.3V or 5V (check module’s datasheet) | 3.3V or 5V (check module’s datasheet) |
| CS | 4 | 53 |
| MOSI | 11 | 51 |
| CLK | 13 | 52 |
| MISO | 12 | 50 |
| GND | GND | GND |
Note: different Arduino boards have different SPI pins. If you’re using another Arduino board, check the Arduino official documentation.
Preparing the SD card
The first step when using the SD card module with Arduino is formatting the SD card as FAT16 or FAT32. Follow the instructions below.
1) To format the SD card, insert it in your computer. Go to My Computer and right click on the SD card. Select Format as shown in figure below.

2) A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.

Testing the SD card module
Insert the formatted SD card in the SD card module.
Connect the SD card module to the Arduino as shown in the circuit schematics below or check Pin Wiring in previous section.
Note: depending on the module you’re using, the pins may be in a different order.
Code – CardInfo
To make sure everything is wired correctly and the SD card is working properly, in the Arduino IDE window go to File> Examples > SD > CardInfo.
Upload the code to your Arduino board. Make sure you have the right board and COM port selected.
Open the Serial Monitor at a baud rate of 9600 and you should see your SD card information.
If everything is working properly you’ll see a similar message on the serial monitor.

Read and write to the SD card
The SD library provides useful functions for easily write in and read from the SD card.
To write and read from the SD card, first you need to include the SPI and SD libraries:
#include <SPI.h>
#include <SD.h>
You also have to initialize the SD card module at the Chip Select (CS) pin – in our case, pin 4.
SD.begin(4);
To open a new file in the SD card, you need to create a file object that refers to your data file. For example:
dataFile = SD.open("data.txt", FILE_WRITE);
The first parameter of this function is the name of the file, data.txt, and the FILE_WRITE ;argument enables you to read and write into the file.
This line of code creates a file called data.txt on your SD card. If the data.txt file already exists, Arduino will open the file instead of creating another one.
To write data to the currently open file, you use:
dataFile.write(data);
In which the dataFile is the file object created previously and the data is what you want to write in the file.
You can also use the print() or println() functions to print data into the file:
dataFile.print(data);
dataFile.println(data); // followed by a new line
To read the data saved on your file:
dataFile.read();
You can only write within a file at once, so you need to close a file before proceeding to the next one. To close the data.txt file we’ve just created:
SD.close("data.txt");
The argument of this function is the file you want to close, in this case data.txt.
For a complete sketch on how to read and write, in your Arduino IDE go to File> Examples > SD > ReadWrite.

Wrapping Up
This was just a quick introduction to the SD card module with the Arduino.
Make sure you check the following blog post for a data logging project example using the SD card module with Arduino:
In that project we save temperature readings on an SD card with time stamps using the DS18B20 temperature sensor and the RTC module.
If you like Arduino projects, make sure you check our latest Arduino course: Arduino Step-by-step Projects – Build 23 Projects
We hope you’ve found this guide useful.
Thanks for reading.





Thank you for sharing a very useful Project.
How easy or difficult ist it to save GPS data the same way ?
NMEA coming in via Serial saving data every 15 Seconds. Time can be taken from the NMEA string so no RTC is needed.
Hi Nils.
Thanks for your support.
We don’t have a particular project for saving GPS data on an SD card, but I think you can apply the concepts learned in this post to do that.
Then, let us know how you did.
Regards.
Good and useful Article…
Thanks 🙂
hey my sd cart module havent got a vcc pin. What should I do
Hi.
What pins does it have instead?
Regards,
Sara
I’ve managed to do this correctly. Now I have another answer, how do I erase the existing file and create a new one with the same name each time I reset the Arduino? Because each time I reset the Arduino, it will open the existing file again, and write on it, leaving every old information in the file. I don’t want this. I would want it to clean the file and start all over. Is this possible?
Hi Jose.
I haven’t tested that. But I think here’s the solution for your problem.
In the setup(), after checking the file you want to delete exists with SD.exists(“yourfilename”), delete the file from the card with SD.remove(“yourfilename”).
I hope this helps. Thanks.
Hello Sara, nice and accurate project. can you please email me?
We can’t answer technical questions via email, but feel free to post them as a comment.
Regard,
Rui
Tried integrating the SD module (using stock SD lib) with my ILS (using UCGLIB) display, but somehow they conflict in the common MISO line. Each module functions perfectly by its own, but once I wire them together (except for the SS line), the SD card is not detected, and the display turns blank.
I’m trying to get alternate libraries, just in case it is not HARDWARE related. Still no luck. 🙁
can we connect an SD card with 2 arduino, one is for writing and other one isfor reading data save in SD card?
Hi.
We’ve never tested that scenario.
Can we insert less than 8 GB SD card in this SD Card module?
Yes. I think so. 🙂
hi…
how are you
this project is very useful
Hello, nice project
I have a question
If you connect a 3.3V SD module to Arduino, data from SPI bus will be from 0 to 3.3 V. What about the 0 to 5 V for logic levels in Arduino UNO or Mega????
Thanks
Hi.
You can use a logic level converter module to convert 5V data to 3.3V data or the other way around: https://makeradvisor.com/tools/logic-level-converter-module/
Regards,
Sara
Hello Sarah,
I hope it’s ok with you, but I cited this article under your name in my senior project. Thanks for the tutorial,
Chase
Hi Chase.
Thank you for referring our work.
I hope it has helped you in your project.
Regards,
Sara
😀
Hi Sara,
I am attempting to read from one file while writing to another. SD.begin(4) succeeds, as does a subsequent SD.open(“filename.ext”, FILE_WRITE) and fileObject.close(). I’ve inserted a delay(2000) for buffers to flush then issue SD.open(“newname.txt”,FILE_READ) with a different file object, which fails. Any suggestions are appreciated.
Hi Joel
What is exactly the error that you’re getting?
While it appears that I can write and read using the Arduino, I fail to see the file and data when I put the card into my PC. I need to be able to create some data files using the PC and then have the Arduino read them. Isn’t this possible?
Yes.
It is possible. Just take a look at the functions that read files.
Regards,
Sara
Thanks for your reply Sara. I found the trouble not too long after I posted. It appears that somewhere in the past the SD card had been partitioned into two drives and when I stuck it into the PC I did not notice the first small one also coming up. I began to think that there was something different about formatting the SD cards so I loaded a free SD formatting app and it was while using that app that I saw the two drives. I formatted it into one drive and everything is now progressing. Thanks again
Great!
I’m glad you find out the problem.
Regards,
Sara
i am making a project that show data on 7 seg display which stored in SD card as day by day. when i make power ON to device then card initialization done & data shows correct as per date then after when i change the date SD card failed i.e. show ” error database.txt opening “.
How can i overcome with this issue.
” When debugging my SD Card I had a error that would put the card into a state that required the microSD card to be powered down and then powered up to work. It would work until the software error and then not work again until the power was cycled. “
data returned after the dataFile.read () function; what? I have a table of values saved as a .csv file, how can I manipulate the data by column and row?
this is great, thanks
Hia Sara,
Thank you very much for this, it is really useful. Can you tell me please, is it possible to reassign the pins used by the SD card reader to alternative SPI connections? I am using an Arduino Mega, and while the pins you have listed work as expected, it would be very helpful if I can use other pins for CS, MOSI CLK etc. Thank you.
kind regards – SteveS
This is great thing 🙂 i dont know what doin
Hi sara ,
I want to create a project such that a new file is created for each day like Sensor_data_110623 , then the next day it’s like Sensor_data_120623 . Is it possible ?
Hello,
On my 2560 board, I need to connect MOSI to pin 50 and MOSI to pin 51.
So, it works.
Is that correct ?
Sorry,
that was my mistake.
Hi,
In this project https://randomnerdtutorials.com/guide-to-sd-card-module-with-arduino/
you have placed a Note:
“Note: different Arduino boards have different SPI pins. If you’re using another Arduino board, check the Arduino official documentation.”
The link to the documentation you give as https://www.arduino.cc/en/Reference/SPI is no longer valid.
I found this one:
https://docs.arduino.cc/language-reference/en/functions/communication/SPI/
Maybe a correction for your project.
Best regards,
Kees
Hi.
Thanks for letting me know.
They probably changed the URL.
Regards,
Sara
Hello,
With Windows10 I have formatted an 32 Gb SDcard as FAT32 and checked by asking the properties after the format was complete. It was OK.
After executing example CardInfo with a SDcard module and an Arduino Nano the results in the monitor are as below:
08:26:31.781 ->
08:26:31.781 -> Initializing SD card…Wiring is correct and a card is present.
08:26:32.388 ->
08:26:32.388 -> Card type: SD2
08:26:32.994 -> Could not find FAT16/FAT32 partition.
08:26:32.994 -> Make sure you’ve formatted the card
Unfortunately, I can’t tell Cardinfo that this SD card is actually formatted as FAT32 🙂
Then I tried File> Examples > SD > ReadWrite with this result:
08:38:58.014 -> Initializing SD card…initialization failed!
If I looked closer to the way these two examples are initializing the SD card then I see a difference.
The line ” if (!card.init(SPI_HALF_SPEED, chipSelect)) {Serial.println(“initialization failed. Things to check:”); ” etc. in the Cardinfo example works OK
but the similar initializing line in the Read/Write example ” if (!SD.begin(4)) {Serial.println(“initialization failed!”); ” etc. then the initializing is failed.
(note: the variable “card” in the CardInfo example is decalerd as “SD2card card;”)
I have tried another formatted SDcard. Same result. And I have used both SDcards in another device without any issue.
Have you perhaps ever encountered this issue as well?
Not at the time of creating this example.
We’ll have to test it again.
Regards,
Sara
Hi Sara,
I have just managed to get an Arduino UNO. With this UNO, I did the tests SD-Cardinfo and SD-ReadWrite (with the same Sd Card reader). No issues at all now. So re-testing is not needed. For your information, there are also a couple of other SD-related examples in File-Examples. Also to format your SD card. They are in File-Examples-SdFat-SdFormatter and -SdInfo. Those examples require that you use pin 10 and not pin 4 for the chip select. To find and use these last examples, you need to have SdFat installed via the library (SdFat v 2.3.0 by Bill Greiman). I will investigate further what might be the issue using the SD card reader with my Arduino Nano (although the Nano looks OK).
Ok.
Thanks for letting me know.
Regards,
Sara