microSD Card module fail to save data after some hours

I am working on a project where I am trying to create six data loggers using an Arduino Uno, an RFID MFRC522 module, an RTC module, and a microSD card module. My goal is to save the time and the name (UID) of the triggered RFID tag onto an SD card. The data loggers work perfectly for a few hours, but after 6-7 hours of inactivity (no triggered tags), four out of the six data loggers fail to save the data onto the SD card. The remaining two data loggers continue to work flawlessly. I suspect that the issue could be related to the power supply or possibly something in the code.


This is my code: 

#include <SPI.h>
#include <MFRC522.h>
#include <virtuabotixRTC.h>
#include <SD.h>
#include <string.h>
#pragma execution_character_set("utf-8")

//MODULE REAL-TIME-CLOCK 
virtuabotixRTC myRTC(5, 7, 8);

#define SS_PIN 10
#define RST_PIN 9
#define Led_pin 6
#define Led_pinRED 2

//PINS RFID
MFRC522 mfrc522(SS_PIN, RST_PIN);
int buzzerPin = 3;

// //SELECT PIN SD MODULE
const int chipSelect = 4;




void setup() {
  pinMode(Led_pin, OUTPUT);
  pinMode(Led_pinRED, OUTPUT);
 pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);

  SPI.begin();
 
 //myRTC.setDS1302Time(30, 36, 11, 4, 24, 5, 2023);
  mfrc522.PCD_Init();
 
  while (!Serial) {
    ;
  }

  while (!SD.begin(chipSelect)) {
    // Serial.println("Initialization failed!");
    delay(1000);
    tone(buzzerPin, 100, 600);
    digitalWrite(Led_pinRED, HIGH);
    delay(1000);
    digitalWrite(Led_pinRED, LOW);
  }
  Serial.println("Initialization done.");
  digitalWrite(Led_pinRED, LOW);
  tone(buzzerPin, 500, 600);
  digitalWrite(Led_pin, HIGH);
  delay(1000);
  digitalWrite(Led_pin, LOW);
}




