Counting the Time With the Arduino Millis()-function
by Donut Studio in Circuits > Arduino
345 Views, 0 Favorites, 0 Comments
Counting the Time With the Arduino Millis()-function
Hey,
in this instructable I will show you how you can make use of the millis function from the arduino to create a clock. Keep in mind, that the oscillator from the arduino chip is not very precised. If you don't have the space or pins left for a real time clock (rtc), using the internal clock can be reasonable. Especially if you have the chance to update the time from the internet and just let itself count for a few hours.
Supplies
For this project you will only need a chip of your choice that is compatible with the arduino ide. I've used an Arduino Pro Mini but also tried it with an esp8266. Since the oscillator of the esp is more accurate, it only differs about six seconds after ten hours from the real time.
Installing the Library
I've created a simple library which converts the time and makes sure the overflow isn't an issue. It can be downloaded here: https://github.com/KonradWohlfahrt/Arduino-Millis-Time-Library.
Once you've downloaded the .zip file you can extract into the 'Documents/Arduino/libraries/' directory or use 'Sketch/Include Library/Add .ZIP Library...' from the IDE.
To make sure everything is set up correctly, restart your IDE.
Uploading the Example Sketch
Now, you can open the example sketch at 'File/Examples/DonutStudioMillisTime/MillisTimeTest.ino' and select your board and port. If you've embedded the library correctly, the arduino ide should compile flawlessly and upload the sketch to your board.
After opening the serial monitor, you have to select the proper baud rate and watch the chip count. That's it!
Absolute and Relative Time
The library divides the time into absolute and relative.
- The absolute time cannot be changed, it starts counting from the beginning of the programm and resets after a day (23:59:59 + 1s -> 00:00:00).
- The relative time can be set by the user and uses the absolute time and the input to output the current time.
For your projects you'll probably want to use the relative time and let it be adjusted by the user.
For more information, you can take a look at all the available functions at the github repository.
Further Steps
There is also another example provided in the library. It displays the relative and absolute time at a seven segment display. You will need another library of mine but I've also published an instructable on how to use it (this example uses the same pinout): https://www.instructables.com/Using-a-Seven-Segment-Display-Library-for-Arduino/
Thanks
Thanks for trying out my library and have fun creating projects with it.
Cheers, Donut Studio!