Synchronize DS1307 RTC module using RTC widget, basic example


I put together a basic example on how to synchronize an RTC module (DS1307) using the RTC widget on blynk.
This can be helpful to have an automatic RTC adjustment and also to have an independent RTC module.
You’ll need to have a WiFi connection to run this code, when the RTC module gets updated you can loose the connection without losing the RTC module time and date.

This is the app QR if you’re too lazy haha!

As you can see, the time gets synchronized after the 40 seconds, you can set it from the beginning also.

This is the code, I just put together some bits form here and there, thanks to Costas, kig, blynk examples and RTClib examples

 Uaing aome code from kig, Costas, RTClib examples and the RTC widget example 
 I put together a basic example to syncronize an RTC module using the RTC widget on Blynk
 this is just a basic example code to see how to synchronize an RTC module 
 using the time given by the RTC widget on blynk

You will need: 
-DS1307 RTC module (It should work with other types)
-the RTC widget
- 4 Value display (to see each time and date)
 V1 and V2 time and date from the internet
 V3 and V4 time and date from the RTC module

 To test the RTC module don´t set up its time to see it being synchronized

    /* Comment this out to disable prints and save space */
    //#define BLYNK_PRINT Serial

    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>
    #include <TimeLib.h>
    #include <WidgetRTC.h>
    #include "RTClib.h"

    //Auth Token from the Blynk App

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "XXXXX";
    char pass[] = "XXXXX";

    BlynkTimer timer;

    WidgetRTC rtc; 
    RTC_DS1307 Hrtc; //hardware RTC

    // Digital clock display of the time
    void sendTime()

      String currentTime = String(hour()) + ":" + minute() + ":" + second();
      String currentDate = String(day()) + " " + month() + " " + year();
      Serial.print("Current time Wrtc: ");
      Serial.print(" ");

      // Send time to the App
      Blynk.virtualWrite(V1, currentTime);
      // Send date to the App
      Blynk.virtualWrite(V2, currentDate);

       DateTime now =;

      String HRTCTime = String(now.hour()) + ":" + now.minute() + ":" + now.second();
      String HRTCDate = String( + " " + now.month() + " " + now.year();
      Serial.print("DS1307 time: ");
      Serial.print(" ");
      // Send DS1307 time to the App
      Blynk.virtualWrite(V3, HRTCTime);
      // Send DS13207 date to the App
      Blynk.virtualWrite(V4, HRTCDate);

      // Synchronize time on connection

    void syncHRTC() // function to synchronize the DS1307
      if (Blynk.connect() && year() != 1970)
        Hrtc.adjust(DateTime(year(), month(), day(), hour(), minute(), second()));

    void setup()

       if (! Hrtc.begin()) {
        Serial.println("Couldn't find HRTC");

    //using this you can adjust the RTC time 
    //  if (! Hrtc.isrunning()) { 
    //    Serial.println("RTC is NOT running, let's set the time!");
    //    Hrtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    //    // This line sets the RTC with an explicit date & time, for example to set
    //    // January 21, 2014 at 3am you would call:
    //    // Hrtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    //  }

      Blynk.begin(auth, ssid, pass);

      setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)

      // Display digital clock every 10 seconds
      timer.setInterval(10000L, sendTime);
      timer.setInterval(40010L, syncHRTC); //10ms "delay" 

    void loop()

If there is something wrong please let me know it!


You’ll find that if you boot your device without a WiFi signal then code execution will stop at the Blynk.begin line, as this is a blocking command.
So, in a real-life situation where you need to be able to perform timed actions regardless of whether there is a WiFi signal (or whether the Blynk server is up and running) then you’d want to manage your own WiFi connection and use Blynk.config and Blynk.begin.

Having said that, I’m sure its a useful building-block for people who can’t get their heads around working with a virtual and physical RTC and getting them synchronised.

BTW, I edited your post to add triple backticks at the beginning and end of your code so that it displays correctly. For future reference, triple backticks look like this:


1 Like

It is my first post, I did not know about the backtick, thank you for that!

Maybe i did not make the point well, but this is just like a showcase and a basic example to see how to do the synchronization. I’m going to edit the post to be more clear.

I could not find any example to do this, so i put together this example.