Go Down

Topic: Setting Arduino's date and time? (Read 1 time) previous topic - next topic


This could go in the networking forum as it involves the WiFi shield or the storage forum as it involves the SD card... but I thought the fact that it should set the info on the actual Arduino puts it here.

I have the WiFi shield with the built-in SD card unit. Whenever I create a file on the SD card, through the Arduino and the WiFi shield, the date and time of the file creation is set to the standard January 1, 2000 00:00. I have found how to connect to an NTP server and read the current date and time.  But how can I set the current date and time on the Arduino for it to use in it's other operations such as file I/O?



Since the SD class doesn't care about the date and time, you have a much bigger job ahead of you than you think. The first thing that you need to do is to make it care.


and the Arduino doesn't have a time or date to set ..........




If you want the time and date survive a reset, you have to put in a DS1307 RTC (there's a dozen different breakout boards available in usual arduino-friendly online stores)


The Arduino documentation for the SD library covers access to the directory and file content, but not to attributes such as the creation/modification times. As far as I can see, these are supported by the underlying SdFat library so could be read at the SD library interface, but there's no obvious way to set them. I see some code in SdBaseFile.cpp which suggests that the creation and modification times default to 1 Jan 2000 but could be modified if a user-defined function was implemented to look up the local date/time:

Code: [Select]

    // set timestamps
    if (dateTime_) {
      // call user date/time function
      dateTime_(&p->creationDate, &p->creationTime);
    } else {
      // use default date/time
      p->creationDate = FAT_DEFAULT_DATE;
      p->creationTime = FAT_DEFAULT_TIME;

Code: [Select]

void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0;

I haven't noticed any documentation describing how to use this (but then, I haven't looked very hard) but I would guess that if you implement a function with the correct signature and then update dateTime_ to point to that function, it would be invoked when you created/updated a file. This leaves you with the problem of how to find the current date/time, and I expect the usual responses would point you towards an RTC or a network interface and (S)NTP.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up