Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |   Electronics Reference Design Library  |   AVR    |    PIC    |    8051    |    Electronic Projects

How to Interface SD Card with Arduino

Summary

Developed By: 
Ajish Alfred

The microcontroller systems may have to back up the data which they have read during their operation or the data which they need to access during their running time. Most of the microcontrollers have built-in EEPROM memory but they come in comparatively small sizes. In order to use the microcontroller in applications like file accessing, media player etc. an external memory card is necessary. The SD memory card comes in large storage capacities ranging from 1 GB to 8 GB and they are compatible with the SPI bus of the microcontroller. The large storage capacity and simplicity in interfacing results in wide usage of SD memory card by the microcontrollers. The files in an SD memory card are stored using FAT32 or FAT16 and one should use the code or FAT file-system library to access the files from an SD card.

The easy prototyping platform Arduino provides a library for accessing the SD memory cards. This particular project explains how to interface a SD card using an Arduino board and perform some read and write operations on it.

How to Interface SD Card with Arduino

Description

The memory card used in this particular project is a 2 GB SD card from Transcend but the code is supposed to work with SD card from all vendors. The SD card operates on 3.3V logic and hence to interface it with a microcontroller which runs on 5V logic one should use a Logic Level Converter. Since the memory card is interfaced using the SPI bus the four channel Logic Level Converter modules which are commonly available in the market can be used. The image of the Logic Level Converter module used in this project is shown in the following image;

Arduino

The SD memory card and the low voltage side of the Logic Level Converter should be provided with the 3.3V power supply and for that one can use any 3.3V regulator IC. A bi-color LED is suggested to connect across the 3.3V positive and MISO and MOSI lines of the SD card. The image of the memory card and the required circuitry that has been built for this particular project is shown in the following image. In the image one can see a potentiometer which is actually forms the circuit with an SMD variable regulator IC LM117 underneath it. It is recommended to use LM1117 which is a 3.3V regulator and which does not require other components to set the voltage as shown in the circuit diagram of this project.

Arduino

The Arduino pro-mini board has digital pins marked as 2, 3, 4 up to 13.  Among the digital pins four pins namely 10, 11, 12 and 13 can be configured as SS, MOSI, MISO and SCK. The MISO of the memory card should be connected to the pin number 11, the MOSI should be connected to the pin number 12 and the SCK should be connected to the pin number 13 of the Arduino pro-min. The SS of the SD card should be connected to the pin which is defined as the SS pin of the Arduino in the code written. The image of the Arduino pro-mini board and the Arduino IDE version 1.0.3 for windows used in this project are shown in the following image;

Arduino

 

Arduino

Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the PC through the USB port of the PC.

Arduino

It is assumed that the reader has gone through the project how to get started with the arduino and done all the things discussed in it. The Arduino IDE provides a library called <SD.h> which has lot of functions to access the SD memory card. The library is able to access the FAT16 or FAT32 filesystem of the SD card using the AVR microcontroller of the Arduino board so that the files can be read, modify or write. The functions used in this particular project are SD.begin(), SD.open(), file. println(), file.read(), and file.close().

SD.begin()

The SD.begin() function used to initialize memory card so that it can be accessed through the SPI bus. The function has an optional parameter which is the number of the pin used as the Slave Select (SS) for the SD card. If the parameter is not provided during the function call the hardware SS pin will be used as default.

The function returns a value 1 on successful initialization of the SD card or otherwise it returns a value 0.

SD.open()

The function is used to open a file from the SD card. The file can be opened in either read mode or write mode. If a file is tried to open in write mode which does not exist, then a new file with the same name will be created and opened for reading. If a file which does not exist in the memory card and is tried to open for reading the function will return an error. If opening the file is successful then the function will return a variable of type ‘File’ which can then be used for reading, writing or closing the file.

The function has two parameters, the first one is the File _name and the second one is the Mode. The details of the two parameters are discussed below;

{C}{C}{C}{C}-          {C}{C}{C}{C}File_name

This is the first parameter of the function SD.open(), and it should be provided with the name of the file to be open. If the file is in a directory then the path of the file should be provided.

{C}{C}{C}{C}-          {C}{C}{C}{C}Mode

The second parameter of the function SD.open() represents either of the two modes in which the file can be opened. The parameter can be provided with the value FILE_READ or FILE_WRITE.

