Arduino real time clock with DS1307

Arduino real time clock with DS1307, LCD and set buttons

This post shows a simple real time clock and calendar example using an Arduino UNO board and DS1307 RTC chip where time and calendar are displayed on 1602 LCD screen and it can be set with two push buttons.

The DS1307 is an IC (integrated circuit) which has only 8 pins, it’s low cost, easy to use and it has the ability to count time and date in real time (more details are in the datasheet).

DS1307 RTC pin configuration

Hardware Required:

  • Arduino board
  • DS1307 RTC
  • 16×2 LCD screen
  • 32.768KHz crystal oscillator
  • 2 x push button
  • 2 x 10K ohm resistor
  • 10K ohm variable resistor (or potentiometer)
  • 330 ohm resistor
  • 3V coin cell battery
  • Breadboard
  • Jumper wires

Arduino real time clock with DS1307 circuit:

Arduino DS1307 LCD and set buttons circuit

In the circuit there are 2 push buttons (B1 & B2) connected to pins 8 and 9 respectively, the two push buttons are used to set time date parameters (minutes, hours, date, month and year). Button B1 selects the parameter and B2 increments the selected parameter.

Arduino real time clock with DS1307 code:
The Arduino code below doesn’t use any library for the DS1307 RTC, the Wire library is for the communication between the Arduino and the DS1307 using I2C protocol.

The DS1307 works with BCD format only and to convert the BCD to decimal and vise versa I used the 2 lines below (example for minute):
// Convert BCD to decimal
minute = (minute >> 4) * 10 + (minute & 0x0F);

// Convert decimal to BCD
minute = ((minute / 10) << 4) + (minute % 10);

void DS1307_display() : displays time and calendar, before displaying time and calendar data are converted from BCD to decimal format.
void blink_parameter() : this small function works as a delay except that it is interrupted by the buttons B1 (connected to pin 8) and B2 (connected to pin 9). When called and without pressing any button the total time is 10 x 25ms = 250ms. With this function we can see the blinking of the selected parameter with a frequency of 2Hz. So a delay of 250ms comes after the print of the selected parameter and after that delay a 2 spaces is printed which makes the parameter disappears from the LCD and another 250ms delay comes after the print of the 2 spaces.
The complete code is below.

Arduino real time clock with DS1307 videos:
The video below shows a hardware circuit of the project.

and the second video shows Proteus simulation.

To be able to simulate this example, Proteus needs the Arduino library which can be downloaded from the link below. After extracting the files (ARDUINO.IDX and ARDUINO.LIB) put it in the Library folder (ex: C:\Program Files\Labcenter Electronics\Proteus 8 Professional\LIBRARY):

Arduino + DS1307 + LCD Proteus simulation file download:

28 thoughts on “Arduino real time clock with DS1307”

  1. Thanks. The clock project runs in Arduino. However, the set time is not stored in simulation. How to save the set time? Can one answer please! Thanks again.

  2. am not able to know that whether ds1307 rtc module gives the exact time when power supply is off and then on..??

    1. Simple Projects

      Actually I don’t know, read DS1302 & DS1307 datasheets and see what’re the differences, just make a try!

    1. Simple Projects

      Try with this:
      Open Arduino IDE, go to File –> Preferences –> Settings
      Check: compilation
      After you verify the code you will get ELF as well as HEX file paths at the bottom of the compilation box, for example:

      You can copy each path using keyboard buttons Ctrl + P (after the selection of the path).

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top