I tested Arduino time keeping without RTC

Hey everyone,

If anyone here is wondering how well an arduino or esp8266 keeps track of time on its own, I tested it for you:

link

Spoiler: only about one second off every day.

Any suggestions on improving the code would be more than welcome. I just read that millis() resets every 50 days, so I am gonna have to write some code to deal with that.

Moderator edit: URL as link

. I just read that millis() resets every 50 days, so I am gonna have to right some code to deal with that.

You don't have to write anything special, just make sure that what you write is written correctly.

"Write", not "right" I saw the mistake right after.

I use this code to keep track of time:

timeNow = millis()/1000;
seconds = timeNow - timeLast;

if (seconds == 60) {
timeLast = timeNow;
minutes = minutes + 1; }

What I mean is that if the millis function resets to zero and my last value was somewhere in the 100000's , timeNow - timeLast will equal a huge negative number. which will mess up my code for sure?

I struggle to find a fix that makes sure no seconds are lost in this scenario

I struggle to find a fix that makes sure no seconds are lost in this scenario

Question : What will happen to the BlinkWithoutDelay example after 49 and bit days when millis() resets to zero ?
Answer : It will carry on regardless and not miss a beat because of how it is written

if i remove the arduino power then install it again. will the time continue or i should set the time again manually?

patrixx:
if i remove the arduino power then install it again. will the time continue or i should set the time again manually?

Without a source for the current time such as an RTC the Arduino cannot maintain time when powered off so you will need to set the time manually each time it is powered up or reset.

You must use subtraction to ensure that the millis() rollover does not cause a problem. Something like

if (millis() - prevMillis >= 1000) {
    prevMillis += 1000;
    seconds ++;
}

If the seconds variable is an unsigned long it will take about 1000 x 49 days before it rolls over. You may or may not need to worry about that :slight_smile:

...R

The conclusion in the "instructable" that you don't need an RTC for accurate timekeeping is not very well supported imho.
Many Arduinos use a ceramic resonator instead of a crystal. These have a tolerance of around 0,5% (1 second in about 3 minutes).
Even crystals age, that it their frequency changes slightly with age. The better RTC chips (say DS3231) include an age compensation.
Apart from all that, one of the main benefits of an RTC is the battery backup so you don't have to reset the time after a power off.
Finally, RTC modules can be had for half a dollar. So the matter is hardly worth discussing.

6v6gt:
Finally, RTC modules can be had for half a dollar.

Are those modules good?

odometer:
Are those modules good?

I have had several similar modules with the model numbers ZS-042 abd DS3231. These were advertised as having a rechargeable battery but actually came with a non-rechargeable battery which can, when the circuit is powered at 5 volts, explode.
Ignoring that minor detail, the clock modules keep excellent time.
In mine, I remove the resistor in the charging circuit and use normal CR2032 cells for the backup (see picture).

ds3231Module.PNG

I have made an Arduino clock to work without an external RTC, but I included two buttons so you could set the time by hand. Later, I added support for an external RTC, but my clock will still run without one.

Please see my "two-button clock" thread, here:
https://forum.arduino.cc/index.php?topic=408565.0

Unluckily, my nano drift off more than 1 second in less than 10 mins. It is a cheap clone though, maybe the genuine Arduino borad may work better.

Of course, if you use an ESP8266, you would usually expect it to use network time. :roll_eyes:

I have built a couple of clocks using esp, Wemos mini to be precise. Used Time library with code to get time from ntp server once per day. These appear to keep time very accurately. One is a chiming clock. It chimes within a second or two of the time signature (the "pips") on the radio. On the other hand, the radio is DAB, and they lag behind the live broadcast by a few seconds.

RoboticEngineer1997:
Unluckily, my nano drift off more than 1 second in less than 10 mins. It is a cheap clone though, maybe the genuine Arduino borad may work better.

I believe that this discussion might interest you:
https://forum.arduino.cc/index.php?topic=583728.msg3982163#msg3982163
(Read replies #25 through #27 in that thread.)

DS3231 + 5V supercap and a diode from +5 supply
Certainly holds time across most outages.