I am developing a very simple project to measure some temperatures and store them in an SD card. I have a problem trying to write. No matter what I do the SD card always ends up with content like this:
And in read only mode. I have formatted it in FAT16, FAT32, in Windows an Ubuntu and always the same. I have changed between two different Arduino UNO boards, two different SD modules connected like this:
and extremly simple codes like this example and I always get the same.
Detailed example of what I do and what I get
I have wrote this simple code to test:
#include <SD.h>
#define CHIP_SELECT_PIN 10
/* CIRCUITO ----------------
* SD card attached to SPI bus as follows:
* MOSI - pin 11
* MISO - pin 12
* CLK - pin 13
* CS - pin 10. */
File archivo;
char cadena[20];
char c;
void setup(void) {
Serial.begin(9600);
while (!Serial) {;} // Esperar a que se inicialice la comunicacion serial
while (Serial.read() >= 0) {;} // Limpiar el buffer
// INICIALIZAR LA MEMORIA ----------------------------------------
Serial.print("Initializing SD card... ");
pinMode(CHIP_SELECT_PIN, OUTPUT);
if (!SD.begin(CHIP_SELECT_PIN)) {
Serial.println("Unable to initialize. Remove the card.");
while (1);
}
Serial.println("OK!");
// ABRIR UN ARCHIVO PARA ESCRIBIR --------------------------------
if (SD.exists("ARCHIVO.TXT")) {
Serial.println("Deleting existing file with the same name. ");
SD.remove("ARCHIVO.TXT");
}
Serial.print("Creating new file... ");
if (!(archivo = SD.open("ARCHIVO.TXT", FILE_WRITE))) {
Serial.println("Unable to create the file. Remove the card.");
while (1);
}
Serial.println("File successfully created. ");
Serial.print("Send a string to store in the card... ");
while (Serial.available() == 0); // Nos quedamos esperando a que nos manden el numero
leerCadena('\n', cadena, 19);
Serial.print("The following string was received: \" ");
Serial.print(cadena);
Serial.println("\"");
archivo.print(cadena);
archivo.close();
Serial.println("The string was successfully stored in the card. ");
// Lectura de los datos guardados en la memoria ------------
Serial.print("Preparing to read data in card... ");
if (!(archivo = SD.open("ARCHIVO.TXT", FILE_READ))) {
Serial.println("Cannot open file to read. Remove the card. ");
while(1);
}
Serial.print("The string stored in memory is: \"");
while ((c = archivo.read()) >= 0) {
Serial.print(c);
}
Serial.println("\"");
archivo.close();
Serial.println("Remove the card.");
while(1);
}
void loop(void) {}
void leerCadena (char corte, char* cadena, byte n_bytes) {
byte n_leidos;
n_leidos = Serial.readBytesUntil(corte, cadena, n_bytes);
cadena[n_leidos] = '\0';
delay(100);
while (Serial.read() >= 0) {;}
}
and what I am doing and getting is:
- First I format my SD card in FAT32 using Gparted (the same happens if I format it in FAT16 or 32 both in Ubuntu and Windows).
- I test the SD card in my computer.
It works perfectly. I can remove it and plug it again and the data is there. Everything OK.
- I put the card in the SD slot connected to Arduino and run the above program. What I get through the serial monitor is
- Now I remove the card from Arduino and insert it into my computer. This is its content:
The file "readme.txt" contains the same text as before. The file "ARCHIVO.TXT" is empty. The binary files vary in size, e.g. the second one is 3 GB and fourth one is 774 MB.
- Now I try to modify the content in "readme.txt" and I get this:
I cannot delete files either.
- I remove the SD card from my computer and insert it again into Ardino. Reset the board and through the serial port I get:
- Now the content of the card seen in the computer is different:
and the file "ARCHIVO.TXT" is not present.
- If I put the card again in Arduino and reset the board everything repeats from step number 3. I mean, it creates the file "ARCHIVO.TXT" among with the other strange files, in the next time I put it into Arduino it deletes the file "ARCHIVO.txt", and so on.
If after all this I format the SD card in my computer, we are back in step number 1.
I am about giving up with Arduino and the SD module and go to a Raspberry. But it is much more expensive for this extremly simple task I need to do...