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*

Programming Arduino Uno R3 to trigger a relay once every 24 hours

I programmed an Arduino Uno R3 to trigger a relay once every 24 hours from the moment it is powered on.

I calculated milliseconds and used the function delay().

24 hours * 60 minutes each hour * 60 second every minute * 1000 milliseconds every second

so I used :

delay(86400000)

for 86 million and 400 thousands milliseconds.

Currently, I'm still testing it...

Will this work? If not, is there a better built-in function/library or even external library to do it?

The relay trigger doesn't have to be very accurate at the very exact time each day.

I don't need it to be very accurate, it can shift few minutes every few months or so, thats okay with me.

But how about after few months or years of use ? Will it shift the timing much?

I read somewhere about using millis() function and others use an RTC (Real Time Clock) module.

I don't want to use anRTC module, I am looking for a programmatical solution.

OR will my previous method using delay() work for such a very high number of milliseconds?

Answer*

Draft saved
Draft discarded
Cancel
8
  • Thank you very much ocrdu I have arduino uno R3 (revision three) and I see a rectangular crystal on the board, So I guess its more accurate? Commented Sep 2, 2020 at 16:56
  • 2
    As far as I know, the UNO R3 also has a resonator on the board for the CPU, and the crystal is used for the USB timing only. This may be different between revisions of the board. Still, even with a crystal, I doubt you will get the accuracy you want. Commented Sep 2, 2020 at 17:10
  • 1
    Well, what I would do is just try it with a time of, say, two hours (or however long you can be bothered to observe the thing), see if it is close enough, and if not, introduce a calibration factor in the code to compensate. Measuring equipment would make this an easier process. If it turns out you can't properly adjust that way because of clock instability, you can always add a precise RTC module. Primitive engineering, maybe, but worth a shot. Commented Sep 2, 2020 at 18:51
  • 3
    1. Re "I think what you do will work": It will. delay() can handle up to ULONG_MAX milliseconds, which is about 49.7 days. 2. Re "introduce a calibration factor": if the experiment is performed in just a few hours, this will likely work. But then frequency wander will defeat the calibration at longer time scales. You can expect the clock frequency to "random walk" with a typical √t law at time scales longer than a few seconds. Commented Sep 2, 2020 at 20:51
  • 1
    @Felix0004: 3 minutes in one day is 3×60÷86400 ≈ 0.002, or 2,000 ppm. Kind of high, but still within the tolerance of a typical ceramic resonator. Commented Sep 4, 2020 at 9:26

lang-cpp

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