Interfacing Micro SD Card Module With Arduino
Interfacing Micro SD Card Module With Arduino
Every once in a while you come across an idea, where you need
for a way to store large amount of log data and other information
for your Arduino project, for example a GPS logger.
Hardware Overview
The micro SD card module contains two main components that
make it undoubtedly easy to add data logging to your next
Arduino project:
VCC pin supplies power for the module and should be connected
to 5V pin on the Arduino.
GND should be connected to the ground of Arduino.
MISO (Master In Slave Out) is SPI output from the Micro SD Card
Module.
MOSI (Master Out Slave In) is SPI input to the Micro SD Card
Module.
SCK (Serial Clock) pin accepts clock pulses which synchronize
data transmission generated by Arduino.
SS (Slave Select) pin is used by Arduino(Master) to enable and
disable specific devices on SPI bus.
As micro SD cards require a lot of data transfer, they will give the
best performance when connected up to the hardware SPI pins
on a microcontroller. The hardware SPI pins are much faster than
‘bit-banging’ the interface code using another set of pins.
Note that each Arduino Board has different SPI pins which should
be connected accordingly. For Arduino boards such as the
UNO/Nano those pins are digital 13 (SCK), 12 (MISO) and 11
(MOSI). You will also need a fourth pin for the ‘chip/slave select’
(SS) line. Usually this is pin 10 but you can actually use any pin
you like.
If you have a Mega, the pins are different! You’ll want to use
digital 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). Refer below
table for quick understanding.
Arduino Uno 11 12 13 10
Arduino Nano 11 12 13 10
Arduino Mega 51 50 52 53
W
iring Micro SD Card Module to Arduino UNO
OK, now insert the SD card into the module and upload the
sketch.
If you have a bad card, which seems to happen more with clone
versions, you might see:
The card mostly responded, but the data is all bad. See there is
no Manufacturer ID / OEM ID and the Product ID is ‘N/A’. This
shows that the card returned some SD errors. It’s basically a bad
scene. If you get something like this, you can try to reformat it or
if it still flakes out, you should toss the card.
Finally, try taking out the SD card and running the sketch again,
you’ll get the following,
See, it couldn’t even initialize the SD card. This can also happen
if there’s a wiring error or if the card is permanently damaged.
#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 Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin()) {
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");
}
void loop()
{
// nothing happens after setup
}
Code Explanation:
The sketch starts with including the built in SD library and
the SPI library which allows us to easily communicate with the
SD card over SPI interface.
#include <SPI.h>
#include <SD.h>
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Next, the SD.open() function will open the file named “test.txt”. In
our case, as such file is not present, it’ll be created. The other
parameter FILE_WRITE opens the file in read-write mode.
Now let’s read the same file to check if the write operation was
successful.To do that, we will use the same function, SD.open() ,
but this time as the file “test.txt” has already been created, the
function will just open the file. Then using
the myFile.read() function we will read from the file and print it on
the serial monitor. The read() function actually reads just one
character at a time, so therefore we need to use the “while” loop
and the function myFile.available() to read all characters in file. At
the end we need to close the file.
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
You must close() the file(s) when you’re done to make sure all
the data is written permanently! This reduces the amount of RAM
used.
Also keep in mind that file names do not have ‘case’ sensitivity,
so datalog.txt is the same file as DataLog.Txt is the same file as
DATALOG.TXT
You can seek() on a file. This will move the reading/writing cursor
to a new location. For example seek(0) will take you to the
beginning of the file, which can be very handy!
Likewise you can call position() which will tell you where you are
in the file.
If you want to know the size of a file, call size() to get the
number of bytes in the file.
Once you have a directory, you can start going through all the
files in the directory by calling openNextFile()
You may end up with needing to know the name of a file, say if
you called openNextFile() on a directory. In this case,
call name() which will return a pointer to the 8.3-formatted
character array you can directly Serial.print() if you want.