Main Content

Accurate clock just using an Arduino

An accurate clock with date displayed on a 16x2 LCD using just the Arduino, the display and few buttons. No RTC module required.

I started this as an academic exercise, but ended up with a very accurate clock. After running for 5 days, it had not lost or gained any time.

The main issue with using just an Arduino is that its internal clock speed is not 100% accurate. Hence if you just rely on this then the count of the milliseconds elapsed will be out by a small percentage and the clock you are creating will either loose or gain time. My approach was to test the accuracy of the Arduino I was using and determine how many milliseconds it lost or gained per hour. All that was then needed was to program a speed adjustment to add or deduct this difference from the internally tracked milliseconds each hour.

My other concern was whether the Arduino clock was consistently inaccurate, but as indicated, the clock I programmed has maintained very accurate time over 5 days so it appears that the inaccuracy is consistent.

The second issue is that the internal millis() function resets itself every 50 days or so and you cannot manipulate the millisecond count. Hence, the answer was to replace the millis() interrupt using a counter that I could manipulate and that would count the milliseconds from midnight, resetting each day removing any run time restrictions.

Assessing the inaccuracy
To assess the inaccuracy, I made the assumption that my computer clock, and hence the millis() in Processing was accurate. I therefore created a program for the Arduino to send the number of milliseconds elapsed since handshaking to Processing once every 2 seconds and a script for Processing to read this and compare it to its elapsed milliseconds displaying a real time result and the difference after an hour had elapsed. This gave the number of milliseconds that had been lost or gained in an hour and therefore the value to use for the speed adjustment in the clock program.

The code for the Arduino program and the Processing script are provided below.

If you do not have Processing installed, visit https://processing.org where you can download and learn about it.”

Link to article