Introduction :
Arduino offers the possibility to read from and
write to SD cards using the Sd library. However, because the Arduino board
doesn’t have a built-in SD card input you have to connect a shield that have an
SD card reader into your board. After that, you will be able to use it for a
multitude of applications such as reading sensor data values and storing them
into the SD card.
In this tutorial, we will see how to connect the
SD card shield to the microcontroller, write some basic commands of the SD
library and finally write a program creating a file, writing data into it and
reading it after.
Project parts :
- Arduino Uno.
- SD card shield.
SD card shield pinout :
First,
you need to connect the shield to the Arduino board. On every pin of the
shield, there is a label. S, you have to connect avery pin of the shield (+5v,
GND, SCK, MOSI, MISO) to its corresponding pin on the Arduino card, according
to the documentation of Arduino Uno :
- MOSI è Pin 11
- MISO è Pin 12
- SCK è Pin 13
- SS è Pin 10
Then, connect the Cs pin on any of the remaining
digital I/O pins of your Arduino, in this tutorial, I have connected it to pin
5.
You also have to format the SD card to FAT16 or
FAT32 from your computer.
Basic Codes :
As always, you have to include the library
#include <SD.h>
To create a file you have to create a variable
of type File.
File file;
Then, in the setup function, you have to write a
code to start the SD card. The following sequence of code enables to determine
if there is a connection error via the serial bus.
If(!SD.begin(5)) {
Serial.println(“Starting
SD card error!”);
}
Serial.println(“SD card ready.”);
Using the File variable that we have created, we
can open the file in read or write mode.
file = SD.open(“myFile.txt”,
FILE_WRITE);//file on writing mode
file = SD.open(“myFile.txt”,
FILE_READ);//file on read mode
We usually have to check if the file has been
opened, for that we use the following condition :
if (file) {
Serial.print(“File
opened successfully”);
}else {
Serial.println(“error
while opening the file”);
}
We can then write to or read from the created file
depending on the defined mode.
file.println(“Welcome to
simple-arduino tutorial !”);
file.close();//always remember to
close the file after
In read mode, we create a loop that reads the
file until its end.
While(file.available()) {
char
c = file.read();
Serial.write(c);//send
the character to serial monitor
}
Total Code:
The following code uses all functions that we
have seen above. It writes to a file, then sends the data over serial bus to
the screen.
#include <SD.write>
File file;
void setup() {
Serial.begin(9600);
Serial.print(“Initializing
SD card”);
pinMode(10,
OUTPUT);
if (!SD.begin(5)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization
done.");
file = 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()
{
// all programming
happens in setup mode
}