Retrieve RTC Data Show On Serial Monitor
Assalamualaikum. In this article, I will share how to display Time Every Second Arduino RTC. By default, we can use the delay(1000) to wait for the next second display.
But in some conditions, it is not good. delay(1000) will hold the next program code in 1second. Imagine if you have a lot of program code that you have to execute quickly.
RTClib libraries
In this article, I use the RTCLib library from Adafruit while for the RTC module I use DS1307. This code can apply to DS3231 and others.
The concept
The concept of this code is simple. We need to create two variables, where one variable is to read the current time and the second variable is to store the previous time.
So if the current time and the previous time are different, then the seconds have changed and display the time data.
Code
I have written the code to display Time Every Second below, please upload it to your board and see the result. If you don’t have the RTClib in your Arduino IDE, please read this article RTC Tutorial Using Adafruit Library.
#include "RTClib.h"
RTC_DS1307 rtc;
char nameOfDay[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char nameOfMonth[12][5] = {"Jan", "Feb", "March", "Apr", "Mei", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
int current = 0;
int previous = 0;
void setup () {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("RTC not found");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, set manual time");
}
//Set the time according to the computer
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//Set Manual time (Year, Month, Date, Hour, Minute, Second);
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
current = now.second();
if (current != previous)
{
Serial.print(nameOfDay[now.dayOfTheWeek()]);
Serial.print(" ");
Serial.print(now.day());
Serial.print(" ");
Serial.print(nameOfMonth[now.month()-1]);
Serial.print(" ");
Serial.print(now.year());
Serial.print(" ");
if (now.hour() < 10)
{
Serial.print("0");
}
Serial.print(now.hour());
Serial.print(":");
if (now.minute() < 10)
{
Serial.print("0");
}
Serial.print(now.minute());
Serial.print(":");
if (now.second() < 10)
{
Serial.print("0");
}
Serial.print(now.second());
Serial.println();
previous = current;
}
}
If uploaded, you will see the time will change every second like this one:
I hope this Display Time Every Second Arduino RTC article can help your code. Many thanks to Arduino Kode for this tutorial in Indonesian.
If you want to share this article, you can click the share button below. Thanks.