ESP32 - DS1307 RTC Module

In this guide, we are going to learn how to use the DS1307 RTC module with ESP32. In detail, we will learn the following topics:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Real-Time Clock DS1307 Module
1×CR2032 battery
1×Jumper Wires
1×Breadboard
1×(Optional) DC Power Jack
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Real-Time Clock DS1307 Module

ESP32 itself has some time-related functions such as millis(), micros(). However, they can not provide the date and time (seconds, minutes, hours, day, date, month, and year). To get date and time, we needs to use a Real-Time Clock (RTC) module such as DS3231, DS1370. DS3231 Module has higher precision than DS1370. See DS3231 vs DS1307

Pinout

Real-Time Clock DS1307 Module includes 12 pins. However, for normal use, it needs to use 4 pins: VCC, GND, SDA, SCL:

  • SCL pin: is a clock pin for I2C interface.
  • SDA pin: is a data pin for I2C interface.
  • VCC pin: supplies power for the module. It can be anywhere between 3.3V to 5.5V.
  • GND pin: is a ground pin.
Real-Time Clock DS1307 Module Pinout

The DS1307 Module also has a battery holder.

  • If we insert a CR2032 battery, It keeps the time on module running when the main power is off.
  • If we do not insert the battery, the time information is lost if the main power is off and you need to set the time again.

Wiring Diagram

ESP32 Real-Time Clock DS1307 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 - DS1307 RTC Module

DS1307 RTC Module ESP32
Vin 3.3V
GND GND
SDA GPIO21
SCL GPIO22

How To Program For DS1307 RTC Module

  • Include the library:
#include <RTClib.h>
  • Declare a RTC object:
RTC_DS1307 rtc;
  • Initialize RTC:
if (! rtc.begin()) { Serial.println("RTC module is NOT found"); while (1); }
  • For the first time, set the RTC to the date & time on PC the sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  • Reads date and time information from RTC module
DateTime now = rtc.now(); Serial.print("ESP32 RTC Date Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(now.dayOfTheWeek()); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC);

ESP32 Code – How to get data and time

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ds1307-rtc-module */ #include <RTClib.h> RTC_DS1307 rtc; char daysOfWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("RTC module is NOT found"); Serial.flush(); while (1); } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // manually sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); Serial.print("ESP32 RTC Date Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC); delay(1000); // delay 1 seconds }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE
  • Search “RTClib”, then find the RTC library by Adafruit
  • Click Install button to install RTC library.
ESP32 RTC library
  • You may be asked to install dependencies for the library
  • Install all dependencies for the library by clicking on Install All button.
ESP32 Adafruit BusIO library
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Open Serial Monitor on Arduino IDE
  • See the output on Serial Monitor.
COM6
Send
ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:35 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:36 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:37 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:38 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:39 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:40 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:41 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:42 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:43 ESP32 RTC Date Time: 2022/08/22 (Monday) 09:31:44
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES