Use SD-card as external memory/EEPROM?

Heyy,

For a project I am trying to use a SD-card as a external memory. I am still new at this stuff. Sorry if there is already a topic about this. I want to make something like a EEPROM. That you can send data to it and use that data the next time. The reason why i dont use the EEPROM for that is that you only can read/write 100000 times.

Does someone know of it is possible to use a SD-card that way and how?

Thanks!! Yvette

This is what i af till know:

#include <SPI.h> //bieb voor SD
#include <SD.h> //bieb voor SD

File myFile; //

#define button 2

const int chipSelect = 53; //waar de chip heen schrijft. bij een arduino uno is dit pin 10, MEGA is dit pin53

int val = 0;
int currentState = 0;
int previousState = 0;
long counter = 0;
long countertest = 0;
String dataString = "";

void setup () {
pinMode(button, INPUT);
Serial.begin(9600);

pinMode(53, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
myFile = SD.open("datalog.txt");
if (myFile) {
//Serial.println("datalog.txt:");

while (myFile.available()) {
Serial.write(myFile.read());
countertest = myFile;
}
// close the file:
myFile.close();
} else {
Serial.println("error opening test.txt");
}
Serial.print("\n");
Serial.println(countertest);
}

void loop () {
counter = countertest;
val = digitalRead(button);
if (val == HIGH) {
currentState = 1;
}
else {
currentState = 0;
}
if (currentState != previousState) {
if (currentState == 1) {
counter = counter + 1;
Serial.print("real counter: ");
Serial.println(counter);

myFile = SD.open("datalog.txt", FILE_WRITE);
if (myFile) {
myFile.println(counter);
myFile.close();
// print to the serial port too:
}
else {
Serial.println("error opening datalog.txt");
}
}
previousState = currentState;
delay(50);
}
}

You can use SD card to store data. But it survives much less rewrotes than an EEPROM. But it is so large should take you ages to fill it. There should be a controller inside SD card that manages wear leveling.
Also SD card can be written only in LARGE block (compared to Arduino). If you write single bytes you will ruin it quickly.

The reason why i dont use the EEPROM for that is that you only can read/write 100000 times.

You are reading and writing a long which is 4 bytes.
The internal EEPROM can hold 256 such values. If you move the stored value around to different addresses, you can get 256*100000 writes. Read does not wear out the eeprom.

The EEPROM.h library supports reading writing of these longs with very simple commands. It also has an update function which can ensure that it doesn't write an unchanged value. I believe that EEPROM.update will treat each byte of the long separately.

What data are you trying to store, and why do you think you will wear out the eeprom.

cattledog:
What data are you trying to store, and why do you think you will wear out the eeprom.

The idea is that everytime you push the button, you store something. But the systeem is a coppel of weeks on. But when only write wear the EEPROM out... :slight_smile:
I think i will go voor a external EEPROM. because than you dont need to replace the total arduino.

But to continue to learn from the thinks i do. how do you read a value from a SD and use this as a 'int'. for example: you have the value 15 on the SD. you pic that 15 and than your counter start from 15.