Overview of Micro SD Card
Secure Digital (SD) is a non-volatile memory card format created by the SD Card Association (SDA) for use in portable devices like mobile phones and cameras. The SD format includes four card types:
- SDSC (Standard Capacity SD): Offers up to 2GB of storage and uses FAT-12 and FAT-16 file systems.
- SDHC (High Capacity SD): Provides storage from 2GB to 32GB, using the FAT-32 file system.
- SDXC (Extended Capacity SD): Ranges in capacity from 32GB to 2TB and uses the exFAT file system.
- SDIO (SD Input/Output): Combines input/output functions with data storage.
SD cards are available in various sizes:
- Standard SD: 1.25 x 0.95 inches
- Mini SD: 0.87 x 0.79 inches
- Micro SD: 0.43 x 0.59 inches
In this interface, we will use the micro SD card.
File System
An SD card is a block-addressable storage device, allowing the host device to read or write fixed-size blocks by referencing specific block numbers. Most SD cards come preformatted with one or more MBR (Master Boot Record) partitions, where the first partition typically contains the file system, enabling the card to function similarly to a hard drive.
- For SDSC Cards:
- Capacity under 16 MB: FAT12
- Capacity between 16 MB and 32 MB: FAT16
- Capacity over 32 MB: FAT16B
2. For SDHC Cards:
- Capacity up to 7.8 GB: FAT32
- Capacity over 7.8 GB: FAT32
3. For SDXC Cards:
- exFAT
Notes
- The Arduino library supports only FAT16 and FAT32 file systems. Ensure that your SD card is formatted in one of these file systems; otherwise, an initialization error may occur.
- To format your SD card, you can download the SD Formatter tool from the following link https://www.sdcard.org/downloads/formatter_4/
Micro SD Card Module
SD Card Module
The micro SD Card Module provides an easy solution for transferring data to and from a standard SD card. It features an SPI interface compatible with any SD card and supports both 5V and 3.3V power supplies, making it suitable for use with Arduino UNO and Mega boards.
MicroSD Pinout
Pin Name | Description |
CS | Chip Select |
SCK | Clock |
MISO | Master In Slave Out |
MOSI | Master Out Slave In |
Connection Diagram of MicroSD with Arduino
MicroSD Card Interfacing with Arduino Uno
The diagram illustrates the detailed connections between the SD module and the Arduino:
- MOSI to pin 11
- MISO to pin 12
- CLK to pin 13
- CS to pin 4
Note: The VCC of the module can be connected to either 5V or 3.3V.
Read and Write on SD Card using Arduino
In this guide, we’ll interface a micro SD card with an Arduino and perform read and write operations.
We’ll use a built-in example code available in the Arduino IDE, which includes two essential library files:
- SPI.h
- SD.h
These libraries contain function definitions that enable read and write operations on the SD card.
To access this example code in the Arduino IDE:
- Open the IDE
- Go to File > Examples > SD > ReadWrite
For convenience, we have provided the same code here as well.
Micro SD Card Read and Write Code using Arduino
#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(4)) {
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
}