0

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:

enter image description here

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:

enter image description here

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:

  1. 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).
  2. I test the SD card in my computer. enter image description here It works perfectly. I can remove it and plug it again and the data is there. Everything OK.
  3. I put the card in the SD slot connected to Arduino and run the above program. What I get through the serial monitor is enter image description here
  4. Now I remove the card from Arduino and insert it into my computer. This is its content: enter image description here 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.
  5. Now I try to modify the content in "readme.txt" and I get this: enter image description here I cannot delete files either.
  6. I remove the SD card from my computer and insert it again into Ardino. Reset the board and through the serial port I get: enter image description here
  7. Now the content of the card seen in the computer is different: enter image description here and the file "ARCHIVO.TXT" is not present.
  8. 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...

1

The problem was magically solved. I think the cables were the causing this issue. I was using cables of about 15 cm, now changed them to 5 cm and is working perfectly.

0

-Did you change the CS pin in the code to 10? -It can be a problem with the SD card, try another and tell us. -After the "dataFile.close();" put: "if(dataFile)Serial.println("not closed");" and watch the serial monitor window.

  • Thanks for your answers. I have tried almost everything. I have added more information to the question. I am waiting for a new SD card. After that I run out of ideas about what could be the problem... – user171780 Aug 19 '18 at 14:35
0

You have to guarantee that the file.txt is not opened before you shutdown the power or take the SD card off. I recomend you firstly, to do a "for" loop to stop in "X" iterations. If it works, you can put then a push-button in your project to stop any time.

0

I had the same problem (using a UNO with the MH-SD module directly connected to the arduino). The module apparently correctly initialised and I could list the files that were already there but when I attempted to create a single blank file ready for reading, a whole bunch of random named files were created many of which were enormous (according to my PC, although it said they all corrupted and couldn't open any of them). And then the card wasn't recognised when I tried using it again and had to be reformatted. I eventually found that one of the wires I was using, specifically the one to GND adjacent to MISO, was faulty. I suspect the length of the cables is irrelevant, I think that user171780 had a faulty cable in the same position.

0

I change voltage from 5V to 3.3V and move GND to the GND pin near the 3.3V. Now I can write and read a text file on a SD card.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.