How to add RTC in an Arduino sketch

Greetings to all.
A friend sent me an example of a modified ‘myMATRIX_Clock’ sketch here: LED Matrix Demo 2 - non-blocking scroll. - YouTube but, for private reasons, he did not add the RTC function in the code.
Time is only being taken with the Arduino internal Timer, and I would like to add a real RTC as in the original sketch, but I do not know enough about it.
To facilitate, the original skech is this: Digital_Clock_and_Message, which is attached.
And the code to add the RTC is this: myMATRIX_Clock, which is also attached.
Therefore, I would ask your help to, for kindness, add the RTC function in the myMATRIX_Clock sketch, just like the original sketch.

My sincere thanks,

Daniel Fernandes

Digital_Clock_and_Message.ino (7.62 KB)

myMATRIX_Clock.ino (3.41 KB)

You should probably spend some time looking a the examples that come with the RTC library.

A DS3231 RTC is a good choice.

Yes, I've already taken a look at the libraries RTClib and DS1307 (which is in the original sketch) and tried to adapt it in my sketch, but certainly for lack of knowledge and experience, I could not.
Of course I will use the DS3231, which is more accurate.

You can read the RTC every example: 100ms.

When the RTC seconds change, you then use the read value for hours/minute/seconds in your existing sketch.

You don't even need a library. If you just want the time, this will do. No changes needed for DS3231.

If you look, the first sketch (Digital_Clock_and_Message) uses the RTClib library, and the second sketch (myMATRIX_Clock) is using the internal arduino clock (without RTC), and I want to use 'myMATRIX_Clock' with a RTC module (DS1307 or DS3231) to, when to turn off the arduino, do not waste the time but, I do not know to add it in the sketch, that's it.
If anyone can do that for me, I appreciate it.


There is a ‘Gigs and Collaborations’ forum on this web site where people can do the work for you.

Thank you, I'll take a look.

Oh, if I knew how to do all this, I would gladly help people!
It's just that I have enough difficulties! already tried

There is a ‘Gigs and Collaborations’ forum on this web site where people can do the work for you.

Okay, but do I need to bring it there or do I have to create a new topic there and a new account as well?

I believe the moderator will move this thread if you send a request to them.

I would suggest you follow the advise of post #1

there are example programs in your IDE, just search down the list.

most often, there are some statements you need to define before setup()

call libraries, set variables, etc.

in setup, there is a section that sets the time on the RTC. this is a one time program
you put in the date and time about a minute in advance, then upload the sketch.
at the time you set, you would reset the Arduino and it would set that time.

then you go back to the IDE, comment out that time
then upload the new sketch. this way, it will not re-set the time in the RTC back to that time, with ever power loss.

then watch the terminal window.

play with the examples. (you only need to set the time once, then comment out in all the exampes.)

but you get to see how to read the time.

once you have the time as you like,

copy and post the bits before setup into your desired sketch

as you work with the sketch, the parts should be come more and more clear.


Thanks for your help and encouragement. (yours and others here in the forum) - I’ve tried and I could not.
I’ll try again.

Well/if you run into trouble, ask us to review your attempt on writing the changes to your sketch.

I made the code with the help of a friend;
thank you to all who have had the spirit of help;

I made the code with the help of a friend;
thank you to all who have had the spirit of help;

Always great if you have an experienced ‘buddy’ to get over the speed bumps.
You should re-post your final code below here (in tags)… to help others

You can use the RTC as clock source for timeLib. Just a few lines of code and you have the RTC fully integrated; you just have to set it once (there are example sketches that do this for you, using the compile time of the sketch or by you entering a timestamp in the Serial console).

timeLib then makes anything to do with time manipulations and getting current time etc. very easy.

Here (attached) is the code with the RTC DS3231 functions adapted by the friend Schupp.

Thank you all

myMATRIX_Clock_rtc.ino (3.87 KB)