Clock in Clock Out Device SD card problem

Arduino: Nano

Modules: RTC DS3231, SD card Module, and RFID RC-522

I am very new to Arduino and currently building my first project, a device to that allows you to log times in order to clock in and out of a location. My code and circuit appear to working, however after the first time I return the SD to my computer, the data file will no longer update when I plug it back into the SD module. If i reupload the code to the Arduino, this solves the problem until the next time I remove the SD. In other words, I will upload the code, and be able to scan my RFID, this gives me the data for that RFID UID along with the time and date down to the minute. As long as I keep the SD card in its module the SD will still save data from new RFID scans. However, once I remove the SD card and place it in my computer, check the file, then return the SD to the module, any new scan I do of the RFID does not save its data on the SD card, unless I completely reupload my code to the Arduino. What can I do to fix this?

I derived my device and code from a similar project that I found a tutorial for online:

Here's my variation of the code:

#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC

// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define select pin for SD card module
#define CS_SD 4 

// Create a file to store the data
File myFile;

// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID); 

// Variable to hold the tag's UID
String uidString;

// Instance of the class for RTC
RTC_DS1307 rtc;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Pins for LEDs and buzzer
const int greenLED = 7;

void setup() {
  
  // Set LED as outputs
  pinMode(greenLED, OUTPUT);

  // Init Serial port
  Serial.begin(9600);
  while(!Serial); // for Leonardo/Micro/Zero
  
  // Init SPI bus
  SPI.begin(); 
  // Init MFRC522 
  rfid.PCD_Init(); 

  // Setup for the SD card
  Serial.print("Initializing SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Setup for the RTC  
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if(!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
}

void loop() {
  //look for new cards
  if(rfid.PICC_IsNewCardPresent()) {
    readRFID();
    logCard();
  }
  delay(10);
}

void readRFID() {
  rfid.PICC_ReadCardSerial();
  Serial.print("Tag UID: ");
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
    String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
  Serial.println(uidString);
 
  // Light LED when a card is read
 digitalWrite(greenLED, HIGH);
    delay(2000);
    digitalWrite(greenLED,LOW);
}

void logCard() {
  // Enables SD card chip select pin
  digitalWrite(CS_SD,LOW);
  
  // Open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // If the file opened ok, write to it
  if (myFile) {
    Serial.println("File opened ok");
    myFile.print(uidString);
    myFile.print(", ");   
    
    // Save time on SD card
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    
    // Print time on Serial monitor
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close();

    // Save check in time;
    userCheckInHour = now.hour();
    userCheckInMinute = now.minute();
  }
  else {
    Serial.println("error opening data.txt");  
  }
  // Disables SD card chip select pin  
  digitalWrite(CS_SD,HIGH);
}

(Code tags added by Moderator)

Creating a new SD file is one thing. Appending to an existing file takes totally different logic. So, I guess you are attempting to append to the file you checked on the computer.
Time to study the documentation.
Paul

1 Like

ok thank you! I think that should get me going in the right direction.

However, I should note that if I delete the file and reinsert it into the SD module, new scans still do not register. I would think that with the file gone, a fresh one should appear and it would allow for new data collection, but this doesn't seem to happen. Is this interrelated or does it require a different solution than just making the file appendable?

Likely to be the difference between deleting a file on your PC and deleting a file on the Arduino. There is no organization that makes sure the SD logic on both are handled identically.
Paul

1 Like

Oh, ok that makes sense. Thank you