Interface DS1307 RTC Module with Arduino – Display Date/Time on OLED

In this tutorial, we will learn to interface DS1307 RTC module with Arduino UNO. Firstly, we will look into these questions: What is RTC (Real-time clock)? Why real-time clock is used? How an RTC module communicates with Arduino on an I2C communication bus? How to make a digital clock using Arduino and DS1307 RTC module? Secondly, we will discuss pinout, pin configuration details, interfacing circuit with Arduino, and example code at the end.

Aduino RTC real time clock DS1307

We have similar guides with ESP32 and ESP8266 NodeMCU:

Pre-Requisites

Before going further into this article, you should have a basic knowledge of Arduino programming and how to use Arduino IDE. You should know how to interface LCD with Arduino and how to use the input/ output ports of Arduino. If you don’t know how to do these things, we recommend you to go through the following articles to get a better understanding of this articles:

What is RTC or Real time Clock?

As its name suggests, a real-time clock is used to keep a record of the time and to display time. It is used in many digital electronic devices like computers, digital smartwatches, data loggers, and situations where you need to keep track of time. one of the great benefits of a real-time clock is that it also keeps a record of time even if the power supply is not available.

Now the question is how can an electronic device like real-time clockwork without the use of a power supply. Because almost all RTC modules come with small coin size 20mm 3V lithium coin cells which can work for years. Usually, CR2032 coil cells are used.

DS1307 RTC IC

At the center of the DS1307 RTC module is an 8-pin DS1307 chip. It provides a real-time clock that is used to keep track of seconds, minutes, hours, days, months, and years. It uses the I2C communication protocol for communicating with other devices like in our case we are using Arduino. Arduino reads values of time and date from DS1307 using the I2C communication protocol. Moreover, it also has a feature to keep a record of the exact time in case of power failure with the help of a backup battery. It is used to make a real-time clock using some other electronic components.

DS1307 Chip Pinout

The following figure shows the pinout diagram of DS1307 IC.

DS1307 pinout

Now let’s see the functionality of each pin one by one.

  • X1 and X2 are used for crystal oscillators. The crystal oscillator value usually used with DS1307 is 32.768k Hz.
  • VBAT: Connect a CR2032 coil cell as backup power. Its value should be between 3-5 volt. voltage more than 5 volts may burn DS1307 permanently. A backup battery is used to keep track of time in case of power failure.
  • Vcc and GND are power supply pins
  • SDA and SCL are used to communicate with other devices over of I2C communication bus.
  • SQW pin provides square waves of different frequencies such as 1Hz, 4kHz, 8kHz, or 32kHz. The frequency of output wave can be selected by sending a signal to DS1307 over an I2C bus.

If you want to use this IC directly with Arduino, you need to use external components to connect with DS1307. The following circuit diagram shows the connections of all required components with DS1306:

real time clock DS1307 circuit diagram
Real Time Clock DS1307 Circuit Diagram

But instead of using the DS1307 chip alone and making your own PCB design, you can get a ready-to-use DS1307 RTC module at a very low cost.

DS1307 RTC Module

DS1307 RTC module has all onboard components which are required for the correct functionality of a DS3107 chip. On top of that, it contains a holder to connect a 20mm 3V lithium coin cell. This holder can contain any CR2032 battery. Now let’s discuss the components of this module one by one.

At the center of the DS1307 RTC module is a chip which we have discussed earlier. This chip keeps track of seconds, minutes, hours, days, and months. At the end of every month, this chip automatically adjusts its seconds, minutes, hours, and date. It is also possible to configure time display in 12-hour with an AM and PM or 24-hour format.

DS1307 RTC module components

32kHz Crystal Oscillator

As discussed earlier, the DS1307 chip requires 32KHz of an external crystal oscillator for its operation (time-keeping). Therefore, the RTC module comes with 32KHz of an external crystal oscillator.

But there is one problem with this 32KHz crystal oscillator that is a change in environmental temperature affects the oscillation frequency of the crystal oscillator. This change in 32KHz of external crystal oscillation frequency is negligible. But it shows an error in the long run. It gives a clock drift of 2-3 minutes per month.

Onboard 24C32 EEPROM

DS1307 RTC module also contains onboard 24C32 EEPROM. This EEPROM can store 32 bytes and have limited read-write operations. We can use this memory to save time when we want to use an RTC module for Alarm based projects. For example, we want to set an alarm at 8:00 AM every day. We can save this time value into EEPROM and whenever the time matches with this saved value, an alarm starts.

