You are on page 1of 5

Course Title: Microprocessors System (Theory)

Submitted to: Engr, Yasir Malik

Assignment #01

Date of Submission: 27th July, 2023

 Submitted by: Haris Ishtiaq


 Class no: 10
 Reg no: 21ABELT0901
 Semester: 4th
Contents
Abstract: .......................................................................................................................................... 1
1. Introduction: ................................................................................................................................ 1
2. Hardware Setup:.......................................................................................................................... 1
3. Library Installation: .................................................................................................................... 2
4. Code Implementation: ................................................................................................................. 2
5. Explanation of the Code: ............................................................................................................ 3
6. Testing and Output:..................................................................................................................... 3
7. Applications: ............................................................................................................................... 3
8. Conclusion: ................................................................................................................................. 3
Page |1

Interfacing DS1307 Real-Time Clock with Arduino

Abstract:
The DS1307 is a popular real-time clock (RTC) integrated circuit that allows precise
timekeeping in various applications. This report outlines the process of interfacing the DS1307
RTC module with an Arduino board. The report covers the hardware setup, library installation,
code implementation, and potential applications of the DS1307 RTC with Arduino.

1. Introduction:
Real-time clocks are essential components in many projects, enabling accurate
timekeeping and timestamping. The DS1307 RTC is a commonly used RTC module that
communicates with microcontrollers like Arduino via the I2C (Inter-Integrated Circuit) protocol.
This report aims to provide a step-by-step guide on how to interface the DS1307 RTC module with
Arduino.

2. Hardware Setup:
To interface the DS1307 RTC with Arduino, the following hardware components are required:
 Arduino board (e.g., Arduino Uno, Arduino Mega, etc.)
 DS1307 RTC module
 Breadboard and jumper wires
 5V power supply (USB cable or external power source)

Figure 1: RTC Module

Connect the components as follows:


 Connect the VCC and GND pins of the DS1307 to the 5V and GND pins of the Arduino,
respectively.
 Connect the SDA (Serial Data) pin of the DS1307 to the Arduino's A4 pin (for Uno) or 20-
pin (for Mega).
Page |2

 Connect the SCL (Serial Clock) pin of the DS1307 to the Arduino's A5 pin (for Uno) or
21-pin (for Mega).

3. Library Installation:
Before programming the Arduino, the appropriate libraries for the DS1307 RTC module must
be installed. The popular RTClib is commonly used for this purpose. To install the library, follow
these steps:
 Open the Arduino IDE.
 Go to "Sketch" > "Include Library" > "Manage Libraries."
 In the Library Manager, search for "RTClib" and click "Install."

4. Code Implementation:
The following code demonstrates how to read and set the time using the DS1307 RTC
module. Upload this code to the Arduino after connecting the DS1307 module as described earlier.
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
Wire.begin();
rtc.begin();
Serial.begin(9600);
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}

To set time to a specific value:


Page |3

rtc.adjust(DateTime(2023, 7, 26, 12, 0, 0));


To set time to local machine time:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

5. Explanation of the Code:


 The code includes the necessary libraries and initializes the Wire library for I2C
communication and the RTC_DS1307 object for the DS1307 RTC module.
 In the setup() function, we begin the Wire communication and the RTC module.
 The rtc.adjust() function allows setting the RTC's time manually or to a specific date and
time.
 In the loop() function, we continuously read the current time from the RTC and print it on
the serial monitor.

6. Testing and Output:


After uploading the code to the Arduino, open the Serial Monitor in the Arduino IDE. You
should see the current date and time being displayed with a one-second interval between updates.

7. Applications:
Interfacing the DS1307 RTC with Arduino opens up various practical applications, including
but not limited to:
 Real-time data logging with accurate timestamps.
 Scheduling tasks at specific times.
 Creating time-sensitive alarms or timers.
 Building clocks and timers for various projects.
 Time-based automation in home and industrial applications.

8. Conclusion:
Interfacing the DS1307 RTC module with Arduino is a straightforward process that enables
precise timekeeping in various projects. By following the hardware setup, library installation, and
code implementation explained in this report, you can utilize the DS1307 RTC's capabilities in
your Arduino-based projects, enhancing time-sensitive functionalities and data logging.

You might also like