Muhammad_Munir
Published © GPL3+

Adjust RTC DS1307 time by using NTP

How to Adjust RTC DS1307 time by using NTP server

BeginnerFull instructions provided1 hour1,607
Adjust RTC DS1307 time by using NTP

Things used in this project

Hardware components

Esp32
×1
Bread Board
×1
RTC DS1307
×1
Jumper wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Diagram

Code

Code

Arduino
// By fair electro...... For more videos... https://www.youtube.com/channel/UCsn5KGbvt5Se7MaLsfNYthA

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
const long utcOffsetInSeconds = 5 * 3600;  // Adjust this based on your timezone.... 5 is for pakistan
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

const char* ssid = "Munir";
const char* password = "munir1234";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  delay(3000);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  timeClient.begin();
  timeClient.update();
  if (WiFi.status() == WL_CONNECTED) {

     //rtc.adjust(DateTime(2023, 6, 18, 13, 59, 0));
    rtc.adjust(DateTime(2023, 6, 18, (timeClient.getHours()), (timeClient.getMinutes()), (timeClient.getSeconds())));
  }
}

void loop() {

  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();


  delay(1000);
}

Credits

Muhammad_Munir
79 projects • 51 followers
I am Arduino programmer, also expertise in ESP32 and 8266 wifi modules.
Contact

Comments

Please log in or sign up to comment.