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 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.

Leave a Reply