FILE_READ: If this value is provided to the second parameter of the function SD.open(), then the file will be opened in reading mode. If the file does not exist the function will return a value 0 otherwise 1 always.

FILE_WRITE: If this value is provided to the second parameter of the function SD.open(), then the file will be opened in writing mode. If the file does not exist in the SD card a new file with the same name will be created and opened for reading. If the file with the name provided in the parameter is opened successfully thebn the functiom will return 1 otherwise 0.

For example if a file with the name “test.txt” need to be opened in writing mode then the following statement can be used.

myFile = SD.open("test.txt", FILE_WRITE);

The above satatement will open the file “test.txt” and the file descriptor is returned to the variable ‘myFile’.

file.print()

The function print() is used to write a data to the file opened by the function SD.open.  Suppose a file is opened by the SD.open () function using the following statement;

myFile = SD.open("test.txt", FILE_WRITE);

The file descriptor myFile can be passed as a parameter to the print() function so that it can write data into it. The following statement can be used to write a string “HELLO WORLD” in to a file named “test.txt” which has been opened for writing by the SD.open().

myFile.print("HELLO WORLD");

file.println()

The function println() writes  a data each and every time to a new line in an opened file of the SD card.

file.read()

The function file.read() can be used to read a byte from the file which has already opened by the function SD.open() for reading. It simply returns the next byte from the SD card or a non-positive number if there is no data to be read.

For example to read a byte from the file which has been opened by using the following statement;

myFile = SD.open("test.txt", FILE_WRITE);

The read() function can be used as shown below;

var = myFile.read();

Where ‘var’ is a character variable into which the value of the byte returned by the function ‘read ()’ can be stored.

file.close()

This function is used to close a file which has been opened by the function SD.open(). Once the function is called the file descriptor no longer exists and hence can be used to access the same file again.

For example to close a file which has been opened with the help of the following statement

myFile = SD.open("test.txt", FILE_WRITE);

The function close() can be used to close the file as shown below;

myFile.close();

The data is transferred from a file created in the SD card and is monitored using the Serial monitor window with the help of the serial communicating functions written in the code. The functions used in this projects are namely Serial.begin(), Serial.println() and Serial.write(). The details of these functions and similar functions for the serial communication are already discussed in previous projects on how to do serial communication with the Arduino, how to send and receive serial data using arduino, how to do serial debugging with the Arduino.

When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino. The code will initialize the SD card, create a file “text.txt” in it and store a string inside that file which is again read back and displays on the Serial monitor. One can observe the data read from the file with the help of Serial monitor as explained in the project on how to do serial debugging with the Arduino.

The function delay() which has already been discussed in previous projects on how to start with Arduino and how to use digital input and output of Arduino is used here to generate a considerable delay between two consecutive reads.

Circuit Diagram

Video

Code

This Code is only visible to Registered users. Please Login/Register

 

Components

Resistor Image
Resistor

Resistor is a passive component used to control current in a circuit. Its resistance is given by the ratio of voltage applied across its terminals to the current passing through it. Thus a particular value of resistor, for fixed voltage, limits the current through it. They are omnipresent in electronic circuits.

...
LEDs | Light Emitting Diode
LED
 
Light emitting diodes (LEDs) are semiconductor light sources. The light emitted...
Arduino Pro Mini
Arduino Pro Mini

The Arduino Pro Mini is an ATmega168 based microcontroller board. The board comes with built-in arduino bootloader. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 8 analog inputs, an on-board resonator, a reset button, and holes for mounting pin headers....

Browse 4m+ Electronic Products


 
Pick the right product for your next design project with "Mouser Store". Mouser Electronics brings broadest range of semiconductors and electronic components from over 500 industry leading suppliers. 
 
Mouser specializes in the rapid introduction of new products and technologies for design engineers and buyers that includes semiconductors, interconnects, passives, and electro-mechanical components. Providing the best possible service with the flexibility of not requiring a minimum order and same-day shipping, Mouser has it all.
 
 
 
 
When you need the right part right now, think of Mouser
 
Thank you
Team EG

 

Inexpensive, Simple, Easy-to-Use Microcontroller Boards By Texas Instruments

Starting at $9.99 Buy Now

Starting at $17.00 Buy Now

Starting at $12.99 Buy Now

Starting at $12.99 Buy Now

 

You are here