void loop() {




  myRTC.updateTime();

  //READ RFID ID 
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  } else {
    if (mfrc522.PICC_ReadCardSerial()) {
      Serial.print("UID tag :");
      String content = "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++) {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase();
      tone(buzzerPin, 3000, 600);
      digitalWrite(Led_pin, HIGH);
      delay(1000);
      digitalWrite(Led_pin, LOW);

      File uidsfile = SD.open("uids.txt");
      if (!uidsfile) {
        Serial.println("Failed to open uids.txt file!");
        return;
      }
      uidsfile.seek(0);  // Start from the beginning of the file
      int lineNum = 1;
      String line;
      //  line.trim();
      while (uidsfile.available()) {
        line = uidsfile.readStringUntil('\n');
        line.trim();

        line.toUpperCase();
        line.replace(" ", "");
        content.toUpperCase();
        content.replace(" ", "");
        // Serial.println(line);
        // Serial.println(content);

        if (line == content) {
          Serial.println("UID found at line: " + String(lineNum));
          break;
        }

        lineNum++;
      }
      uidsfile.close();
      int currentLine = 1;

      File namefile = SD.open("names.txt");
      while (namefile.available()) {
        String line = namefile.readStringUntil('\n');
        if (currentLine == lineNum) {
          line.trim();  // Optional: Remove leading and trailing whitespaces
          Serial.println(line);
          File dataFile = SD.open("data.txt", FILE_WRITE);
          dataFile.print(line);
          dataFile.close();

          break;  // Exit the loop after printing the target line
        }
        currentLine++;
      }
      namefile.close();
      


      File dataFile = SD.open("data.txt", FILE_WRITE);
      if(currentLine == 1) 
      {
        dataFile.print("  ");
      }
       if(currentLine == 2 || currentLine == 4 ) 
      {
        dataFile.print("   ");
      }

      dataFile.print("      ");

      if (myRTC.dayofmonth < 10)
        dataFile.print("0");
      dataFile.print(myRTC.dayofmonth);
      // data file
      dataFile.print("/");
      if (myRTC.month < 10)
        dataFile.print("0");
      dataFile.print(myRTC.month);
      dataFile.print("/");
      dataFile.print(myRTC.year);
      dataFile.print("  ");
      if (myRTC.hours < 10)
        dataFile.print("0");
      dataFile.print(myRTC.hours);
      dataFile.print(":");
      if (myRTC.minutes < 10)
        dataFile.print("0");
      dataFile.println(myRTC.minutes);
      // dataFile.print(":");
      // dataFile.println(myRTC.seconds);
      dataFile.close();

both the MFRC & the SD card use the SPI bus, you will have to altenately enable/disable them to make sure that not both of them are enabled at the same time.

is it some code missing or only brackets ?

i put 330 ohm resistor in miso pin of sd card . Can you tell me what i have to do more please? Is the code right ?

I cant understand why the 2 of 6 data loggers working perfectly and the others stop saves data after some hours..

Both String and SD support do dynamic memory allocation. I suspect that you are eventually overwriting the limited UNO memory. Symptoms will vary depending on what memory is affected.

1 Like

It sounds like you might be on the correct track. Assuming you have validated the power try swapping the good SD cards with bad and see if the problem moves or stays the same. You can also place a fan on them and see if that helps? It could be a thermal issue as well.

1 Like

yes i will try it thank you very much

Hmm, yes, but if it had been like this, none of the six would have worked. Two of them working perfectly. :confused:

Not sure if this is your issue but all the micro SD cards I've seen do not tristate the MISO pin. What board do you have?

1 Like

There are 6 cards and each one is different and in a different location, probably a different voltage, probably at a different temperature ...etc. If they were exactly the same they would work exactly the same. Something is causing problems over time. You could be causing it yourself with how you access them and have them wired into the system. Posting an annotated schematic as how you have wired it would help a lot and note any wire over 10" in length as well as all power sources etc. Something as simple as bus contention could be causing the problem and one of the parts is getting a junction to hot, may not be measurable outside the package.

1 Like

i would make sure that before any function call to either object, the other device is disabled, normally i would leave the mfrc enabled most of the time.
so in setup()

  }
  digitalWrite(SS_PIN, HIGH);  // disable mfrc
  while (!SD.begin(chipSelect)) {  // this call includes digitalWrite(chipSelect, LOW);
    // Serial.println("Initialization failed!");
    delay(1000);
    tone(buzzerPin, 100, 600);
    digitalWrite(Led_pinRED, HIGH);
    delay(1000);
    digitalWrite(Led_pinRED, LOW);
  }
  // SD is done for now
  digitalWrite(chipSelect, HIGH); // disable SD
  digitalWrite(SS_PIN, LOW);  // enable mfrc

and so in loop
around the sd structure you should do the same.

well yes there is that, not only that you you are using 'Strings' but you are mish-mashing them with other Strings and file objects.

File uidsfile = SD.open("uids.txt");

File namefile = SD.open("names.txt");

File dataFile = SD.open("data.txt", FILE_WRITE);

These 3 file objects are all in scope at the same time, close() closes the file, but the object still exists.

String content = "";

String line;

These Strings are both still in scope as well at the same time as those file objects, although they may not change size after their first processing.

while (namefile.available()) {
        String line = namefile.readStringUntil('\n');
        if (currentLine == lineNum) {
          line.trim();  // Optional: Remove leading and trailing whitespaces
          Serial.println(line);
          File dataFile = SD.open("data.txt", FILE_WRITE);
          dataFile.print(line);
          dataFile.close();

          break;  // Exit the loop after printing the target line

this is already much better, both 'line' & 'dataFile' go out of scope at the end of the iitiration of the while loop, freeing up any memory they had in use during that time. If you are going to use dynamic memory on a small memory machine, you should make sure you keep everything as tidy as possible.

1 Like

understandable!! i will check everything of what you said , Thank you very much !!

Thank you very much!! i will test it and i will let you know !!

arduino board you mean ?

No, the SD card module.

No the SD board.

I have this one and had to modify the MIOS tristate line.

microsd-breakout-board

i have the same one !!


Pin 13 has to go to CS. If tied to ground the MISO pin is always an output.

HOWEVER, you need to check yours first. Mine was several years ago and they may have fixed this mistake.

John

1 Like

Thank you very much !!!!!! Appreciate your help guys!!