SD Card Module With Arduino

About: Share Knowledge about Arduino, ESP,GSM,Basic Electronics, DIY PROJECTS, Raspberry pi

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.

Introducing the SD Card module

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 above (front and back view).

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: 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 above.

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

Step 2: 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 above

Note: depending on the module you’re using, the pins may be in a different order.

Step 3: 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.

Step 4: 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.

Step 5: Where to Buy?

The SD card module is a very cheap and you can find one for approximately $1 SD card module.

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions