In this tutorial, we will learn how to get time in Arduino without RTC Module. We use a time library to display live time.
We want to use the time of day (hours, minutes, and seconds) in a sketch, and we don’t want to connect external hardware.
- How to Generate Random Numbers using Arduino.
- Arduino Arithmetic Operators.
- Serial communication in Arduino.
Code for Arduino Time
This sketch uses the time library to display the time of day. Open the Serial Monitor to view the output.
Download Arduino Time Library :
The time library enables us to keep track of the date and time. A standard Arduino board quartz for timing and this is accurate to a couple of seconds per day, but it does not have a battery to remember the time when power is switched off. Therefore, time will restart from 0 each time a sketch starts, so you need to set the time using the setTime function. The sketch sets the time to noon on January 1 each time it starts.
Of course, it’s more useful to set the time to your current local time instead of a fixed value. The following sketch gets the numerical time value (the number of elapsed seconds since January 1, 1970) from the serial port to set the time. You can enter a value using the Serial Monitor.
We can also display the time on an LCD 16×2 Display. So this is your homework to show the time on an LCD.
Take Reference:- LCD 16×2 Display with Arduino.
Creating an Alarm to Periodically Call a Function
We want to perform some action on specific days and at specific times of the day.
TimeAlarms is a companion library included in the Time Library download (installing the Time library will also install the TimeAlarms library).
You can schedule tasks to trigger at a particular time of day (these are called alarms) or schedule tasks to occur after an interval of time has elapsed (called timers). Each of these tasks can be created to continuously repeat or to occur only once.
The TimeAlarm library requires the Time library to be installed. No internal or external hardware is required to use the TimeAlarms library. The scheduler does not use interrupts, so the task-handling function is the same as any other function you create in your sketch.