Arduino LC Studio SD Card Tutorial

Arduino Low Cost SD Card

LC Studion SD CardThe LC Studio SD Card module allows you to add data storage capability to your Arduino for about three bucks.   Its been around a while so there are plenty of internet resources.

This tutorial, in addition to providing module specific interface information,  will march you through SD card basics that occasionally get lost in project specific articles.

Here you will learn how to initialize, write to and read from your SD Card.

Getting the LC Studio SD Card

eBay     Amazon     Bang Good     Deal Extreme

LC Studio SD Card Pin Outs

LC Studio SD Card Pin OutsArduino LC Studio SD Card Tutorial

This particular tutorial will involve a single hook up. You will upload different sketches that demonstrate some of the different things you can do with the SD Card.

Format Your SD Card

You will need an SD Card.  Mine is 8 GB.   Format that SD Card using Windows.  I formatted mine as a FAT32 volume.

Connect the Arduino SD Card Module to your Arduino

The good news here is that you’re only going to require one set up to play with the various functions.

Arduino LC Studio SD Card Tutorial Hook Up

Initializing your SD Card

Before you can do any cool stuff with your SD Card you will need to:

  • Have an SD Card in your module.
  • Establish an instance of the card in your code.
  • And tell it begin

Copy, Paste, and Upload the Initializing Code

//Henry's Bench
// LC Studio SD Card Initializing Tutorial
//Connections:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10

 
#include <SD.h>
#include <SPI.h>
 
int cs = 10; // Set Chip Select to pin ten
 
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    
  }
  Serial.println("Initializing SD card...");
  Serial.println();
  
  pinMode(cs, OUTPUT);
  
  // Documentation says you're supposed to do this
  // even if you don't use it:
  pinMode(SS, OUTPUT);
   
  // see if the card is present and can be initialized:
  if (!SD.begin(cs)) {
    Serial.println("SD did not initiliaze");   
    while (1) ;
  }
  Serial.println("SD initialized.");
}
 

void loop()
{

}

Test the Initializing Sample Sketch

Open your serial monitor.  If the SD card is inserted, it should initialize.

If it does initialize, try removing your removing your SD card and notice that it will fail to initialize.

Creating an SD Card File and Writing to It

In this next sample sketch,  you’re going to create a file if it doesn’t already exists and write your first line of text to it.

If it does exist,  you will open it and add another line.

Notice the flush() command.  It saves your file.

Copy, Paste and Upload the Creating and Writing Sketch

 

//Henry's Bench
// LC Studio SD Card Create and Write to File Tutorial
//Connections:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10

 
#include <SD.h>
#include <SPI.h>
 
int cs = 10; // Set Chip Select to pin ten

File myFile;  // a File Object
 
void setup()
{
  //
  char myFileName[] = "MyFile.txt";  // The name of the file we will create
  
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    
  }
  Serial.println("Initializing SD card...");
  Serial.println();
  
  pinMode(cs, OUTPUT);
  
  // Documentation says you're supposed to do this
  // even if you don't use it:
  pinMode(SS, OUTPUT);
   
  // see if the card is present and can be initialized:
  if (!SD.begin(cs)) {
    Serial.println("SD did not initiliaze");   
    while (1) ;
  }
  Serial.println("SD initialized.");
 
 
 // Lets check to make sure that the SD card doesn't already have our file
  if (! SD.exists(myFileName)){
     // This next statement will open a file for writing if it exists
     // If it does not exist, it will create that file. That's what we're doing here.
     myFile = SD.open(myFileName, FILE_WRITE); 
     // This next statement checks to see if the file
     myFile.println("My 1st Line of Data");  // Send Your First Line to that file
     myFile.flush();  // Save it.
  }
  else{
    // We got here because the file already exists.
    // Therefore we're simple opening the file and writing to it. We will add another line at the end.   
    myFile = SD.open(myFileName, FILE_WRITE); 
    myFile.println("Another Line of Data");  // Send Your First Line to that file
    myFile.flush();
    
  }
  
  Serial.println("Done Writing");
  
}

void loop()
{

}

Test your Creating and Writing to SD Card Sketch

Walk through these steps carefully.

  1.   Start with a clear SD File
  2.  Open your serial monitor
  3.  When it indicates that it is done,  close your serial monitor
  4.  Remove your SD Card and insert into your computer
  5.  Navigate to the SD card and find “MyFile.txt”
  6. Open the text file and see that it contains “My 1st Line of Data”
  7. Remove the SD Card from your computer and reinsert into your LC Studio SD Card Module
  8. Open your Serial Monitor
  9. When it indicates that it is done,  close your serial monitor
  10. Remove your SD Card and insert into your computer
  11. Navigate to the SD Card and find “Myfile.txt”:
  12. Open the text file and see that it now also contains “Another Line of Data”

Reading an SD Card File Sample Sketch

In this example, we’re going to use the file we just created and read the information that we put on it.

Copy, Paste and Upload the Reading an SD Card Sample Sketch

//Henry's Bench
// LC Studio SD Card Read From File Tutorial
//Connections:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10

 
#include <SD.h>
#include <SPI.h>
 
int cs = 10; // Set Chip Select to pin ten

File myFile;  // a File Object
 
void setup()
{
  //
  char myFileName[] = "MyFile.txt";  // The name of the file we will create
  String LineString = "";  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    
  }
  Serial.println("Initializing SD card...");
  Serial.println();
  
  pinMode(cs, OUTPUT);
  
  // Documentation says you're supposed to do this
  // even if you don't use it:
  pinMode(SS, OUTPUT);
   
  // see if the card is present and can be initialized:
  if (!SD.begin(cs)) {
    Serial.println("SD did not initiliaze");   
    while (1) ;
  }
  Serial.println("SD initialized.");
  Serial.println();
  Serial.println("Reading MyFile.txt...");
  Serial.println();
 
  // Open our File for Reading
  myFile = SD.open(myFileName, FILE_READ);
  
  // Keep Reading String until there are no more
  while (myFile.available() != 0) {	
    // read the string until we have a newline
    // Careful on using this where you don't have newlines.

    LineString = myFile.readStringUntil('\n');
    Serial.println(LineString);				
  }	
 myFile.close();
 
 Serial.println();
 Serial.println("Done");  
} 
  
void loop()
{

}

Test the Reading from File Sketch

Open your serial monitor.  If successful,  you will see what you wrote to the file.