Introduction: Arduino – DS1307 Real Time Clock - RTC


Real Time Clock (RTC), which plays essential role in embedded system design, will provide precious time and date to the system. The DS1307 serial RTC is a low power, full binary-coded decimal (BCD) clock/calendar, operates in low power and connected to the Arduino’s I2C bus. The LCD display [16x2 LCD] is used for displaying running RTC values. The Arduino UART is used for setting the RTC parameters via RS232 PC interface. This intractable is describes about RTC and Arduino interfacing and design.

Parts and components

Arduino Uno board = 1 No

16x2 LCD = 1No

1 K ohm potentiometer = 1 No

DS1307 RTC = 1 No

32.768 KHz Crystal = 1 No

10K Resistor = 2 Nos

Step 1: Schematic

The 16x2 is very common type LCD, with two rows, and each row displays 16 characters of either 5x7 or 5x8 dot matrix characters.

The LCD is available in a 16 pin package. It consists of back light and contrast adjustment function and each dot matrix has 5×8 dot resolution.

The 16x2 LCD display is connected to the Arduino (13,12,11,10,9,8) digital IO pins, where those pins are configured as digital in / out, where LCD operates at 4 bit data mode.

If the display is not visible, adjust the Contrast pot (1K), to make it visible.

The DS1307 serial real-time clock (RTC) is a low power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM.

Address and data are transferred serially through an I2 C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information.

The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year.

The clock operates in either the 24-hour or 12- hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply.

Timekeeping operation continues while the part operates from the backup supply.

DS1307 is connected to the I2C bus of Arduino.

The Arduino hardware serial port is configured for 9600, 8N1 mode / baud rate.

The Arduino serial port is connected to Proteus COMport Physical Interface Module (COMPIM) and configured for 9600, 8N1 mode / baud rate.

The com0com null modem emulator is used for creating the virtual serial ports.

COMPIM serial data is passed to com0com virtual serial ports and monitored at the PC serial terminal.

The Arduino UART is used for setting the RTC parameters via RS232 PC interface.

Step 2: Software

The Arduino LiquidCrystal library is used for LCD device.

This display will be used for displaying the RTC parameters.

The Wire.h interface will provide the I2C communication.

The RTClib.h will provide the init, set, get functionalities of DS1307 RTC device.

The Cmd.h interface provide the serial port command line interface for the system.

Every 1000mS a RTC string is send via serial port, which will be received and printed on serial port monitoring tool running at the PC.

Demonstrates the use of an I2C RTC. Date and time functions using a DS1307 RTC connected via I2C and Wire lib RTC Date / Time displayed on 16x2 LCD screen. The Arduino circuit connection for LCD: * LCD RS pin to Digital pin 13 * LCD Enable pin to Digital pin 12 * LCD D4 pin to Digital pin 11 * LCD D5 pin to Digital pin 10 * LCD D6 pin to Digital pin 9 * LCD D7 pin to Digital pin 8

Name:- M.Pugazhendi Date:- 10thAug2016 Version:- V0.1 e-mail:- */

//Include the library code: #include <LiquidCrystal.h>

#include <Wire.h>

#include "RTClib.h" #include "Cmd.h"


//Initialize the LCD library with the numbers of the interface pins LiquidCrystal lcd(13,12,11,10,9,8); RTC_DS1307 RTC; void setup () { //Initialize the Command parser cmdInit(9600);

//Initialize the RTC Wire.begin(); RTC.begin();

//Set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("RTC TEST");

//One second delay delay(1000); //Add a command for setting the RTC values cmdAdd("SetRTC", SetDateTime); } void loop () { //Poll the serial port commands cmdPoll(); DateTime now =;

//Clear screen. Set the cursor to line 1 //Print the date lcd.clear(); lcd.print(now.year(), DEC); lcd.print('/'); lcd.print(now.month(), DEC); lcd.print('/'); lcd.print(, DEC); lcd.print(' '); //Set the cursor to line 2 //Print the time lcd.setCursor(0, 2); lcd.print(now.hour(), DEC); lcd.print(':'); lcd.print(now.minute(), DEC); lcd.print(':'); lcd.print(now.second(), DEC);

#ifdef SERIAL_DEBUG Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(, DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); #endif //Delay one second delay(1000); } // SetDate // Example to show how to split the command and set the RTC // Usage: At the command line, type // SetRTC 2016 08 10 14 56 12 // // The output should look like this: // Arg 0: SetRTC // Arg 1: 2016 // Arg 2: 08 // Arg 3: 10 // Arg 4: 14 // Arg 5: 56 // Arg 6: 12

void SetDateTime(int arg_cnt, char **args) { int Year; int Month; int Date; int Hour; int Min; int Sec; String x = args[1]; Year = x.toInt(); x = args[2]; Month = x.toInt(); x = args[3]; Date = x.toInt(); x = args[4]; Hour = x.toInt(); x = args[5]; Min = x.toInt(); x = args[6]; Sec = x.toInt(); #ifdef SERIAL_DEBUG Serial.print("Year is "); Serial.println(Year); Serial.print("Month is "); Serial.println(Month); Serial.print("Date is "); Serial.println(Date); Serial.print("Hour is "); Serial.println(Hour); Serial.print("Min is "); Serial.println(Min); Serial.print("Sec is "); Serial.println(Sec); #endif // The below line sets the RTC with date & time RTC.adjust(DateTime(Year, Month, Date, Hour, Min, Sec)); }

Step 3: Conclusion

The project is successfully simulated by using the Proteus.

The serial port set and debug messages are given below.


About This Instructable




More by PugazhM:Arduino and ENC28J60 Ethernet Controller, (320x480) TFT LCD, DHT22 Temperature / Humidity Web ServerArduino and Infra-Red (IR) Obstacle Detection / Proximity Sensing / Line Counter, With 320x480 LCD InterfaceArduino and 3.5 Inch (320x480) TFT LCD (ILI9488) SPI Interface With DHT22 Temperature / Humidity Measurement
Add instructable to: