How To Interface A SD CARD To An Arduino Board
How To Interface A SD CARD To An Arduino Board
board.
Published by gg1 on July 12, 2012 | 1 Response
The ATMega 328 mounted on the Arduino UNO board has 512 bytes of EEPROM to store your data, without loosing them
when you poweroff the Arduino UNO board,
This amount of data could seem to small, but for a large number of projects this is quite enough. If you have to store, for
example, temperature data once a minute for 50 days you need to add some hardware.
If the Arduino system is working close to your PC, you can interface it using the serial line and then the PC will store data.
In some cases the Arduino system has to work far from other intelligent hardwares, for example imagine you want to
monitorize the temperature in a forest, you have to put a large number of Arduino boards on the trees, then when you want,
you have to take them back to retrieve info from them. In this case you can use a SD Card.
Bill Of Materials
To build a board to interface Arduino with a SDCard you need the following materials:
WIres
A SD card connector (i'm using one from an old and broken memory card reader)
We are going to use the resistors to build the resistive dividers, so we can use the 3,3 V starting from the 5V I/O of Arduino.
Building the Board
You have to wire the circuit has shown in the following pictures.
Here you are a simple scheme to realize the circuit over a stripboard.
Final Result
/*
SD card read/write
This example shows how to read and write data to and from 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 4
created
Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
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
}