Search code examples
c++arduino

How do you add a timestamp to an SD card without using a RTC?


I'm using an SD card with an Arduino Mega. Each time I write to the card, which is about once every 2 or 3 seconds, I want there to be a time stamp next to the text, the same way it works on the Serial Monitor.

I was wondering if there's any way to do this programmatically instead of getting an RTC.


Solution

  • There are software RTC libraries out there, like swRTC, and Arduino-RTC has a software-only mode.

    These libraries rely on the accuracy of the Arduino's clock, so they won't be all that accurate and will need adjustment every so often. Also, you will need to set them every time you reset your Arduino.

    Setting and adjusting these software RTCs requires a current time from somewhere, like your PC if connected over serial, WiFi for your network time, GPS time, an SNTP server on the interwebs, etc.

    The simplest way would be to set a time on start-up by hand, and keep time yourself with millis(). Also not very accurate, but better than nothing.

    Having said that: I would just get a hardware RTC; they are cheap, fairly accurate, and they have a backup battery so they always keep time. They are also better than the RTC in Arduinos that do have one in hardware.