The Micro SD Card Module is a compact and convenient device that allows microcontrollers, such as the Arduino, to read from and write to Micro SD cards. These cards are widely used for data storage in portable devices due to their small size and high capacity. The module is commonly used in applications such as data logging, MP3 players, and portable data storage.
Pin Name | Description |
---|---|
CS | Chip Select, active low |
SCK | Serial Clock, connects to SPI clock |
MOSI | Master Out Slave In, SPI data input |
MISO | Master In Slave Out, SPI data output |
VCC | Supply voltage (3.3V to 5V) |
GND | Ground |
Connecting the Module:
Library Installation:
SD
library in the Arduino IDE by going to Sketch > Include Library > Manage Libraries...
and searching for "SD".Initialization:
Reading and Writing:
SD
library to read from and write to files on the SD card.#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Chip select pin for the SD card module
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect.
}
Serial.print("Initializing SD card...");
// Ensure that the chip select pin is set as an output
pinMode(chipSelect, OUTPUT);
// Check for the presence of the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Don't continue if the initialization fails
while (1);
}
Serial.println("Card initialized.");
}
void loop() {
// Open a new file to write to it
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it
if (dataFile) {
dataFile.println("Data log entry");
dataFile.close();
Serial.println("Data written to file");
} else {
// If the file isn't open, report an error
Serial.println("Error opening datalog.txt");
}
// Wait a bit before writing new data
delay(3000);
}
filename.txt
).flush()
method after writing to ensure data is properly written to the card.Q: Can I use a Micro SD card larger than 32GB? A: The module typically supports cards up to 32GB formatted with FAT16 or FAT32. Larger cards may require different formatting and are not guaranteed to work.
Q: How do I ensure my data is written to the card?
A: After writing data, use the file.flush()
or file.close()
methods to ensure all data is saved to the card.
Q: Can I use multiple SD card modules on the same Arduino? A: Yes, you can use multiple modules by assigning different chip select (CS) pins for each one. However, you can only communicate with one card at a time.
Q: What should I do if I get a 'Card failed' or 'Card not present' message? A: Check the card and connections. If the issue persists, try using another SD card to determine if the card is faulty.