Reading and writing files from SD card with an Arduino

details

Description:

This projects describes about using Arduino  system with SD Card to store and retrieve the information from the SD card.

 

wiznet

 

Hardware:

  • 1 x micro SD card
  • 1 x Ethernet shield module
  • 1 x Arduino Mega2560

To connect a micro SD card to our Arduino Mega, we will use an Ethernet shield with a micro SD slot on it. There are, however, many different shields available for other types of SD cards.

Pin   Name     Description

1     NC       not connected

2     CS       Chip Select/Slave Select (SS)

3     DI         Master Out/Slave In (MOSI)

4     VDD      Supply voltage

5     CLK      Clock (SCK)

6     VSS      Supply voltage ground

7     DO       Master In/Slave Out (MISO)

8     RSV      Reserved

If you were to try interfacing this SD card yourself, you would have to ensure that you connected the pins of the SD card to the appropriate pins of the Arduino. Since we are using a commercially-available shield, this is not an issue. All we need to do is to declare the default CS (chip select) pin of the Arduino as OUTPUT. This is pin 53 on our Arduino MEGA. On the Ethernet shield, the CS pin is pin number 4. You need to specify this in the code for the SD card to work properly.

Code1:

To read a file from the SD Card,we will use the SD.h library. This code assumes that the file “ourfile.txt” has already been written to the SD card.

#include 

const int cs = 4;

void setup()
{
  Serial.begin(9600);
  



  Serial.print("Initializing card...");
  // make sure that the default chip select pin is declared OUTPUT
  
  pinMode(53, OUTPUT);
  
  // see if the card is present
  if (!SD.begin(cs)) 
  {
    Serial.println("Card failed to initialize, or not present");
  
    return;
  }
  Serial.println("card initialized.");
  
  // open the file named ourfile.txt
  
  File myfile = SD.open("ourfile.txt");

  // if the file is available, read the file
  if (myfile) 
  {
    while (myfile.available())
    {
      Serial.write(myfile.read());
    }
    myfile.close();
  }  
  // if the file cannot be opened give error report
  else {
    Serial.println("error opening the text file");
  } 
}

void loop()
{
}

Code2:

We will create a file, write it, and then read it from SD card.

#include 

File myfile;

void setup()
{
 
  Serial.begin(9600);
  
  Serial.print("Initializing card...");
  
  // declare default CS pin as OUTPUT
   pinMode(53, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization of the SD card failed!");
    return;
  }
  Serial.println("initialization of the SDcard is done.");
  
 
  myfile = SD.open("textFile.txt", FILE_WRITE);
  
  
  if (myfile)
  {
    Serial.print("Writing to the text file...");
    myfile.println("Congratulations! You have successfully wrote on the text file.");
	
    myfile.close(); // close the file:
    Serial.println("done closing.");
  } else
  {
    // if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
  
  // re-open the text file for reading:
  myfile = SD.open("textFile.txt");
  if (myfile)
  {
    Serial.println("textFile.txt:");
    
    // read all the text written on the file
    while (myfile.available()) 
    {
    	Serial.write(myfile.read());
    }
    // close the file:
    myfile.close();
  } else 
  {
  	// if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
}

void loop()
{
}

Demo:

 

Author:Tim Youngblood

 

Tags:

201802,ArduinoMega 2560,Ethernet  shield module W5100,SD Card

 

Description:

This projects describes about using Arduino  system with SD Card to store and retrieve the information from the SD card.

 

wiznet

 

Hardware:

  • 1 x micro SD card
  • 1 x Ethernet shield module
  • 1 x Arduino Mega2560

To connect a micro SD card to our Arduino Mega, we will use an Ethernet shield with a micro SD slot on it. There are, however, many different shields available for other types of SD cards.

Pin   Name     Description

1     NC       not connected

2     CS       Chip Select/Slave Select (SS)

3     DI         Master Out/Slave In (MOSI)

4     VDD      Supply voltage

5     CLK      Clock (SCK)

6     VSS      Supply voltage ground

7     DO       Master In/Slave Out (MISO)

8     RSV      Reserved

If you were to try interfacing this SD card yourself, you would have to ensure that you connected the pins of the SD card to the appropriate pins of the Arduino. Since we are using a commercially-available shield, this is not an issue. All we need to do is to declare the default CS (chip select) pin of the Arduino as OUTPUT. This is pin 53 on our Arduino MEGA. On the Ethernet shield, the CS pin is pin number 4. You need to specify this in the code for the SD card to work properly.

Code1:

To read a file from the SD Card,we will use the SD.h library. This code assumes that the file “ourfile.txt” has already been written to the SD card.

#include 

const int cs = 4;

void setup()
{
  Serial.begin(9600);
  



  Serial.print("Initializing card...");
  // make sure that the default chip select pin is declared OUTPUT
  
  pinMode(53, OUTPUT);
  
  // see if the card is present
  if (!SD.begin(cs)) 
  {
    Serial.println("Card failed to initialize, or not present");
  
    return;
  }
  Serial.println("card initialized.");
  
  // open the file named ourfile.txt
  
  File myfile = SD.open("ourfile.txt");

  // if the file is available, read the file
  if (myfile) 
  {
    while (myfile.available())
    {
      Serial.write(myfile.read());
    }
    myfile.close();
  }  
  // if the file cannot be opened give error report
  else {
    Serial.println("error opening the text file");
  } 
}

void loop()
{
}

Code2:

We will create a file, write it, and then read it from SD card.

#include 

File myfile;

void setup()
{
 
  Serial.begin(9600);
  
  Serial.print("Initializing card...");
  
  // declare default CS pin as OUTPUT
   pinMode(53, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization of the SD card failed!");
    return;
  }
  Serial.println("initialization of the SDcard is done.");
  
 
  myfile = SD.open("textFile.txt", FILE_WRITE);
  
  
  if (myfile)
  {
    Serial.print("Writing to the text file...");
    myfile.println("Congratulations! You have successfully wrote on the text file.");
	
    myfile.close(); // close the file:
    Serial.println("done closing.");
  } else
  {
    // if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
  
  // re-open the text file for reading:
  myfile = SD.open("textFile.txt");
  if (myfile)
  {
    Serial.println("textFile.txt:");
    
    // read all the text written on the file
    while (myfile.available()) 
    {
    	Serial.write(myfile.read());
    }
    // close the file:
    myfile.close();
  } else 
  {
  	// if the file didn't open, report an error:
    Serial.println("error opening the text file!");
  }
}

void loop()
{
}

Demo:

 

Author:Tim Youngblood

 

Tags:

201802,ArduinoMega 2560,Ethernet  shield module W5100,SD Card

 

COMMENTS

Please Login to comment
  Subscribe  
Notify of