Arduino Clock

In this example we will experiment with a DS1307 breakout, a keypad/LCD shield and an Arduino. We will create a simple clock example.

The DS1307 real-time clock is a low-power, full binary-coded decimal clock/calendar. Address and data are transferred over the I²C bus.The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply, this backup supply is usually a lithium ion battery.

Here is a picture of the setup we are using, you can see the DS1307 breakout connected to the LCD and keypad shield, in this example you can see the time displayed on the LCD.

arduino clock
arduino clock

Layout

Here is how you wire up the DS1307 to your Arduino

VCC and GND connect to 5v and GND
SCL connects to A5
SDA connects to A4

arduino and ds1307
arduino and ds1307

 

keypad ds1307
keypad ds1307

Code

You will need to download and copy the RTCLib into your ARduino libraries folder

https://github.com/getmicros/Arduino-Libraries/blob/master/RTClib-master.zip

#include <Wire.h>
#include “RTClib.h”
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
// Connections: Sainsmart LCD/Keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
//Serial.begin(9600);
Wire.begin();
rtc.begin();

//setup the LCD
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(“Time”);
//debug the rtc
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,1);
lcd.print(now.hour(), DEC);
lcd.setCursor(2,1);
lcd.print(“:”);
lcd.setCursor(3,1);
lcd.print(now.minute(), DEC);
lcd.setCursor(5,1);
lcd.print(“:”);
lcd.setCursor(6,1);
lcd.print(now.second(), DEC);
delay(1000);
}

Links

LCD and Keypad Shield on Amazon US
ds1307 breakout on Amazon US
LCD and keypad shield on Amazon UK
ds1307 breakout on Amazon UK