In the Arduino Real Time Clock Tutorial, we will learn about Real Time Clock (RTC) and how Arduino and Real Time Clock IC DS1307 are interfaced as a time keeping device. If you recall, we have already implemented an Arduino Alarm Clock using RTC DS1307 in an earlier project.
But that project didn’t cover the basics of Real Time Clock or RTC, the specifications of DS1307 RTC IC and how to interface a Real Time Clock like DS1307 or DS3231 with Arduino.
An RTC or Real Time Clock is a Timekeeping device, usually in the form of an Integrated Circuit (IC). An RTC is battery powered and keeps track of the current time even when there is no power.
Real Time Clock ICs are present in computers, servers, many embedded systems and in fact they are used wherever it is required to keep an accurate time.
Also read: ARDUINO ALARM CLOCK
Why do we need a Real Time Clock (RTC)?
Even though Arduino and almost all microcontrollers have built-in timers and timekeepers (millis () in case of Arduino), they are power dependent i.e. they run as long as there is power supply. Once the power is turned off (manually or due to power outage), all the timers are reset to 0.
While timekeeping using internal timers is acceptable for simple projects, we need an alternative in projects like data loggers, clocks, alarms, etc. where the timer runs independently irrespective of the external power or if the Microcontroller (or Arduino) is reprogrammed.
Here comes the use of Real Time Clock ICs. Almost all RTC ICs are low-current devices that run for years on a single lithium cell (usually CR2032). One of the popular and most commonly used RTC ICs is the DS1307 Real Time Clock.
DS1307 Real Time Clock
The DS1307 RTC is a low cost, low power real time clock IC that can maintain full clock and calendar i.e. hours, minutes, seconds as well as year, month and day. Some of the well-known features of the popular DS1307 RTC are mentioned below.
- Complete Timekeeping functionality i.e. hours, minutes, seconds, year with leap-year, month, date of the month and day of the week.
- Valid up to the year 2100.
- Low power consumption: consumes less than 500nA while operated on battery.
- Automatic switching to battery supply in case of power failure.
- 24 – hour or 12- hour clock with AM/PM indicator.
DS1307 RTC is available as modules, which consists of all the necessary components like Battery, connectors, pull-up resistors and crystal. One such module is used in this project and is shown below.
DS1307 RTC Pin Diagram
The following image shows the pin diagram of the DS1307 RTC IC. In order to reduce the power consumption, the number of pins on the IC has to be reduced. Hence, DS1307 RTC used I2C Communication.
Pin description of DS1307 RTC
- X1 and X2: These are pins for connecting the crystal of frequency 32.768 KHz to enable the internal oscillator. If an external oscillator is connected to X1, then X2 can be left floating.
- VBAT: Battery Power Supply Pin. Must be connected to a 3V Lithium cell for backup supply.
- GND: Ground Pin.
- SDA: Serial Data Pin. It is the Data Input/Output pin of the I2C Interface. An external pullup of 5V is required, usually through a 10KΩ Resistor.
- SCL: Serial Clock Input Pin. It is the clock input pin of the I2C Interface. It must also be pulled up to 5V through a 10KΩ resistor.
- SQW/OUT: Square wave output pin. If not used, it can be left floating.
- VCC: The main supply pin.
Arduino Real Time Clock DS1307 Interface
Now that we have seen a little bit about the Real Time Clock IC DS1307, we will proceed with the interface of Arduino and Real Time Clock. As mentioned earlier, the DS1307 RTC Module uses I2C Communication.
In the Arduino Real Time Clock I2C interface, the Arduino Microcontroller always acts as Master and the DS1307 acts as Slave. The Master in I2C Communication i.e. Arduino in this case, is responsible for clock signal, bus access, start and stop signals.
The following image shows the circuit diagram of the Arduino Real Time Clock DS1307 Interface. This circuit explains just the basic connections with respect to a DS1307 Module (a board that contains the DS1307 IC along with the crystal, Battery and pullup resistors).
In order to understand better about the DS1307 RTC Module, the following image will help you as it contains the circuit of a typical DS1307 Real Time Clock Module.
- Arduino UNO [Buy Here]
- DS1307 RTC Module
- 16×2 LCD Display [Buy Here]
- Connecting wires
- Power supply
The design of the Arduino RTC Interface is quite straight forward. Connect the SDA and SCL pins of the DS1307 RTC to the SDA and SCL pins of Arduino i.e. pins A4 and A5.
A 16×2 LCD is connected in order to display the data and time information. The connections are made as per the circuit diagram.
Working of Arduino Real Time Clock DS1307 Interface
A simple project where Arduino UNO is interfaced with DS1307 Real Time Clock is implemented here. In this project, we will be programming the DS1307 RTC with current date and time and see whether it actually keeps that data even if the power supply to Arduino is removed.
A special library called “RTClib” is used in the programming and it can be downloaded from this link. Make sure that it is downloaded first and added to the Arduino library database.
In order to upload the data and time into the DS1307 RTC IC, we have used a feature available in the RTClib library, where the Arduino will upload the date and time from the computer while uploading the code.
- With the help of Arduino Real Time Clock interface, we can implement several project related to data logging, alarms, clock, etc.
- Since the RTC Module DS1307 is backed with a battery, it will continue to maintain time even in the event of power fail.