Date on SD card file

I'm writing files to an SD card in an Arduino MKR IoT Carrier. Whatever file I write is always dated Jan. 01, 2000 00:00. This only happens when I write the file with the SD card in the carrier. Files created when I use the SD card in my computer slot have the proper date and time. Any suggestions on what's happening?? Thanks. AA

So have you put any facility in your Arduino sketch to set the date and time?

Not really, do I need to include code in my sketch to set the date and time of the file created on the SD card?? If I do, can you suggest a reference on how to do that??

my be that's relevant for you?

Thank you kindly!! I will try this reference.
Thank you @awneil and @J-M-L for your suggestions, much appreciated. AlexA

https://forum.arduino.cc/t/revisiting-add-date-and-time-to-your-sd-card-files/1161570

Please don't cross post?

Cross post??

This topic is related to that topic correct?
I think that It would be easier for helpers to have it in one topic?

I am trying to replicate the solution given in topic Add DATE and TIME to your SD CARD Files.. However, the line SdFile::dateTimeCallback(dateTime); throws error: no matching function for call to 'dateTimeCallback(void (&)(byte*, byte*)). I'm using the libraries (SD.h and SPI.h) that are included in the carrier of the Explore IoT Kit. Is there an issue with them or I'm not doing this right?? Thanks in advance.

Who knows? Post the complete code, following the guidelines in "How to get the best out of this forum", and people will be able to help.

#include <SD.h>

File file;
const uint8_t CS = 10; // SD chip select

    // YOUR SKETCH SHOULD UPDATE THESE SIX VALUSE
    // EACH TIME BEFORE CREATING YOUR SD CARD FILE
unsigned int year = 2015;
byte month = 6;      
byte day = 18;
byte hour = 7;
byte minute = 8;
byte second = 9;

void dateTime(uint16_t* date, uint16_t* time)
{
  *date = FAT_DATE(year, month, day);
  *time = FAT_TIME(hour, minute, second);
}

void setup()
{
  Serial.begin(9600);
  
      // THIS LINE SETS YOUR SKETCH TO SAVE YOUR
      // TIME AND DATE INTO ANY FILE YOU CREATE.
  SdFile::dateTimeCallback(dateTime);
  
  if (!SD.begin(CS))
  {
    Serial.println("SD.begin failed");
    while(1);
  }
  file = SD.open("TEST_IT.TXT", FILE_WRITE);
  file.close();
  Serial.println("Done");
}

void loop() {}

Seems like that function to the SD.h is removed.

However, if you want to add time and date to the file you are creating, it is quite easy.

Can you suggest a reference on how to add date and time to a file created on an SD card??

Look for a version of the library that still has that function. Or, write the date and time of creation into the file (as the first line, for example).

It compiles for me. I'm on IDE v1.8.13 with SD library version 1.2.4.

The error message is interesting because it says you're passing in a function that takes two byte arguments rather than two uint16_t arguments. If I change the dateTime function to void dateTime(byte* date, byte* time) I get the same error message that you posted.

Edit: by the way, 1.2.4 is the latest version of the SD library according to SD - Arduino Reference. The source has that method.

Right on, it worked!! Changed the arguments of dateTime to uint16_t like you suggested and it compiled without a problem. I always thought that uint16_t and byte were identical. I guess they aren't?? I now need to figure out how NOT to hard code the date and time, so that it's read from my system. In any case, THANKS A MILLION!!

But I just copied your code from post #4. Were you using some other code that you didn't post here?

By the way, byte is the same as uint8_t, since a byte is 8 bits (ignoring the various historical definitions of "byte").

That was my bad, uint16_t is 2 byte unsigned integer. No I did not post any other code

Threads merged.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.