2

I am building a time-critical system using Arduino. Is there any way to get the elapsed seconds like time.h of C in Arduino? I tried time(NULL) but it returns 0. Then moved forward to use now() in Time.h. But the result persists.

How can I get the elapsed seconds since Jan 1, 1970 until now?

1

The accepted answer is not the only (nor best) answer. It depends what you have in hand. The Time library will allow your arduino to keep the current time, it just need to be set the time periodically (because of clock shifting issues) There are several ways you can get the current time:

  • Use a GPS module
  • Get time from NTP server if you have network access to one, using an Ethernet shield or may be wifi
  • Use a custom Serial interface
  • Use a GSM service
  • Use a RTC module, may be the most popular and standalone, like the DS3231. However these kind of module will also require some adjustment in the long term, and they require a backup battery (often a coin like CR2032) to keep time when module is not externally powered.
  • Use a few push buttons and LCD to have the user manually input it.
  • Use a light sensor to see when it is the brightest time of the day and using azimuth calculation determine the hour of the day (ok, I might be going to far here, but consider a solar powered device in the middle of nowhere)

I sorted them in the order I would choose it if I have several options available. Those are a few ways that come to my mind

  • Brilliant answer. Thanks a lot for enumerating every possible options. – Seeker Sep 20 '17 at 15:30
  • @GLM - generic and broad answers do not help anyone and will only confuse other people looking at the post and answers; using an RTC module is the most standard way of doing it since RTC modules are cheaper and easier to connect and program than spending a lot of time and money on any of the other mentioned alternatives; Answer to the point is the key rather than having a broad array of options; All the best in your journeys; – techniche Sep 22 '17 at 4:49
5

An arduino does not have a real time clock (RTC) built in. Every time it restarts it will reset the millis counter.

You will need to add a RTC peripheral that you can poll to find the current date and time.

3

You must use an RTC module like DS3231 or DS1307 for such purposes and there are good libraries available for these modules;

Unlike the standard return value of milli seconds elapsed since Jan 1 1970 00:00 on a computer, the millis() function on arduino just returns the number of milliseconds since the board began running the current program and this number will overflow (go back to zero), after approximately 50 days as per the official documentation at https://www.arduino.cc/en/Reference/Millis

While millis() can be used for very basic time controlled operations, its not really reliable to be used for time critical operations;

  • 2
    “[millis()] is not really reliable to be used for time critical operations”. This sweeping statement should be better qualified. millis() can be fine for time-critical applications. It all depends on the specific timing requirements of your application (resolution, jitter, long-term/short term accuracy, ...). – Edgar Bonet Sep 17 '17 at 12:17
  • The reason I said its not reliable for time critical operations is because an arduino can reset for various reasons including but not limited to inefficient SRAM usage which is a common mistake done by most newbies; – techniche Sep 17 '17 at 14:28
  • 1
    Thanks a lot for your detailed answer. It really helped. – Seeker Sep 18 '17 at 6:30
0

There is an 1307 RTC module emulator for Arduino: DS1307Emulator. You can set this emulator running and save seconds to struct tm *timeStructure and afterwards converting the structure to a time_t variable using time.h functions

DS1307Emulator.init(13);
DS1307Emulator.bufferUserData();
DS1307Emulator.writeToRTC(0x00); // set address 0
DS1307Emulator.writeToRTC(0x00); // start the RTC issuing the clock run command at address 0
DS1307Emulator.setUserData();

Adresses are as follows:

  • 0x00 - seconds
  • 0x01 - minutes
  • 0x02 - hours

If you want to use weekdays, date, year etc. refer to the DS1307 datasheet or check examples on the link I provided.

For reading seconds:

DS1307Emulator.bufferUserData();
DS1307Emulator.writeToRTC(0x00);
temp = DS1307Emulator.readUserData();

temp now holds seconds in binary format, you may want to convert them to dec.

Check out examples in the link I gave for full operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.