Example of a Micro SD Card Program with Arduino

Spread the love

Introduction

In the following article we will learn how to access a Micro SD Card using Arduino with a simple example program. From the example of the program, we can develop it according to the project that is or wants to be done, for example related to data logger tools and so on.

SD Card is a portable storage media that is currently almost found in various electronic equipment such as mobile phones, digital cameras, MP3 players etc. SD Cards are available in various capacities and speeds. Of course the capacity and speed is directly proportional to the price.

Micro SD Card Modul

SD Card uses a voltage range of 3.3V and hence the Micro SD Card module has a voltage regulator chip that changes the voltage from 5V to 3.3V so it is safe to use the 5V supply voltage.

Specification

  • Supply Voltage : 4.5V(min), 5V(typical), 5.5V(max)
  • Current : 0.2mA(min), 80mA(typical), 200mA(max)
  • Interface : a standard SPI interface.
  • Level conversion circuit board can interface : 5V or 3.3V
  • Card supported : Micro SD card (<=2G), Mirco SDHC card (<=32G)
  • Size : Approx 42 x 24 x 12mm
  • Weight : Approx 5g

Hardware Required

The materials used in this example program include :

  • Arduino UNO or others + Data cable
  • Micro SD Card Module
  • Some of jumper dupont cable M – F or F – F

Wiring

The following table shows the connection between Micro SD Card and Arduino.

Micro SD CardArduino
VCC5V
GNDGND
CSpin 10
MOSIpin 11
MISOpin 12
SCKpin 13

Code Program

In the example of this program, if we explain the points we can get or conclude as follows :

  • SD Card initialization
  • Check if there is a file “example.txt” on the SD Card, if there is then the file is deleted.
  • Create a new file “example.txt”
  • Writing data numbers 1 – 100 in that file.
  • Read contents of file “example.txt” that has been written or filled with data.
  • Display contents of that file to Serial Monitor.

/*
 SD card basic example
 This example shows how to create and destroy 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 10

 This example code is in the public domain.
 */ 

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

File myFile;

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(10)) {
     Serial.println("initialization failed!");
     while (1);
   }

   Serial.println("initialization done.");
   if (SD.exists("example.txt")) {
     Serial.println("example.txt exists.");
     // delete the file:
     Serial.println("Removing example.txt…");
     SD.remove("example.txt");
   } else {
     Serial.println("example.txt doesn't exist.");
   }

 // open a new file, write 1 - 100 and immediately close it:
   Serial.println("Creating example.txt…");
   myFile = SD.open("example.txt", FILE_WRITE);
   for (int i = 1; i < 100; i++) {
     myFile.println(String(i));
   }
   myFile.close();

   // open the file for reading:
   myFile = SD.open("example.txt");
   if (myFile) {
      Serial.println("example.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 example.txt"); 
    } 
}

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

After the above program is uploaded, the serial monitor can be observed the process steps and after completion, if we check in the SD Card with a PC or laptop, there should be a file “example.txt whose contents are 1 – 100.

Thus the tutorial and example of a simple Micro SD Card program with Arduino

++++++++ Hope This Useful ++++++++

Be the first to comment

Leave a Reply

Your email address will not be published.


*