Skip to content Skip to sidebar Skip to footer

How to access Micro SD Card Arduino With Catalex Module

Micro SD Arduino Tutorial

How to access Micro SD Card Arduino with Catalex Module  -  This time we will learn how to access the micro SD card adapter using Arduino Uno. There will be many benefits from this micro sd module, like the data logger.
 
Micro SD Adapter is a module used to read or write data in micro SD memory. To be able to communicate with Arduino, this module uses SPI (Serial Parallel Interface) communication which uses the SCK, MISO, MOSI and CS pins.
 
In this tutorial, I am using a Micro SD card Adapter module under the CATALEX brand. For it looks like this.
Cara Akses Modul Micro SD Card Adapter Arduino Catalex
 

Arduino Micro SD Card Module Schematic

Before we talking about the micro sd arduino program code, please connect the micro SD Module to Arduino. For communication using SPI. Look at the following picture:
 
Cara Akses Modul Micro SD Card Adapter Arduino Catalex
 

Wiring table for the micro sd arduino circuit above:

 Arduino Uno  MicroSD Module
 5V  VCC
 GND  GND
 13  SCK
 12  MISO
 11  MOSI
 4  CS
 
For the CS pin, in our circuit we use pin 4 of the Arduino. You can change from pin 4 to another pin on the Arduino.
 
After the circuit has been connected, insert a memory card into Micro SD module. 
 

1. Check Memory Card (read MicroSD information)

In this step we have to make sure that the micro SD that we are using can be read by Arduino. Therefore, we use the program line below.
 
Please upload it to your Arduino Uno:
/*
  created  28 Mar 2011
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>

Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = 4;

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


  Serial.println("\nMicroSD read at first time...\n");

  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("Micro SD failed to read!");
    Serial.println("* Is the wiring correct?");
    Serial.println("* Or the memory card is not inserted?");
    while (1);
  } else {
    Serial.println("MicroSD successfull to read.");
  }

  Serial.println();
  Serial.print("Memory 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");
  }

  
  if (!volume.init(card)) {
    Serial.println("Cannot find FAT16 / FAT32 partitions. \ n Make sure you have formatted the card");
    while (1);
  }

  Serial.print("Clusters         : ");
  Serial.println(volume.clusterCount());
  Serial.print("Blok x Cluster   : ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks     : ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Sistem file type :  FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // cluster is a collection of blocks
  volumesize *= volume.clusterCount();       // we're going to have multiple clusters
  volumesize /= 2;                           // SD card block is always 512 bytes (2 blocks of 1KB size)
  Serial.print("Storage size (Kb):  ");
  Serial.println(volumesize);
  Serial.print("Storage size (Mb):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Storage size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);

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

 
  root.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop(void) {
}
 
From this experiment I used 8GB (not in accordance with the image above). After I uploaded the program above, the display on the Arduino serial monitor:
 

 

 

2. Creating a File on the MicroSD

There are 2 ways to create files, manually and automatically using program code. Manually, we make it directly on the computer and save the file with .txt format in microSD. To create a file with the program I created the program below.
 
Attention!
a. For file names up to 8 characters only and add the file extension ".txt". For example "Data_Log.txt"
b. The file name is not Case Sensitive or does not recognize capital or lowercase letters. For example in microSD the file name is "DATA_LOG.TXT" while the file name in the program "data_log.txt", as it is not an error, can still be read and detected, as long as the name is the same.
 
Now we will try to create a file with "txt" format that can store text. With this format we can create a data logger. The basic commands are:
 
myFile = SD.open(namaFile, FILE_WRITE);
 
 
 
#include <SPI.h>
#include <SD.h>

File myFile;

String fileName;

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

  Serial.println("\nMicroSD read at first time...\n");

  if (!SD.begin(4)) {
    Serial.println("Micro SD failed to read!");
    Serial.println("* Is the wiring correct?");
    Serial.println("* Or the memory card is not inserted?");
    while (1);
  } else {
    Serial.println("MicroSD successfull to read.");
    Serial.println("to create a file, use the .txt format.");
    Serial.print("Write a file name: ");
  }

  while (Serial.available() == 0) {}

  fileName = Serial.readString();
  Serial.println(fileName);

  fileName.trim();

  // check the name you want to create in memory
  if (SD.exists(fileName)) {
    Serial.println("File " + fileName + " already in memory. Try a different name");
  } else {
    Serial.println("File " + fileName + " not in memory yet.");

    // Create a file as desired
    Serial.println("Create a file " + fileName + "...");
    myFile = SD.open(fileName, FILE_WRITE);
    myFile.close();
    Serial.println("Congratulations. Files " + fileName + " have been created");
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}
 
The results micro sd arduino is :

 

 
In the picture above, (a) is the result displayed on the Arduino serial monitor. Whereas (b) is the result of checking when a microSD memory card is inserted into the computer. This means that we have successfully created a file with Arduino.
 

3 Writing Data To MicroSD File

After the microSD has a file that has been created, the next step is to write data to that file. The basic commands are:
 
myFile.println("data");
 
For the program you can use the following:
 
#include <SPI.h>
#include <SD.h>

File myFile;


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

  Serial.println("\nMicroSD read at first time...\n");

  if (!SD.begin(4)) {
    Serial.println("Micro SD failed to read!");
    Serial.println("* Is the wiring correct?");
    Serial.println("* Or the memory card is not inserted?");
    while (1);
  } else {
    Serial.println("MicroSD successfull to read.");
  }


  
  if (SD.exists("logger.txt")) {
    myFile = SD.open("logger.txt", FILE_WRITE);
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("Write data is complete.");
  } else {
    Serial.println("error opening logger.txt");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
 
The results micro sd arduino is:

 

 
(a) is the information from the serial monitor when writing data to file in Micro SD card, while (b) is the result of writing the data. 
 
Then I opened the file using a text editor (black) and displayed the data that was written from Arduino.
 
 

4 Read Data from the Arduino MicroSD

If we want to read the data that was written to the file, the basic command is:
 
Serial.write(myFile.read());
 
The command is placed inside the loop to read all the data. The program reading data from files on the microSD using Arduino is as follows:
 
#include <SPI.h>
#include <SD.h>

File myFile;

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

  Serial.println("\nMicroSD read at first time...\n");

  if (!SD.begin(4)) {
    Serial.println("icro SD failed to read");
    Serial.println("* Is the wiring correct?");
    Serial.println("* Or the memory card is not inserted?");
    while (1);
  } else {
    Serial.println("MicroSD successfull to read.");
  }



  if (SD.exists("logger.txt")) {
    Serial.print("Data : ");
    myFile = SD.open("logger.txt");

    while (myFile.available()) {
      Serial.write(myFile.read());
    }

    myFile.close();
  } else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
 

The result is:

 

 

 

5 Delete Files from the Arduino MicroSD

If you feel like deleting files on the microSD, the command used is:
 
SD.remove("fileName");
 
The program is:
 
#include <SPI.h>
#include <SD.h>

File myFile;


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

  Serial.println("\nMicroSD read at first time...\n");

  if (!SD.begin(4)) {
    Serial.println("Micro SD failed to read!");
    Serial.println("* Is the wiring correct?");
    Serial.println("* Or the memory card is not inserted?");
    while (1);
  } else {
    Serial.println("MicroSD successfull to read.");
  }

  SD.remove("logger.txt");

  if (SD.exists("logger.txt")) {
    Serial.println("The file still exists");
  } else {
    Serial.println("The file has been deleted");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
 
Then results is:

 
Hopefully this micro sd arduino article can help all of you and hope it is useful.
 
 

Tags:

Post a Comment for "How to access Micro SD Card Arduino With Catalex Module"

Subscribe via Email