{"id":164069,"date":"2025-01-23T11:27:25","date_gmt":"2025-01-23T11:27:25","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=164069"},"modified":"2025-01-23T11:45:48","modified_gmt":"2025-01-23T11:45:48","slug":"esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/","title":{"rendered":"ESP8266 NodeMCU: Guide for DS1307 Real Time Clock (RTC) Module (Arduino IDE)"},"content":{"rendered":"\n<p>Learn how to use the DS1307 Real Time Clock Module with the ESP8266. We&#8217;ll cover how to set and read the time. You&#8217;ll learn to synchronize the time with the ESP8266 local time and with an NTP server periodically. We&#8217;ll also cover how to deal with time zones and daylight saving time. Finally, we&#8217;ll build an ESP8266 digital clock using the DS1307 RTC module and an OLED display.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU: Guide for DS1307 Real Time Clock Module RTC Arduino IDE\" class=\"wp-image-164070\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Note:<\/strong> this tutorial is also compatible with the DS3231 RTC just by changing one line of code in the examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>In this tutorials, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#intro-rtc\" title=\"\">Introducing Real-Time Clock (RTC) Modules<\/a><\/li>\n\n\n\n<li><a href=\"#intro-ds1307\" title=\"\">Introducing the DS1307 RTC Module<\/a><\/li>\n\n\n\n<li><a href=\"#ds1307-esp8266-wiring\" title=\"\">Connecting the DS1307 RTC Module to the ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"#working-with-rtc\" title=\"\">Working with the RTC<\/a><\/li>\n\n\n\n<li><a href=\"#rtclib\" title=\"\">Installing the RTCLib Library<\/a><\/li>\n\n\n\n<li><a href=\"#set-read-time\" title=\"\">Setting and Reading the Time<\/a><\/li>\n\n\n\n<li><a href=\"#set-time-ntp\" title=\"\">Syncing the RTC with an NTP Server<\/a><\/li>\n\n\n\n<li><a href=\"#timezone-and-dst\" title=\"\">Daylight Saving Time and Time Zone Adjustments<\/a><\/li>\n\n\n\n<li><a href=\"#ESP8266-digital-clock\" title=\"\">ESP8266 Clock with RTC Module and OLED Display (with timezone and daylight saving time)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-rtc\">Introducing Real-Time Clock (RTC) Modules<\/h2>\n\n\n\n<p>RTC modules, such as the DS3231 and DS1307, have their own tiny clock inside to keep track of time by themselves. Usually, they come with a battery holder to connect a battery so that they keep working even if the ESP8266 resets or loses power.<\/p>\n\n\n\n<p>The DS3231 and the DS1307 are some of the most popular choices to use with microcontrollers. Both are compatible with the ESP8266 and communicate via I2C communication protocol. The DS3231 is more accurate because it comes with a temperature sensor and gives temperature-compensated results. Nonetheless, the DS1307 is also very accurate and suitable for most applications that need to keep track of time. In this tutorial, we&#8217;ll cover the DS1307 RTC module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-ds1307\">Introducing the DS1307 RTC Module<\/h2>\n\n\n\n<p>In this tutorial, we\u2019ll be using the DS1307 RTC module, but if you have a DS3231, most of the information provided applies to both modules. Additionally, all the code should be compatible with both modules with just a small change. (In a near future, we&#8217;ll create a tutorial for the DS3231 RTC module).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Module\" class=\"wp-image-163510\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The DS1307 RTC Module comes with the DS1307 chip (to keep track of time) and the AT24C32 EEPROM (to save data permanently). It can also be programmed to output square waves with different frequencies. We\u2019ll only use the DS1307 chip features for timekeeping.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DS1307 Battery Holder<\/h3>\n\n\n\n<p>It comes with a battery holder to connect a CR2032 battery to keep accurate timekeeping. In the event of a power outage, it can still keep track of time accurately.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Real Time Clock Battery Holder\" class=\"wp-image-163511\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Real Time Clock Battery Holder with Battery\" class=\"wp-image-163512\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>This module also comes with the option to connect a DS18B20 temperature. After connecting that sensor to the module, you can get the temperature from the module DS pin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DS1307 RTC Module I2C Address<\/h3>\n\n\n\n<p>By default, the address of the DS1307 RTC is <strong>0x68<\/strong> and the EEPROM connected to the module is 0x50. You can run an <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-scanner-arduino\/\" title=\"\">I2C scanner sketch to double-check the addresses<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"252\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/RTC-I2C-scanner-addresses.png?resize=671%2C252&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"RTC module getting I2C addresses\" class=\"wp-image-163966\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/RTC-I2C-scanner-addresses.png?w=671&amp;quality=100&amp;strip=all&amp;ssl=1 671w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/RTC-I2C-scanner-addresses.png?resize=300%2C113&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">DS1307 RTC Module Pinout<\/h3>\n\n\n\n<p>The following table quickly describes the DS1307 RTC Module Pinout.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>SQ<\/strong><\/td><td>Output for square waves: 1 Hz, 4.096 kHz, 8.192 kHz, or 32.768 kHz (we won\u2019t use)<\/td><\/tr><tr><td><strong>DS<\/strong><\/td><td>Output for temperature readings if DS18B20 is connected (we won\u2019t use)<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>SCL pin for I2C<\/td><\/tr><tr><td><strong>SDA<\/strong><\/td><td>SDA pin for I2C<\/td><\/tr><tr><td><strong>VCC<\/strong><\/td><td>Provides power to the module (3.3V or 5V)<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>GND<\/td><\/tr><tr><td><strong>BAT<\/strong><\/td><td>Backup supply input (we won\u2019t use) or use the default battery holder<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"ds1307-esp8266-wiring\">Connecting the DS1307 RTC Module to the ESP8266 NodeMCU<\/h1>\n\n\n\n<p>Here\u2019s a list of the parts required for this tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266 NodeMCU Board<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/real-time-clock-module-ds1307\/\" target=\"_blank\" rel=\"noopener\" title=\"\">DS1307 RTC Module<\/a> or DS3231 Module<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\" rel=\"noopener\" title=\"\">OLED Display<\/a> (optional)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n\n<p>We\u2019ll only use the I2C and power pins to interface the RTC module with the ESP8266. We\u2019ll connect SCL to <span class=\"rnthl rntcyellow\">GPIO 5<\/span> (D1); and SDA to <span class=\"rnthl rntclgray\">GPIO 4<\/span> (D2). You can use any other suitable I2C pins, as long as you change them on the code. You can use the following table as a reference or take a look at the schematic diagram.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>RTC Module<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntcyellow\">GPIO 5<\/span> (D1)<\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntclgray\">GPIO 4<\/span> (D2)<\/td><\/tr><tr><td>VCC<\/td><td><span class=\"rnthl rntcred\">3V3<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"539\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-Circuit_bb.png?resize=786%2C539&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with DS1307 RTC Module Circuit Diagram\" class=\"wp-image-164080\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-Circuit_bb.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-Circuit_bb.png?resize=300%2C206&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-Circuit_bb.png?resize=768%2C527&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Working with the RTC<\/h2>\n\n\n\n<p>Using an RTC module in your projects always requires two important steps.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Setting the current time<\/strong>: you can do it manually by inserting the current time (or a different desired time) on the code; the system&#8217;s local time; or get the time from an NTP server.<\/li>\n\n\n\n<li><strong>Retaining the time<\/strong>: to make sure the RTC keeps the correct time, even if it loses power, it needs to be connected to a battery. RTC modules come with a battery holder, usually for a coin cell.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rtclib\">Installing the RTClib Library<\/h2>\n\n\n\n<p>There are several libraries to interface with the DS1307 RTC. We&#8217;ll use the <a href=\"https:\/\/github.com\/adafruit\/RTClib\" target=\"_blank\" rel=\"noopener\" title=\"\">RTClib from Adafruit<\/a> that is compatible with DS1307, DS3231, and PCF8523 RTC modules. Additionally, this library is also compatible with ESP8266 boards.<\/p>\n\n\n\n<p>In the Arduino IDE, go to <strong>Sketch<\/strong> &gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. Search for <strong>RTClib <\/strong>and install the library by Adafruit. We&#8217;re using version 2.1.4.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"391\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?resize=750%2C391&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE install RTCLib Library\" class=\"wp-image-163965\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?resize=300%2C156&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"set-read-time\">Setting and Reading the Time<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-with-DS1307-RTC.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU with DS1307 RTC Guide\" class=\"wp-image-164121\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-with-DS1307-RTC.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-with-DS1307-RTC.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The following example sets the time on the RTC clock, and then reads the time in the loop every three seconds. This code shows two different ways to set the time: synchronizing the RTC with the system time (date and time the sketch was compiled), and setting a specific date and time manually by writing it yourself on the code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/\n  Based on the RTClib Library examples: github.com\/adafruit\/RTClib\/blob\/master\/examples\n*\/\n\n\/\/ Date and time functions using a DS1307 RTC connected via I2C and Wire lib\n#include &quot;RTClib.h&quot;\n\nRTC_DS1307 rtc;\n\nchar daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};\n\nvoid setup () {\n  Serial.begin(115200);\n\n  if (! rtc.begin()) {\n    Serial.println(&quot;Couldn't find RTC&quot;);\n    Serial.flush();\n    while (1) delay(10);\n  }\n\n  if (! rtc.isrunning()) {\n    Serial.println(&quot;RTC is NOT running, let's set the time!&quot;);\n    \/\/ When time needs to be set on a new device, or after a power loss, the\n    \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\n    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n    \/\/ This line sets the RTC with an explicit date &amp; time, for example to set\n    \/\/ January 21, 2014 at 3am you would call:\n    \/\/rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));\n  }\n\n  \/\/ When time needs to be re-set on a previously configured device, the\n  \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\n  \/\/rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n  \/\/ This line sets the RTC with an explicit date &amp; time, for example to set\n  \/\/ January 21, 2014 at 3am you would call:\n  \/\/rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));\n}\n\nvoid loop () {\n  \/\/ Get the current time from the RTC\n  DateTime now = rtc.now();\n  \n  \/\/ Getting each time field in individual variables\n  \/\/ And adding a leading zero when needed;\n  String yearStr = String(now.year(), DEC);\n  String monthStr = (now.month() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.month(), DEC);\n  String dayStr = (now.day() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.day(), DEC);\n  String hourStr = (now.hour() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.hour(), DEC); \n  String minuteStr = (now.minute() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.minute(), DEC);\n  String secondStr = (now.second() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.second(), DEC);\n  String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];\n\n  \/\/ Complete time string\n  String formattedTime = dayOfWeek + &quot;, &quot; + yearStr + &quot;-&quot; + monthStr + &quot;-&quot; + dayStr + &quot; &quot; + hourStr + &quot;:&quot; + minuteStr + &quot;:&quot; + secondStr;\n\n  \/\/ Print the complete formatted time\n  Serial.println(formattedTime);\n\n  Serial.println();\n  delay(3000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/Set_Get_Time_DS1307_RTC.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does the Code Work<\/h3>\n\n\n\n<p>Start by importing the <span class=\"rnthl rntliteral\">RTCLib<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"RTClib.h\"<\/code><\/pre>\n\n\n\n<p>Then, create an <span class=\"rnthl rntliteral\">RTC_DS1307<\/span> object called <span class=\"rnthl rntliteral\">rtc<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>RTC_DS1307 rtc;<\/code><\/pre>\n\n\n\n<p>If you&#8217;re using a DS3231 RTC module, use the following line instead.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>RTC_DS3231 rtc;<\/code><\/pre>\n\n\n\n<p>Then, you create a char array with the days of the week.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>char daysOfTheWeek&#091;7]&#091;12] = {\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"};<\/code><\/pre>\n\n\n\n<p>You can access each day by its index. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">daysOfTheWeek[0]<\/span> will return &#8220;Sunday&#8221;.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">daysOfTheWeek[1]<\/span> will return &#8220;Monday&#8221;.<\/li>\n<\/ul>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the RTC module as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (! rtc.begin()) {\n  Serial.println(\"Couldn't find RTC\");\n  Serial.flush();\n  while (1) delay(10);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Checking the RTC Status<\/h4>\n\n\n\n<p>Then, check if the RTC is keeping the time with the <span class=\"rnthl rntliteral\">isrunning()<\/span> function. If it is not running, because it is a new device or because the battery backup failed, we&#8217;ll print a message in the Serial Monitor and set the time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (! rtc.isrunning()) {\n   Serial.println(\"RTC is NOT running, let's set the time!\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting the Time<\/h4>\n\n\n\n<p>To set the time on thr RTC, we can use the <span class=\"rnthl rntliteral\">adjust()<\/span> method on our <span class=\"rnthl rntliteral\">rtc<\/span> object. The following line sets the RTC\u2019s date and time to the current date and time when this sketch was last compiled.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));<\/code><\/pre>\n\n\n\n<p><span class=\"rnthl rntliteral\">__DATE__<\/span> and <span class=\"rnthl rntliteral\">__TIME__<\/span> are macros that provide the current date and time at compilation.<\/p>\n\n\n\n<p>Alternatively, you can set the date and time manually. Pass the time fields in this order: year, month, day, hour, minute, second. This line is commented on the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ January 21, 2014 at 3am you would call:\nrtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));<\/code><\/pre>\n\n\n\n<p>If you need to re-set the time on a previously configured device, you can call one of the previous two lines to set the time without checking if it is running or not (this is commented on the code).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ When time needs to be re-set on a previously configured device, the\n\/\/ following line sets the RTC to the date &amp; time this sketch was compiled\n\/\/rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n\/\/ This line sets the RTC with an explicit date &amp; time, for example to set\n\/\/ January 21, 2014 at 3am you would call:\n\/\/rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Getting Date and Time<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we get the date and time every three seconds, and we print it in the Serial Monitor.<\/p>\n\n\n\n<p>We call <span class=\"rnthl rntliteral\">rtc.now()<\/span> to get the current date and time from the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DateTime now = rtc.now();<\/code><\/pre>\n\n\n\n<p>It returns a <span class=\"rnthl rntliteral\">DateTime<\/span> object containing the values for the current year, month, day, hour, minute, and second.<\/p>\n\n\n\n<p>To access each field of the date and time, we can use the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><span class=\"rnthl rntliteral\">now.year()<\/span><\/td><td>Gets the current year (e.g., 2024)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.month()<\/span><\/td><td>Gets the current month (1\u201312)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.day()<\/span><\/td><td>Gets the current day of the month (1\u201331)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.dayOfTheWeek()<\/span><\/td><td>Gets the day of the week (0-6), where 0 is Sunday, and 6 is Saturday<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.hour()<\/span><\/td><td>Gets the current hour (0\u201323)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.minute()<\/span><\/td><td>Gets the current minute (0\u201359)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.second()<\/span><\/td><td>Gets the current second (0\u201359)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To convert the result into a string, we can use the <span class=\"rnthl rntliteral\">String()<\/span> method. We also pass <span class=\"rnthl rntliteral\">DEC<\/span> as a second argument to the <span class=\"rnthl rntliteral\">string()<\/span> method to get a decimal number.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String yearStr = String(now.year(), DEC);<\/code><\/pre>\n\n\n\n<p>In case of the month, day, hour, minute, and second, we add a leading zero when the number is smaller than 10. So, instead of having, for example: 3:5:6 (which is weird for a time format), you&#8217;ll get <span class=\"rnthl rntliteral\">03:05:06<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String monthStr = (now.month() &lt; 10 ? \"0\" : \"\") + String(now.month(), DEC);\nString dayStr = (now.day() &lt; 10 ? \"0\" : \"\") + String(now.day(), DEC);\nString hourStr = (now.hour() &lt; 10 ? \"0\" : \"\") + String(now.hour(), DEC); \nString minuteStr = (now.minute() &lt; 10 ? \"0\" : \"\") + String(now.minute(), DEC);\nString secondStr = (now.second() &lt; 10 ? \"0\" : \"\") + String(now.second(), DEC);<\/code><\/pre>\n\n\n\n<p>To get the name of the day of the week, we use the <span class=\"rnthl rntliteral\">daysOfTheWeek<\/span> array we created at the beginning of the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String dayOfWeek = daysOfTheWeek&#091;now.dayOfTheWeek()];<\/code><\/pre>\n\n\n\n<p>In the end, we concatenate all the time fields in a variable and display it in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Complete time string\nString formattedTime = dayOfWeek + \", \" + yearStr + \"-\" + monthStr + \"-\" + dayStr + \" \" + hourStr + \":\" + minuteStr + \":\" + secondStr;\n\n\/\/ Print the complete formatted time\nSerial.println(formattedTime);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>With the RTC connected to the ESP8266, upload the code to your board.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200. The ESP8266 will set the RTC time and will display the current time every three seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"264\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-read-time-RTC.png?resize=666%2C264&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with RTC module, setting and getting the time\" class=\"wp-image-164082\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-read-time-RTC.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-read-time-RTC.png?resize=300%2C119&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"set-time-ntp\">Syncing the RTC with an NTP Server<\/h2>\n\n\n\n<p>The following example is similar to the previous one, but it syncs the RTC time with an NTP server after connecting the ESP8266 to the internet. This method is useful if there&#8217;s a need to reset or adjust the time without manual input, for example, after changing the coin cell of the RTC, or if for some reason the RTC module crashes. In this particular example, we also add the option to synchronize the time with the NTP server every hour to prevent drifts along the time (the periodic sync should be adjusted depending on your project requirements).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;time.h&gt;\n#include &lt;RTClib.h&gt;\n\n\/\/ Enter your Wi-Fi credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ NTP server details\nconst char* ntpServer = &quot;pool.ntp.org&quot;;\nconst long gmtOffset_sec = 0;  \/\/ Offset for GMT in seconds\nconst int daylightOffset_sec = 3600;  \/\/ Daylight savings time in seconds\n\n\/\/ RTC object (for DS1307 or DS3231)\nRTC_DS1307 rtc;  \/\/ Change to RTC_DS1307 for DS1307 module\n\nchar daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};\n\n\/\/ Global timeinfo struct and last sync timestamp\nstruct tm timeinfo;\nunsigned long lastSyncMillis = 0;  \/\/ Last sync time in milliseconds\n\nvoid setup() {\n  Serial.begin(115200);\n  initWiFi();\n\n  \/\/ Initialize RTC\n  if (!rtc.begin()) {\n    Serial.println(&quot;Couldn't find RTC&quot;);\n    while (1);\n  }\n\n  \/\/ Sync the RTC at startup\n  syncTime();\n}\n\nvoid loop() {\n  checkTimeAndSync();  \/\/ Check if 1 hour has passed and sync if necessary\n\n  \/\/ Get current time from RTC\n  DateTime now = rtc.now();\n\n  \/\/ Getting each time field in individual variables\n  String yearStr = String(now.year(), DEC);\n  String monthStr = (now.month() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.month(), DEC);\n  String dayStr = (now.day() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.day(), DEC);\n  String hourStr = (now.hour() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.hour(), DEC);\n  String minuteStr = (now.minute() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.minute(), DEC);\n  String secondStr = (now.second() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.second(), DEC);\n  String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];\n\n  \/\/ Complete time string\n  String formattedTime = dayOfWeek + &quot;, &quot; + yearStr + &quot;-&quot; + monthStr + &quot;-&quot; + dayStr + &quot; &quot; + hourStr + &quot;:&quot; + minuteStr + &quot;:&quot; + secondStr;\n\n  \/\/ Print the complete formatted time\n  Serial.println(formattedTime);\n\n  Serial.println();\n  delay(10000);\n}\n\nvoid initWiFi() {\n  WiFi.begin(ssid, password);  \/\/ Connect to WiFi\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;\\nWiFi connected.&quot;);\n}\n\nvoid syncTime() {\n  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);  \/\/ Configure time with NTP server\n  if (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(&quot;Failed to obtain time&quot;);\n    return;\n  }\n  Serial.println(&quot;\\nESP8266 Time synchronized with NTP server.&quot;);\n\n  \/\/ Format the current time as a string\n  char timeStr[64];\n  strftime(timeStr, sizeof(timeStr), &quot;%A, %B %d %Y %H:%M:%S&quot;, &amp;timeinfo);\n  Serial.print(&quot;Current time: &quot;);\n  Serial.println(timeStr);\n\n  \/\/ Sync the RTC with the NTP time\n  rtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,\n                      timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));\n\n  lastSyncMillis = millis();  \/\/ Record the last sync time in milliseconds\n}\n\nvoid checkTimeAndSync() {\n  \/\/ Check if 1 hour has passed since the last sync (1 hour = 3600000 milliseconds)\n  if (millis() - lastSyncMillis &gt;= 3600000) {\n    Serial.println(&quot;Synchronizing time with NTP...&quot;);\n    syncTime();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/Sync_Time_NTP_DS1307_RTC.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does the Code Work?<\/h3>\n\n\n\n<p>This code is similar to the previous one, but uses an NTP server to sync the time. To get time from an NTP server, the ESP8266 needs to be connected to the internet.<\/p>\n\n\n\n<p>First, you need to include the following libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;ESP8266WiFi.h&gt;\n#include &lt;time.h&gt;\n#include &lt;RTClib.h&gt;<\/code><\/pre>\n\n\n\n<p>Then, you need to insert your network connections in the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ WiFi credentials\nconst char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>Next we have the NTP server details. The NTP server returns the time in GMT. If you&#8217;re on a different time zone, you can add the time zone offset on the <span class=\"rnthl rntliteral\">gmtOffset_sec<\/span> variable in seconds. Also add daylight saving time in seconds in the <span class=\"rnthl rntliteral\">daylightOffset_sec<\/span> variable if needed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ NTP server details\nconst char* ntpServer = \"pool.ntp.org\";\nconst long gmtOffset_sec = 0;  \/\/ Offset for GMT in seconds\nconst int daylightOffset_sec = 3600;  \/\/ Daylight savings time in seconds<\/code><\/pre>\n\n\n\n<p>We create a <span class=\"rnthl rntliteral\">tm<\/span> structure called <span class=\"rnthl rntliteral\">timeinfo<\/span> to save the time from the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>struct tm timeinfo;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">tm<\/span> structure contains a calendar date and time broken down into its components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">tm_sec<\/span>: seconds after the minute;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_min<\/span>: minutes after the hour;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_hour<\/span>: hours since midnight;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_mday<\/span>: day of the month;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_year<\/span>: years since 1900;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_wday<\/span>: days since Sunday;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_yday<\/span>: days since January 1;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">tm_isdst<\/span>: Daylight Saving Time flag;<\/li>\n\n\n\n<li><a href=\"http:\/\/www.cplusplus.com\/reference\/ctime\/tm\/\" target=\"_blank\" rel=\"noreferrer noopener\">tm structure documentation<\/a>.<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">To learn more about getting time from an NTP server with the ESP8266, check out this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-date-time-ntp-client-server-arduino\/\">Get Date and Time with ESP8266 NodeMCU NTP Client-Server<\/a>.<\/p>\n\n\n\n<p>We create a variable to save the time that has passed since we synchronized the time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long lastSyncMillis = 0;  \/\/ Last sync time in milliseconds<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we intialize the Serial Monitor and connect the ESP8266 to Wi-Fi.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup() {\n    Serial.begin(115200);\n    initWiFi();<\/code><\/pre>\n\n\n\n<p>We initialize the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize RTC\nif (!rtc.begin()) {\n    Serial.println(\"Couldn't find RTC\");\n    while (1);\n}<\/code><\/pre>\n\n\n\n<p>And finally, we call the <span class=\"rnthl rntliteral\">syncTime()<\/span> function, declared at the end of the code, to synchronize the time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Sync the RTC at startup\nsyncTime();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">The syncTime() function<\/h4>\n\n\n\n<p>On the <span class=\"rnthl rntliteral\">syncTime()<\/span> function, we get the time from the NTP server and save it on the <span class=\"rnthl rntliteral\">timeinfo<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);  \/\/ Configure time with NTP server\nif (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(\"Failed to obtain time\");\n    return;\n}<\/code><\/pre>\n\n\n\n<p>Then, we call the <span class=\"rnthl rntliteral\">rtc.adjust()<\/span> method and pass as arguments the time fields from the <span class=\"rnthl rntliteral\">timeinfo<\/span> variable. This will synchronize the RTC time with the time from the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Sync the RTC with the NTP time\nrtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,\n                     timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> <span class=\"rnthl rntliteral\">timeinfo.tm_year<\/span> holds the number of years since 1900. So, we need to add 1900 to get the current year.<\/p>\n\n\n\n<p>Finally, we update the <span class=\"rnthl rntliteral\">lastSyncMillis()<\/span> with the current time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Sync the RTC at startup\nsyncTime();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we start by calling the <span class=\"rnthl rntliteral\">checkTimeAndSync()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>checkTimeAndSync();  \/\/ Check if 1 hour has passed and sync if necessary<\/code><\/pre>\n\n\n\n<p>This function will check if one hour has passed since the last sync, and synchronize the time if needed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void checkTimeAndSync() {\n    \/\/ Check if 1 hour has passed since the last sync (1 hour = 3600000 milliseconds)\n    if (millis() - lastSyncMillis &gt;= 3600000) {\n        Serial.println(\"Synchronizing time with NTP...\");\n        syncTime();\n    }\n}<\/code><\/pre>\n\n\n\n<p>Still in the <span class=\"rnthl rntliteral\">loop()<\/span>, after synchronizing the time, we get the time from the RTC module and print the time in the Serial Monitor every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Get current time from RTC\nDateTime now = rtc.now();\n\n\/\/ Getting each time field in individual variables\nString yearStr = String(now.year(), DEC);\nString monthStr = (now.month() &lt; 10 ? \"0\" : \"\") + String(now.month(), DEC);\nString dayStr = (now.day() &lt; 10 ? \"0\" : \"\") + String(now.day(), DEC);\nString hourStr = (now.hour() &lt; 10 ? \"0\" : \"\") + String(now.hour(), DEC);\nString minuteStr = (now.minute() &lt; 10 ? \"0\" : \"\") + String(now.minute(), DEC);\nString secondStr = (now.second() &lt; 10 ? \"0\" : \"\") + String(now.second(), DEC);\nString dayOfWeek = daysOfTheWeek&#091;now.dayOfTheWeek()];\n\n\/\/ Complete time string\nString formattedTime = dayOfWeek + \", \" + yearStr + \"-\" + monthStr + \"-\" + dayStr + \" \" + hourStr + \":\" + minuteStr + \":\" + secondStr;\n\n\/\/ Print the complete formatted time\nSerial.println(formattedTime);\n\nSerial.println();\ndelay(10000);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>Upload the previous code to the ESP8266. It will connect to the internet, get the time from the NTP server and synchronize the time of the RTC module.<\/p>\n\n\n\n<p>After that, it will display the date and time every 10 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"377\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-sync-time-RTC-2.png?resize=667%2C377&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 RTC synchronize time with NTP server\" class=\"wp-image-164084\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-sync-time-RTC-2.png?w=667&amp;quality=100&amp;strip=all&amp;ssl=1 667w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-set-and-sync-time-RTC-2.png?resize=300%2C170&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><\/figure><\/div>\n\n\n<p>Every hour, it will resynchronize the time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"timezone-and-dst\">Daylight Saving Time and Time Zone Adjustments<\/h2>\n\n\n\n<p>Even if you adjust the offset to get the time for your time zone, it will not take into account when daylight saving time is active or not (when the hour changes). There is a way to get the time for you specific time zone and adjusted with daylight saving time by setting the <span class=\"rnthl rntliteral\">TZ<\/span> environment variable to the correct value.<\/p>\n\n\n\n<p>The following example is similar to the previous one, but you&#8217;ll always get the correct time taking into account your time zone (and daylight saving time).<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;time.h&gt;\n#include &lt;RTClib.h&gt;  \/\/ For DS3231 or DS1307 RTC module\n\n\/\/ Enter your Wi-Fi credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ NTP server and timezone details\nconst char* ntpServer = &quot;pool.ntp.org&quot;;\nconst char* timezone = &quot;WET0WEST,M3.5.0\/1,M10.5.0&quot;;  \n\/\/ Check list of timezones here: https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\n\n\/\/ RTC object (change to RTC_DS3231 for DS3231 module)\nRTC_DS1307 rtc;\n\nchar daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};\n\n\/\/ Track last sync time\nstruct tm timeinfo;\nunsigned long lastSyncMillis = 0;  \/\/ Last sync time in milliseconds\n\nvoid setup() {\n  Serial.begin(115200);\n  initWiFi();\n\n  \/\/ Initialize RTC\n  if (!rtc.begin()) {\n    Serial.println(&quot;Couldn't find RTC&quot;);\n    while (1);  \/\/ Stop if RTC is not found\n  }\n\n  \/\/ Sync the RTC at startup\n  syncTime();\n}\n\nvoid loop() {\n  checkTimeAndSync();  \/\/ Check if 1 hour has passed and sync if necessary\n\n  \/\/ Get current time from RTC\n  DateTime now = rtc.now();\n\n  \/\/ Format the time as strings\n  String yearStr = String(now.year());\n  String monthStr = (now.month() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.month());\n  String dayStr = (now.day() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.day());\n  String hourStr = (now.hour() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.hour());\n  String minuteStr = (now.minute() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.minute());\n  String secondStr = (now.second() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.second());\n  String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];\n\n  \/\/ Print the formatted time\n  String formattedTime = dayOfWeek + &quot;, &quot; + yearStr + &quot;-&quot; + monthStr + &quot;-&quot; + dayStr + &quot; &quot; + hourStr + &quot;:&quot; + minuteStr + &quot;:&quot; + secondStr;\n  Serial.println(formattedTime);\n\n  delay(10000);\n}\n\nvoid initWiFi() {\n  Serial.print(&quot;Connecting to WiFi&quot;);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;\\nWiFi connected.&quot;);\n}\n\nvoid syncTime() {\n  \/\/ Configure time with NTP server and get time info\n  configTime(0, 0, ntpServer);\n  if (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(&quot;Failed to obtain time&quot;);\n    return;\n  }\n\n  \/\/ Configure timezone for DST using POSIX rule\n  Serial.println(&quot;Configuring timezone and daylight saving time&quot;);\n  setenv(&quot;TZ&quot;, timezone, 1);\n  tzset();\n  if (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(&quot;Failed to obtain adjusted time&quot;);\n    return;\n  }\n\n  Serial.println(&quot;\\nTime synchronized with NTP server with timezone and DST.&quot;);\n  char timeStr[64];\n  strftime(timeStr, sizeof(timeStr), &quot;%A, %B %d %Y %H:%M:%S&quot;, &amp;timeinfo);\n  Serial.print(&quot;Current time: &quot;);\n  Serial.println(timeStr);\n\n  \/\/ Update the RTC time\n  rtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,\n                      timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));\n\n  lastSyncMillis = millis();  \/\/ Update last sync time\n}\n\nvoid checkTimeAndSync() {\n  \/\/ Sync with NTP server every 1 hour (3600000 ms)\n  if (millis() - lastSyncMillis &gt;= 3600000) {\n    Serial.println(&quot;Synchronizing time with NTP...&quot;);\n    syncTime();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/Sync_Time_NTP_DST_Timezone_DS1307_RTC.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Basically, we get the time from the NTP server, convert it to your Time zone by setting the <span class=\"rnthl rntliteral\">TZ<\/span> environment variable, and then, we set the RTC module with that time. Every hour, the RTC module resynchronizes its time (you can change this to a longer period).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does the Code Work?<\/h3>\n\n\n\n<p>This code is quite similar to the previous example, let&#8217;s just take a look at the relevant parts to adjust the time zone.<\/p>\n\n\n\n<p>Set your <span class=\"rnthl rntliteral\">timezone<\/span> string on the following variable. A<a href=\"https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\" target=\"_blank\" rel=\"noopener\" title=\"\"> list of time zone strings can be found here<\/a>. Ours is Europe\/Lisbon.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* timezone = \"WET0WEST,M3.5.0\/1,M10.5.0\";  <\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">syncTime()<\/span> function, we first get the time from the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void syncTime() {\n\n\/\/ Configure time with NTP server and get time info\nconfigTime(0, 0, ntpServer);\nif (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(\"Failed to obtain time\");\n    return;\n}<\/code><\/pre>\n\n\n\n<p>To convert the time to a specific timezone, use the <span class=\"rnthl rntliteral\">setenv()<\/span> function to set the <span class=\"rnthl rntliteral\">TZ<\/span> (timezone) environment variable as follows, in which <span class=\"rnthl rntliteral\">timezone<\/span> is your timezone string.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>setenv(\"TZ\", timezone, 1);<\/code><\/pre>\n\n\n\n<p> After that, call the <span class=\"rnthl rntliteral\">tzset()<\/span> function for the changes to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>tzset();<\/code><\/pre>\n\n\n\n<p>After that, call the <span class=\"rnthl rntliteral\">getLocalTime()<\/span> function again to get the time adjusted for your timezone.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!getLocalTime(&amp;timeinfo)) {\n  Serial.println(\"Failed to obtain adjusted time\");\n  return;\n}<\/code><\/pre>\n\n\n\n<p>Now that you have the time adjusted, you can sync the time of the RTC module using <span class=\"rnthl rntliteral\">rtc.adjust()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>rtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,\n                    timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));<\/code><\/pre>\n\n\n\n<p>The rest of the code works exactly like the previous example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Now, after uploading the code, you should get the correct time for your timezone and adjusted with daylight saving time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"340\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-sync-rtc-module-timezone-dst.png?resize=740%2C340&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with RTC module adjust timezone and DST\" class=\"wp-image-164086\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-sync-rtc-module-timezone-dst.png?w=740&amp;quality=100&amp;strip=all&amp;ssl=1 740w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/esp8266-sync-rtc-module-timezone-dst.png?resize=300%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 740px) 100vw, 740px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"ESP8266-digital-clock\">ESP8266 NodeMCU Clock with RTC Module and OLED Display (with time zone and daylight saving time)<\/h2>\n\n\n\n<p>Now, we can easily build an ESP8266 digital clock that is always with the correct time synchronized with your timezone and taking into account daylight saving time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-OLED-Clock.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Clock with DS1307 RTC and OLED Display\" class=\"wp-image-164123\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-OLED-Clock.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-RTC-OLED-Clock.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Wiring the Circuit<\/h3>\n\n\n\n<p>Add an OLED display to your circuit. Wire it as shown in the table below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>OLED Display<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntcyellow\">GPIO 4<\/span> (D2)<\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntclgray\">GPIO 5<\/span> (D1)<\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td>VCC<\/td><td><span class=\"rnthl rntcred\">3V3<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">ESP8266 NodeMCU Clock with RTC Module and OLED Display &#8211; Code<\/h3>\n\n\n\n<p>Upload the following code to your board. Adjust the <span class=\"rnthl rntliteral\">timezone<\/span> variable with your time zone string and insert your network credentials.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;time.h&gt;\n#include &lt;RTClib.h&gt;          \/\/ For DS3231 or DS1307 RTC module\n#include &lt;Adafruit_SSD1306.h&gt;\n\n\/\/ Enter your Wi-Fi credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ NTP server and timezone details\nconst char* ntpServer = &quot;pool.ntp.org&quot;;\nconst char* timezone = &quot;WET0WEST,M3.5.0\/1,M10.5.0&quot;;  \n\/\/ Check list of timezones here: https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\n\n\/\/ RTC object (change to RTC_DS3231 for DS3231 module)\nRTC_DS1307 rtc;\n\nchar daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};\n\n\/\/ OLED display setup\n#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\n\n\/\/ Track last sync time\nstruct tm timeinfo;\nunsigned long lastSyncMillis = 0;\n\nvoid setup() {\n  Serial.begin(115200);\n  initWiFi();\n\n  \/\/ Initialize the OLED display\n  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\n    while (1); \/\/ Stop if display initialization fails\n  }\n  display.clearDisplay();\n  display.setTextSize(2);\n  display.setTextColor(WHITE); \/\/ Draw white text\n  display.setCursor(0, 0);   \/\/ Start at top-left corner\n  display.display();\n\n  \/\/ Initialize RTC\n  if (!rtc.begin()) {\n    Serial.println(&quot;Couldn't find RTC&quot;);\n    while (1);\n  }\n\n  \/\/ Sync the RTC at startup\n  syncTime();\n}\n\nvoid loop() {\n  checkTimeAndSync(); \/\/ Check if 1 hour has passed and sync if necessary\n\n  \/\/ Get current time from RTC\n  DateTime now = rtc.now();\n\n  \/\/ Format the time as strings\n  String yearStr = String(now.year());\n  String monthStr = (now.month() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.month());\n  String dayStr = (now.day() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.day());\n  String hourStr = (now.hour() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.hour());\n  String minuteStr = (now.minute() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.minute());\n  String secondStr = (now.second() &lt; 10 ? &quot;0&quot; : &quot;&quot;) + String(now.second());\n  String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];\n\n  \/\/ Construct the formatted time\n  String formattedTime = dayOfWeek + &quot;\\n&quot; + yearStr + &quot;-&quot; + monthStr + &quot;-&quot; + dayStr +\n                          &quot;\\n&quot; + hourStr + &quot;:&quot; + minuteStr + &quot;:&quot; + secondStr;\n\n  \/\/ Display the formatted time on the OLED\n  display.clearDisplay();\n  display.setCursor(0, 0);\n  display.println(formattedTime);\n  display.display();\n\n  delay(1000); \/\/ Update every second\n}\n\nvoid initWiFi() {\n  Serial.print(&quot;Connecting to WiFi&quot;);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;\\nWiFi connected.&quot;);\n}\n\nvoid syncTime() {\n  \/\/ Configure time with NTP server and get time info\n  configTime(0, 0, ntpServer);\n  if (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(&quot;Failed to obtain time&quot;);\n    return;\n  }\n\n  \/\/ Configure timezone for DST using POSIX rule\n  Serial.println(&quot;Configuring timezone and daylight saving time&quot;);\n  setenv(&quot;TZ&quot;, timezone, 1);\n  tzset();\n  if (!getLocalTime(&amp;timeinfo)) {\n    Serial.println(&quot;Failed to obtain adjusted time&quot;);\n    return;\n  }\n\n  Serial.println(&quot;\\nTime synchronized with NTP server with timezone and DST.&quot;);\n  char timeStr[64];\n  strftime(timeStr, sizeof(timeStr), &quot;%A, %B %d %Y %H:%M:%S&quot;, &amp;timeinfo);\n  Serial.print(&quot;Current time: &quot;);\n  Serial.println(timeStr);\n\n  \/\/ Update the RTC with NTP time\n  rtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,\n                      timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));\n\n  lastSyncMillis = millis(); \/\/ Update last sync time\n}\n\nvoid checkTimeAndSync() {\n  \/\/ Sync with NTP server every 1 hour (3600000 ms)\n  if (millis() - lastSyncMillis &gt;= 3600000) {\n    Serial.println(&quot;Synchronizing time with NTP...&quot;);\n    syncTime();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/Sync_Time_NTP_OLED_DS1307_RTC.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>We won&#8217;t explain how this code works. Most of it was explained in previous examples. To learn how to interface the OLED display with the ESP8266, you can use the following tutorial as a reference.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-0-96-inch-oled-display-with-arduino-ide\/\" title=\"\">ESP8266 OLED Display with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code, the digital clock is ready. Now, you can build a nice case for a more finished look.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-OLED-Clock-with-RTC.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Digital Clock with RTC and OLED\" class=\"wp-image-164047\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-OLED-Clock-with-RTC.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-OLED-Clock-with-RTC.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to use the DS1307 RTC module with the ESP8266. You learned how to set and read its time. You also learned how to synchronize the time with an NTP server periodically, so that the RTC is always on time. Additionally, we also took a look at how to adjust the time to your time zone and take into account daylight saving time.<\/p>\n\n\n\n<p>Finally, we created a digital clock using an OLED display. Alternatively, you may also want to use an <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-i2c-lcd-arduino-ide\/\" title=\"\">LCD<\/a>, for example. You may also want to combine this project with the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht-temperature-and-humidity-oled-display\/\">ESP32\/ESP8266: DHT Temperature and Humidity Readings in OLED Display<\/a><\/li>\n<\/ul>\n\n\n\n<p>We have tutorials and guides for more than 20 modules with the ESP8266 that you may find useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-guides-sensors-modules\/\">ESP8266 NodeMCU: 21 Free Guides for Sensors and Modules<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about the ESP8266, check out our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\" title=\"\">Home Automation Using ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\" title=\"\">All our ESP8266 Tutorials and Guides<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use the DS1307 Real Time Clock Module with the ESP8266. We&#8217;ll cover how to set and read the time. You&#8217;ll learn to synchronize the time with the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU: Guide for DS1307 Real Time Clock (RTC) Module (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-ds1307-real-time-clock-rtc-arduino\/#more-164069\" aria-label=\"Read more about ESP8266 NodeMCU: Guide for DS1307 Real Time Clock (RTC) Module (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":164070,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,246,300,240,264],"tags":[],"class_list":["post-164069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP8266-DS1307-RTC-Guide.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/164069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=164069"}],"version-history":[{"count":7,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/164069\/revisions"}],"predecessor-version":[{"id":166917,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/164069\/revisions\/166917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/164070"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=164069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=164069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=164069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}