Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this tutorial, you are going to know in detail about Arduino SD card shield. The Arduino has very less amount of memory so if your project have audio or video or any kind of graphics, then you will require a SD card to store data into. This is where the Arduino SD card shield will help you.

The SD cards works on 3.3V but if you want o use them with Arduino for storing data then you will have to use a SD card shield. The SD card shield we have used is for the micro SD cards and it uses the FET’s for level shifting and also a 3.3V regulator which converts the 5V from Arduino into the 3.3V for micro SD card.

The Arduino SD card shield has a socket for the SD cards on the back side and I have tested memory cards up to 128 GB which works fine. It is recommended to format the memory card before usage.

Circuit Diagram and Explanation

Before connecting the SD card Shield with the Arduino, let’s have a look at the pin out of the SD card shield. The shield has six pins, VCC and GND for power and the other four pins for SPI communication. The other four pins are as follows

  • MISO (Master in Slave out)
  • MOSI (Master out slave in)
  • SCK (System Clock)
  • CS (Chip Select)

This SD card shield uses FET’s for level shifting and also have a voltage regulator which converts 5V in to 3.3V.

SD card shield pin out

SD card shield pin out

The communication between the Arduino and the SD card shield is done by using the SPI. The SPI is available on the pins 11, 12 and 13 for Arduino Uno and on the pins 50, 51 and 52 for the Arduino mega 2560. Another pin is used for the communication between the Arduino and the SD card shield which is the hardware SS pin (pin 10 on Arduino and pin 53 on Arduino mega 2560).

Connections of Arduino with Micro SD card shield are as follows

SD Card To Arduino Uno Arduino Mega 2560
CS Pin 10 Pin 53
SCK Pin 13 Pin 52
MISO Pin 12 Pin 50
MOSI Pin 11 Pin 51
VCC 5V 5V
GND GND GND
Arduino sd card shield interfaicng

Arduino sd card shield interfaicng

Code

You don’t have any need to download the library. The Arduino IDE have these libraries when it is installed.

#include <SD.h>
#include <SPI.h>
File sdcard_file;
int CS_pin = 10; 

void setup() {
 Serial.begin(9600); //Setting baudrate at 9600
 pinMode(CS_pin, OUTPUT);//declaring CS pin as output pin
 if (SD.begin())
 {
 Serial.println("SD card is initialized and it is ready to use");
 } else
 {
 Serial.println("SD card is not initialized");
 return;
 }
 
 sdcard_file = SD.open("data.txt", FILE_WRITE); //Looking for the data.txt in SD card
 
 if (sdcard_file) { //If the file is found
 Serial.println("Writing to file is under process");
 sdcard_file.println("This data is for test"); //Writing to file
 sdcard_file.close(); //Closing the file
 Serial.println("Done");
 }
 else {
 Serial.println("Failed to open the file");
 }
 sdcard_file = SD.open("data.txt");
 if (sdcard_file) {
 Serial.println("Reading from the file");
 while (sdcard_file.available()) {
 Serial.write(sdcard_file.read());
 }
 sdcard_file.close();
 }
 else {
 Serial.println("Failed to open the file");
 }
 }
void loop() {
//Nothing in the loop
 }

 Code Explanation

First of all, we have added the SD and SPI libraries and after that, we have created an object named ‘File’. Then we have initialized the ‘CS’ pin of the SPI bus which is the pin 10 for Arduino Uno and pin 53 for Arduino Mega.

After that, we have started the serial communication at 9600 baud rate and have declared the CS pin as the output pin.

The SD.begin() function will initialize the SD card and on successful initialization, it will show ‘SD card is ready to use’ on the serial monitor and otherwise, It will show ‘SD card initialization failed’ on the serial monitor.

The SD.open() function will open the file ‘test.txt’ if it already available and if it is not already available then it will create it. If the file is successfully opened, then it will show ‘Writing to file…’ and will print ‘Testing text 1, 2 ,3…’ in the file. After that, it will close the file and will show ‘Done’ on the serial monitor.

The SD.open() function will open the file and next we will show the data in the file on the serial monitor by using the Serial.write(myFile.read()) command.

For naming the file, you must use the 8.3 format. A file name using the 8.3 format will be like this ‘file123.TXT’, where ‘file123’ is the 8 character or fewer strings while the ‘.TXT’ is the 3 character extension. The extension name can also be ‘.log’ or ‘.EXT’. You cannot use the file names longer than 8.3 format.

When using the file.write() function, It does not save the file until you close the connection or use the function for saving data. The function used for saving data is flush() and for closing the data is the close(). It is a good practice to save the file after every reading.

If everything work fine, then the output will look like this

In this article, you learn about Arduino SD card interfacing. If you want to add a temperature sensor in to it and then show the readings in the form of a graph, then follow this article | Arduino SD Card Shield Temperature Logger

 

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar