Start a new topic

How to get my DS3231 RTC on my Nextionscreen?

Hello everybody,

How can I get my RTC DS3231, wich is connected to my Arduino Mega, to set the time on my Nextionscreen?

I attempted to find an example. Therefore I checked the Smart Fish tutorial.

The problem is that I use a sketch with a library from Adafruit, the RTClib. That one and the one they used in the Smart Fishtank sketch, although they named them the same, are different. They are not compatible. I would like to stick on my Adafruit RTClib. How can I solve this problem?

Thanks for your help!

Ben Vogelzang.

Arduino question - ask at Arduino

Nextion users are less likely to build you a library for DS3231

  - that is an Arduino thing.

You probably misunderstood my question?

I don’t want you to build or rebuild me an Arduino library.

I want to set the Time on my Nextionscreen send from an Arduino Mega using the DS 3231.

You, people, do have, as I mentioned, an example, the Smart Fish tutorial. But that tutorial is useless for me for reasons I already gave.

So, the question is; how do I get the Time printed on my Nextion screen?

Thanks again,

Ben Vogelzang.

Okay, an open source project is code donated to public

 - credit the creator, do as you will with the source.

When such an open source project is useless for one reason or another

 - it doesn't matter that it is useful for specific purpose

 - it only matters creator had fun, and shared such fun with others.

 - a few people enjoyed, the creator was happy

   - the creator probably isn't going to redo and code for your purpose

   - code was free, he most likely isn't now going to work further for free.

So OpenSource Fishtank is moot point.

So now for the Nextion portion of your question

1) are you trying to set Nextion's RTC on an enhanced model or

2) are you trying to send Arduino DS3231 time to Nextion screen

    a) as a Text

    b) as numbers

Thanks for your reply.

I do have an Enhanced 7 inch model. In this case I can’t use the built-in clock.

Because, for various reasons, the clock in my sketch needs to give the time that I want on my Nextion.

It would be fine, btw., if it was adjustable. But first things first.

Will I want to use Text? I don’t know..

If that could be done best by numbers, well, then that would be the better choice?

Thanks again,

Ben Vogelzang.

User decision for Text or Numbers

  the consideration is the format separators  : . / 

When I use number components, I embed separators in background image.

Text requires more fiddling with to convert and add separators but sent as one.

Nextion time is indeed adjustable.


If you can successfully print your time to the Arduino Serial monitor in the format you desire, then you are a very small step away from doing the same on a Nextion.

The process is the same, you just need an instruction prefix say 




 Followed by the Nextion terminating characters, an example....


 Assuming that Nextion is connected to Serial1 and the textbox is t0

You can of course do variations on this format and send every second if you want the that level of precision on the display.

Then, numbers are indeed the best choice for me. But, I wonder how its done?

How do I get the DS3231 values printed on my Nextion? Can you give me an example?

Thanks again for your reply!


Ben Vogelzang.

Sorry I made an error in the last string - should be.... 


 To include the required closing quote for the 'time' string

@indev2, Thanks very much for your answer!

I will try this, first thing, tomorrow.

I let you know whether my attemps were successful!


Ben Vogelzang.


If you first test with Adafruits RTClib example here..

add my example code in blue, in the loop() prints, the results should be self explanatory. 

void loop () {

    DateTime now =;

    Serial.print(now.year(), DEC);


    Serial.print(now.month(), DEC);


    Serial.print(, DEC);

    Serial.print(" (");


    Serial.print(") ");

    Serial.print(now.hour(), DEC);


    Serial.print(now.minute(), DEC);


    Serial.print(now.second(), DEC);


    //Nextion command string format to textbox t0


    Serial.print(now.hour(), DEC);


    Serial.print(now.minute(), DEC);


    Serial.print(now.second(), DEC);


    //Should print for example... t0.txt="15:23:17"ÿÿÿ


And just to point out that as a general rule, when developing any code for your Arduino -> Nextion. As Nextion conveniently uses human readable serial character commands, then always test your output to a Serial monitor first. It can save you much time and headaches in development. But remember there should be no Carriage Returns or Newlines ('\r\n') sent between in your final code, whilst they are useful during development.   

I will also add

  Nextion side RTC system variables in the Nextion Instruction Set.

  rtc0 - 4 digit year, rtc1 - month, rtc2 - day

  rtc3 - hour, rtc4 - minute, rtc5 - second

   rtc6 - is readonly auto calculated weekday (0 Sunday to 6 Saturday)

If you send


In this manner you would be able to update Nextion side RTC to sync to your Arduino.

If periodically synchronizing with Nextion RTC, then time will never be far off.

With Nextion side synchronized to your Mega side

  then 6 Number components year, mon, day, hour, min, sec

  and a timer with tm0.tim=250

    (250 ensures screen updates are close to timely, with no seconds missed in refresh)

  then inside timer event the code







But you'll still need to investigate getting your values from your DS3231

Using Adafruits RTClib this function should set Nextions RTC, not this code is not tested!!! 

void setNexRTC(){

uint16_t timeComponents [6] = {now.year(), now.month(),, now.hour(), now.minute(), now.second()};

    for(uint8_t i = 0; i < 6; i ++) {
      char rtcBuffer[13];
      sprintf(rtcBuffer,"rtc%d=%d\xFF\xFF\xFF", i, timeComponents[i])
      Serial.print(rtcBuffer);//Or Serial1/2/3 to suit


First of all, I would like to thank you for your help!

I am showing you now a part of a much bigger sketch. My RTC 3231 clock, btw, is running correctly

In the end I would like to have my clock sketch adjustable.

This is what I did so far. But I must doing something wrong because it didn't work.

CurrentTime =;

 //Set the time on my Nextion.
 Serial2.print("NexText.t0=\"");  //t0 is my Hours textbox
 Serial2.print("NexText.t1=\"");  //t1 is my minutes textbox
 Serial2.print("NexText.t2=\"");  //t2 is my seconds textbox
 Serial2.print("NexText.t5=\"");  //t5 is used for separators
 Serial2.print(" : ");
 Serial2.print("NexText.t6=\"");  //t6 is used for separators
 Serial2.print(" : ");
 Serial2.print("NexText.t4=\"");  //Textbox 4 is uesed for, from left to right, day-month-year.


Ben Vogelzang.