These EEPROM chips also communicate with microcontrollers or Arduino over the I2C bus. Hence, it shares the same I2C bus as DS1307. Different slave addresses are assigned to EEPROM (o 0x50 Hex) and DS1307 chip to communicate with them on the same I2C bus.

Backup Battery

On the back side of a RTC module, there is a holder to connect the CR2032 coil cell. This backup battery is used to keep track of time accuracy even when the main power source which is connected to DS1307 fails. This chip contains a power sensing circuitry which senses the main power and whenever the main power shutdown, it switches to the backup coil cell.

DS1307 RTC module backup battery and DS18B20 sensor

In backup mode, DS1307 consumes only 300nA current. That means a backup battery based on a coin cell can work for more than 17 years. Because, generally, the capacity of CR2032 battery is 47mAh and chip consumes only 300nA when powers through a battery.
Life_time = 47mAh/300nA = 156666 hours = 6527 days = 17+ years

DS18B20 Sensor

There is an empty slot available on this module to connect an external DS18B20 digital thermometer. The three empty pinouts in the bottom right corner is a place holder for the DS18B20 sensor and output of the sensor can be received from DS pin of RTC module.

DS1307 RTC Module Pinout

The following figure shows the pinout diagram. This module exposes a total of 7 pins out of which two are power supply pins and two are for SCL and SDA pins for the I2C communication bus. We have already seen the functionality of all these pins in the previous sections.

DS1307 RTC Module pinout

DS1307 and Arduino Wiring Diagram

First make connections with DS1307 RTC module and Arduino according to this connection diagram:

DS1307 RTC Module interfacing with Arduino
Arduino UNODS1307 RTC Module
5VVCC
GNDGND
A4SDA
A5SCL
Interface DS1307 RTC Module with Arduino hardware

Install DS1307 Library In Arduino IDE

To program a DS1307 RTC module, we use I2C communication pins of Arduino. If you don’t understand about I2C communication, you can read this report:

But it demands an exercise to write code from scratch. Luckily, an Arduino RTClib library is available, which implements callback functions to get time measurements from an RTC module. This library hides all the complexity to interact with an RTC module over I2C communication. We can use simple callback functions implemented by RTClib to read data from DS1307. 

Therefore, to program a DS1307  RTC module with Arduino, you will use the RTClib library available in Arduino IDE. 

To apply this library, first, we need to install this library in Arduino IDE by going to Arduino’s library manager. Open Arduino IDE, go to Tools > Manage Libraries.

Install DS3231 library in Arduino IDE

After that, this window library manager window will appear. Search for “RTClib” by typing in the search bar. You will see many options for DS1307. But the one that we will use in this tutorial is RTClib by Adafruit. Select RTClib by Adafruit and click on the install button. 

Install DS3231 RTC module library in Arduino IDE

When you click on the install button, you may get a message that RTClib may require other libraries dependencies which is currently not installed in your Arduino IDE. We recommend you to install all dependencies by clicking on install button. Otherwise, RTClib will not work properly.

DS3231 library software dependency libraries

Arduino Code

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS1307 DS1307_RTC;

char Week_days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(115200);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! DS1307_RTC.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
    DS1307_RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));

}

void loop () {
    DateTime now = DS1307_RTC.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(Week_days[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");

    // calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));

    Serial.print(" now + 7d + 12h + 30m + 6s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();

    Serial.println();
    delay(3000);
}

Demonstration

In Arduino IDE, click Tools > Board and select Arduino.

select ESP8266 NodeMCU board

Now, click Tools > Port and choose the port which you are using. Now, upload the code by clicking on the upload button.

After you have uploaded the following code on your Arduino development board, press the RESET button as follows:

ESP8266 NodeMCU reset button

In your Arduino IDE, open up the serial monitor and set the baud rate to 115200. The serial monitor will start displaying the current date and time along with future date and time and time since midnight of 1/1/1970.

DS1307 RTC Module with Arduino serial monitor

OLED with DS1307 and Arduino

Now if you want to display time and data on OLED display, make connections according to this diagram. Connect SSD1306 OLED with the same I2C bus. 

OLED with DS1307 and Arduino
DS1307 RTC Module with Arduino display date and time on OLED hardware 1
DS1307 RTC Module with Arduino display date and time on OLED hardware 2

Arduino Code OLED and DS1307

Make sure to install Adafruit SSD1306 OLED library in Arduino IDE by going to Tools > Library Manager.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 RTC;
char days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup() {
  Serial.begin(115200);

   if (! DS1307_RTC.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(1000);
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(30, 20);
  // Display static text
  display.println("RTC");
  display.display();
  delay(3000);
  display.clearDisplay();
}

void loop() {
  DateTime now = RTC.now();

  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print(now.day());
  display.print('/');
  display.print(now.month());
  display.print('/');
  display.print(now.year());
  display.println(days[now.dayOfTheWeek()]);
  display.println(' ');
  display.setCursor(0, 40);
  if (now.hour() < 10)
    display.print('0');
  display.print(now.hour());
  display.print(':');
  if (now.minute() < 10)
    display.print('0');
  display.print(now.minute());
  display.print(':');
  if (now.second() < 10)
    display.print('0');
  display.println(now.second());
  display.display();
}

Demonstration

In Arduino IDE, click Tools > Board and select Arduino.

select ESP8266 NodeMCU board

Now, click Tools > Port and choose the port which you are using. Now, upload the code by clicking on the upload button.

After you have uploaded the following code on your Arduino development board, press the ENABLE button as follows:

ESP8266 NodeMCU reset button

The OLED will start displaying the current date, day and time.

DS1307 RTC Module with Arduino display date and time on OLED demo

Video demo:

DS1307 Proteus Simulation

In this section, we will see a proteus simulation of DS1307 RTC module by using all external components and display the value of time and data on 16×2 LCD.

If you don’t know how to use 16×2 LCD with Arduin, you can read this articles: 

Proteus Circuit 

The following circuit shows the connections of DS1307chip along with external components with an Arduino. SCL and SDA pins of DS1307 are connected with SCL and SDA pins of Arduino. If you have already gone through the above-mentioned article on LCD interfacing with Arduino and other basic articles to get a know-how of Arduino, you can easily understand the following circuit.

circuit diagram of DIgital clock using real time clock and Arduino
circuit diagram of DIgital clock using real time clock and Arduino

Arduino Code

This Arduino sketch displays the time and date value on 16×2 LCD.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 RTC;

void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(20, 4);

pinMode(8,OUTPUT);

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}

void loop () {
DateTime now = RTC.now();
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.setCursor(0, 1);
if (now.hour()<10)
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute()<10)
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second()<10)
lcd.print('0');
lcd.print(now.second(), DEC);
delay(1000);
}

The result of above code is given below:

circuit diagram of DIgital clock using real time clock and Arduino result
circuit diagram of Digital clock using real-time clock and Arduino result

Related Articles:

9 thoughts on “Interface DS1307 RTC Module with Arduino – Display Date/Time on OLED”

  1. I would like to make an automatic college bell, but I want to see a video how to make it step by step, to know what I need to buy. If you already have a video on youtube please give me the link, if not, you can send me an email (petrascuandrei@yahoo.com – my email adress). I wait your answer.
    Thank you !

    Reply
  2. Hello sir… Thanx for the code… It is working properly in software…
    But I want to know if it will work on hardware as well.. Is it not needed to set the time for the first time to RTC module as by default time is something else like 01-01-2000 date 12:00

    Reply
  3. can you help me out to make an automatic school alarm bell using ARDUINO? I want to store the entries of 1 week but the projects available over the internet is for only 1 day entry.

    Please help me….!!!!!

    Reply
  4. Hello Sir, now.hour(), now.minut(). now.second() always give 6,51,48 at serial print. i will share the code and screen shoot of proteus. I will be thankful for assistance in task.

    Reply
  5. I got this error
    /sdk/hardware/tools/avr/bin/avr-g++ -no-canonical-prefixes -std=gnu++11 -c -g -Os -w -mmcu=atmega328p -DF_CPU=16000000L -DUSB_VID=null -DUSB_PID=null -DARDUINO=10812 -DIDE=”ArduinoDroid” -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO /build/sketch_apr12a.cpp -o /build/sketch_apr12a.cpp.o sketch_apr12a.ino: In function ‘void setup()’:
    sketch_apr12a.ino:17:10: error: ‘DS1307_RTC’ was not declared in this scope
    Return code is not 0

    Reply

Leave a Comment