Arduino digital clock

Arduino digital clock with DS1307

I found a DS1307 module in my mailbox today. The DS1307 is a serial real time clock, or RTC. You can easily find this chip at your favourite electronics dealer and make your own circuit with it. Several companies that make Arduino stuff sell modules with this chip. They all come with a small battery and an oscillator that is required to let the DS1307 do it’s thing. I found mine on Ebay from a Hungarian seller, arrived here pretty quickly so I immediately wanted to try it out.

Adafruit sells a board with the DS1307 chip and has made a nice library for it. It comes with an example that shows the time and date to the serial monitor. I changed the sketch to show the date and time on my I2C LCD display. The DS1307 is also an I2C device so I had to use my new little breadboard to connect both devices to the I2C pins of my Arduino Uno.

Here is the sketch:

// http://www.bajdi.com
// Arduino digital clock
// Date and time shown on I2C LCD display
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

RTC_DS1307 RTC;

void setup () {
    Wire.begin();
    RTC.begin();
    lcd.init(); // initialize the lcd
    lcd.backlight();
  }

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

An RTC module is a very handy device if you want to do some sort of data logging or other time based things. For example in a home automation program where you want to turn on devices at a certain time.

9 responses to “Arduino digital clock”

  1. Claudio Marin

    Very Thanks, its works! in my case i change the address to “0x20”.

    thanks again.

    1. Bebo

      really nice project. I new in this world of programming, could you please explain to me what will be the best way to add an alarm.

  2. Jim Sharman

    I am new to working with Arduino. I am trying to construct a digital clock showing both date and time. I came across your sketch and tried using it. when I entered any line such as “lcd.print(now.year(), DEC);” I get an error statement saying DEC is incorrect. Howcan I correct these errors so that I can use your program. Thanks for your help with this matter.

    Jim Sharman

  3. D6equj5

    Nice project and useful tips and code.
    Thank you for posting.

  4. Israel

    I get error invalid conversion from ‘int’ to ‘t_backlighPol’ [-fpermissive]
    what can I do?

  5. Kevin

    Many thanks i have tried a few codes and yours works well.

  6. Rasheed

    I am new at Arduino I’ve been trying to construct a distance detector using the same code i am confused as to whether i have to add a DS1307 module or not and also my LCD does not show any distance but rather it shows blocks

Leave a Reply

*