Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Arduino RTC - getting the time from RTC after the power is cut off

I have a new DS1307 (original, not a Chinese rip off), with a new battery. It is working great, but when I turn the Arduino off (it is currently connected to laptop) and then turn it back on, it resets the time.

The line that sets the time is (in setup part of code):

setDS1307time(30, 42, 21, 4, 26, 11, 14);

So, when the Arduino gets the power back it runs the program again and it overrides the correct current time.

How can I avoid this?

Answer*

Draft saved
Draft discarded
Cancel
13
  • 1
    No. You will see that you will need to readjust the time regularly. The DS1307 isn't that precise. Commented Dec 1, 2015 at 17:20
  • 1
    But since you have a network connection, it would be good to look into running NTP. NTP (network time protocol) will hold your time to within a fraction of a second. Commented Dec 1, 2015 at 17:59
  • 2
    No matter how good the RTC is there are reasons why you might want to set the time – battery failure, summer time, moving to a different time zone, initial setting error, ...). Commented Dec 1, 2015 at 18:02
  • 1
    NTP expects the network to go down from time to time. It will recover as soon as it can communicate with its peers again. In the mean time the RTC will help you stay close and will give you a good estimate of the time if you need to reboot. You could use NTP to update the RTC from time to time as well. Commented Dec 1, 2015 at 18:58
  • 1
    I would just let it run. You could cut it down to every few minutes or even every day depending on how good a job the Arduino is doing of keeping time. But I don't think you'll have issues of CPU utilization. Commented Dec 1, 2015 at 20:50

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /