luca 27/11/2013 27

All of you certainly had to configure an RTC chip with the current date and time… today’s project can help to perform that task!

The project

RTCSetup is composed by two elements:

  • a GUI, written in C# and running on the PC
  • sketch running on Arduino, that is connected to the RTC

GUI and sketch communicate through a serial connection, with a simple protocol. The source for both of them are available in my Github’s repository.

Arduino and RTC

I chose to use the Adafruit’s RTClib library to talk with the DS1307 chip, that is for sure one of the most used RTC in the hobbistic world. The connection between the IC and Arduino is established using the I2C bus:

In the image below the connection is made using the “new” I2C PINs of Arduino Uno; obviously you can still use the A4 and A5 PINs.

For a detailed description of the connection and the use of the library, please refer to the official tutorial.

The Arduino’s sketch receives the commands from the PC, executes them and sends back a response.


As I wrote, the communication between the GUI and the Arduino uses a simple protocol made by only 4 commands:

Command: ##

The first command sent after the connection is established, used by the PC to verify that a “compatible” device is connected to the serial port. Arduino must answer with !!

Command: ?V

This command is used to obtain the version of the sketch. Arduino answers with the string defined as a constant at the top of the sketch:

#define VERSION     "1.0"

Command: ?T

This command is used to get the actual date and time, read from the RTC. Arduino answers with a string in the format dd/MM/yyyy hh:mm:ss.

Command: !TddMMyyyyhhmmss

This command is used to set the RTC time. Arduino answers with OK.


The GUI, developed in C#, talks with Arduino using the protocol above and implements three functionalities:

  • sets the current date and time
  • sets a custom date and time defined by the user
  • gets and displays the actual date and time of the RTC

First, you must choose the serial port Arduino is connected to and press CONNECT. If the connection is established (commands ## and ?V) the sketch’s version is displayed in the status bar.

The first panel shows the actual time of the PC. With a click on the right button (red arrow) you can configure the RTC with that time:

With the green arrow button you can instead get the actual time of the RTC:

Finally you can, using the date-time picker on the center, choose custom date and time values and send those to the RTC:

Technical details

I used the method described in this tutorial to embed a custom font in the application.

I measured the time (150ms) the sketch takes to set the time when the command is sent by the GUI: this is the reason why in the code you can see that the !T command is sent when the actual milliseconds are 850 and the time passed is the actual one + 1 second.

while (DateTime.Now.Millisecond != 850)
string command = "!T" + time.AddSeconds(1).ToString("ddMMyyyyHHmmss");

string command = "!T" + time.AddSeconds(1).ToString("ddMMyyyyHHmmss");


  1. Devin 30/11/2013 at 04:01 - Reply

    You are the man! Been waiting for something simple like this for setting my rtc’s. Thanks again for sharing!

  2. Antonio 18/02/2014 at 01:02 - Reply

    Where I can downloas RTCsetup GUI?

  3. CS 21/03/2014 at 16:19 - Reply

    Hi, thanks for sharing this nice project – very useful :-) May I ask a question?…. I have updated the Arduino Sketch to use the RTC DS3231 which is very similar – the only difference is that ‘day’ in the DS1307 library becomes ‘date’ in the DS3231 library – all compiles fine but the computer end doesn’t successfully set the RTC desite reporting that it has done so – wondernig if there is anything I need to change in the VB code to get this working? Thanks.

    • luca 22/03/2014 at 15:01 - Reply

      Hi! well… no if your sketch can correctly “parse” the string my app sends and set the fields in the RTC. I don’t have a DS3231, which library are you using on Arduino side?

  4. CS 22/03/2014 at 16:22 - Reply

    Hi Again, Whilst I havn’t got this working with the DS3231 yet I have got it working with the DS1307 – very nice.

    One cool update might be to automatically update the RTC every second so you can see how this relates to the system time with ease :-)

  5. CS 22/03/2014 at 16:29 - Reply

    Hi, Thanks for the reply – the library I’m using is this one: https://gist.github.com/jeje/2021145 – it looks like the day of week element of this library might be tha main difference and is causing the problems?

  6. CS 22/03/2014 at 16:56 - Reply

    Hi Luca,

    OK, it was the Day of Week element that was throwing things out – splicing a ‘0’ into the end of set_time solved that one :-)… would still be great to see RTC time update every second as per System Time…. how do we do that ?

  7. JH 23/09/2014 at 17:40 - Reply

    Hi Luca,
    thanks for your great job. It works fine on 2 different Arduino Mega 2560.
    It doesn’t work on a Pro Micro with ATmega32U4. The ATmega32U4 features build-in USB functionality, instead a additional USB-Controller like the Mega.
    Is this already the problem or could it be with the serial buffer size?

    • luca 29/09/2014 at 07:46 - Reply

      Hi JH, unfortunately I don’t have a Leonardo or similar to test the sketch at the moment… did you get any errors?

  8. Duncan 04/01/2015 at 12:59 - Reply

    Am I correct in thinking that this is windows only?

    I agree with Devin – this is so incredibly useful but I was forced onto Linux when Windows continued to crash regularly, even after a complete re-install.

    It might run under Wine I guess but, so far, I haven’t had a lot of success with that.

    Your ENC28J60 tutorial series is also proving to be incredibly helpful and, as far as I have got with it, everything has worked ‘straight out of the box’.

    Thanks Luca…

    • luca 08/01/2015 at 09:43 - Reply

      Hi Duncan

      thanks for your feedback! I developed the RTCSetup client using .Net, maybe you can run it on Linux using Mono. I’m going to develop my next projects using Java so they should be more portable on different OSes.

      • Duncan 12/01/2015 at 17:00 - Reply

        Thanks Luca
        I took a look at Mono but didn’t understand a word of it (in my 60s and no background in computing, but techie in the days of valves!).
        Could you add this to your list of Java projects – It really is an incredibly useful.
        If it could set all the RTCs on the network, using Ethernet or WiFi (it’s just easier sometimes if things have their own RTC to act at certain intervals) that would be a real killer, but I doubt it’s possible.
        Keep the tutorials coming…

  9. ARE 23/06/2015 at 19:21 - Reply

    Hola que tal. mira traté de correr tu boceto pero me marca un error donde dice que el RTC_sd1307 no tiene un miembro begin en la libreria.no eh podido corregirlo. Utilizo arduino uno.

    • luca 24/06/2015 at 20:48 - Reply

      Hi! Did you install the RTClib from Adafruit?

  10. MileNS 25/06/2015 at 22:03 - Reply

    I really have some bad luck…
    I doesn’t work for me bcs some reason RTC app crash. it does connect to board, it does read (wrong) time/date from ds1307 but when I hit SET button it goes to “not responding”…

  11. Antonio 10/10/2015 at 16:19 - Reply

    è possibile per far girare la una GUI in C# sul Mac?

  12. Harry H. Arends 18/11/2015 at 16:24 - Reply

    Hi Luca,
    I installed the GUI and uploaded the sketch.
    The sketch works fine if i use the IDE terminal, it responds on all commands.
    Using the GUI is gets the correct version (1.0) and the RTC time.
    But when i want to write the new time using the GUI it seems that GUI ‘hangs’.
    The mouse cursor keeps waiting for the program to finisch.
    I am using Windows8.1

  13. Gavin Andrews 30/10/2016 at 12:17 - Reply

    Just wanted to say thanks for this. The clocks had changed and I needed to update the time and didn’t have the time myself. Downloaded this, changed to DS3231 and worked first time.


    (Must add DCF77 to my new clock!)

  14. angelo 30/10/2016 at 21:26 - Reply

    salve luca,
    il mio nome è angelo. ho realizzato il tuo rtcsetup mami sono accorto che manca del passaggio in automatico dell’ora legale/solare. come posso risolverlo?

    • luca 02/11/2016 at 11:36 - Reply

      Ciao Angelo, purtroppo visto che la data di cambio ora varia ogni anno, l’unica possibilità è aggiungere qualche controllo manuale (pulsanti…) per cambiare l’ora manualmente.

  15. Mic 13/07/2017 at 12:55 - Reply

    very nice projet thank you :)
    I have a small clock based on a RTC PCF8563,
    Can you make an adaptation of your setup program for the PCF8563 because I do not know the Visual C but I know how to program in Pascal Delphi is that the adaptation to this language is difficult?
    To help me, you can quote me the methods of dialogue between the RTC and your setup program in the program Source C.

    • luca 13/07/2017 at 13:04 - Reply

      Hi Mic, you have to change the Arduino sketch, the VisualC program only communicates with Arduino, it doesn’t know which RTC is connected.

  16. mauro 02/01/2018 at 11:17 - Reply

    Ciao,stavo utilizzando questo tuo progetto ,ma quando imposto l’orario il tempo della conferma varia da 3 a minuti e il modulo rtc è di conseguenza sempre in ritardo,nvece quando richiedo l’ora è immediato.
    Utilizzo arduino uno R3 con il modulo tiny rtc v1.1 basato su ds1307.
    La cartella visual studio l’ho solo scompattata,non devo installare niente penso?

    • luca 02/01/2018 at 15:50 - Reply

      Ciao Mauro, no, dovrebbe essere sufficiente lanciare il programma… strano che impieghi 3min per configurare l’orario, da me è istantaneo

      • mauro 03/01/2018 at 08:17 - Reply

        Ciao,potrebbe un problema di sistema operativo,io utilizzo windows 8.1.Se volessi inviare la stringa tramite terminale,mi riesci a dire la stringa perchè ci provo,ma non mi funziona .Grazie

  17. John Benedict 28/10/2018 at 15:21 - Reply

    Updated, simple improvements and fixed crash.

    connected = false;
    lbRTCTime.Text = serialManager.getRTCTime();

    public void btGet_Click(object sender, EventArgs e)
    lbRTCTime.Text = serialManager.getRTCTime();

    private void btSetSystem_Click(object sender, EventArgs e)
    lbSystemTime.Text = DateTime.Now.ToString(“dd/MM/yyyy HH:mm:ss”);
    dtpCustom.Value = DateTime.Now;
    lbRTCTime.Text = serialManager.getRTCTime();

Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.