Site icon AranaCorp

Read and write on a SD card with Arduino

One of the weak spot of the Arduino is the memory space. When playing with data (saving measurement, reading or writing to a file, etc.), it is necessary to have lot of space available. You can easily add memory to the Arduino with a Secure Digital card or SD card.

Material

SD Card module overview

Whatever the module you select. The idea is the same :  A slot for the SD card and some pins to interface the microcontroller with the SD card.

 

The SD card module has 6 pins to establish connection: 2 for power and 4 to establish a SPI connection (Serial Peripheral Interface).

In case of a shield the wiring is predefined. Be sure to check the datasheet and to modify the code accordingly.

 

Wiring the SD card module

Code to get info on SD card

To interact with your SD card module or shield, there is a Arduino library : SD.h. Examples are available on the Arduino IDE.

// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
// VELLEMAN I/O VMA304: SD shield: pin 10 
const int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}


Code to read or write a file on the SD card

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

File myFile;
const int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}


Caution: If your files are not recognized, try to use a short name for the file saved on the SD card (max 8 characters). Indeed, the number of characters for a file on an SD card is limited with Arduino.

Test and mix those examples to get the functionality that you want. If you have any issue interacting with your SD card module. Do not hesitate to let a comment or to send us a message.

Bonus: Display the files stored on the SD card.

To check that the files are saved on the SD card, you can view the files stored at the root. To do this, simply use the File object to browse the SD card’s directories.

//Libraries
#include <SD.h>//https://www.arduino.cc/en/reference/SD 
#include <SPI.h>//https://www.arduino.cc/en/Reference/SPI 

//Constants
#define SD_ChipSelectPin 4

//Objects
File root;

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init sd shield
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println(F("SD fail"));
    return;
  }
  printFilenames();
}

void loop() {

}

void printFilenames(void ) { /* function printFilenames */
  ////find files in SD card
  root = SD.open("/");
  while (true) {
    File entry =  root.openNextFile();
    if (! entry) {
      break;// no more files
    }
    Serial.println(entry.name());
    entry.close();
  }
}




Application

Source

Find other examples and tutorials in our Automatic code generator
Code Architect

Exit mobile version