6074

In today's lecture, we will discuss the Interfacing of RTC Module DS1307 with Arduino, we will design a Proteus Simulation.

Arduino microcontrollers are extremely popular in the embedded systems domain because of their simplicity, affordability, and flexibility. They provide a wide platform for hobbyists, students, and engineers to develop innovative projects. However, one limitation of the Arduino is that it does not have an internal real-time clock. This means that whenever the Arduino is powered off or reset, it loses track of the current time. For applications that rely on time-based operations, this can be a major problem.

To solve this issue, an external Real-Time Clock (RTC) module is integrated with Arduino systems. RTC modules are specialized electronic devices designed to maintain accurate time and date information even when the main system is powered down. They achieve this reliability by using a small backup battery, which allows them to keep functioning independently of the main power source.

Simulation tools such as Proteus Design Suite allow engineers and students to test and verify their designs virtually before implementing them in physical hardware. Using simulation, one can check the working of circuits, debug errors in code, and understand system behavior without the risk of damaging components. Proteus is particularly favored for simulating Arduino-based embedded projects because it provides a library of common microcontrollers, sensors, and display modules.

In this project, we will interface the DS1307 RTC module with Arduino and display real-time day, date, and time on a 20x4 LCD. The project covers step-by-step instructions on building the circuit in Proteus, programming the Arduino, and simulating the system to understand its working thoroughly.

What Is an RTC Module (DS1307)?

A Real-Time Clock (RTC) module is an electronic component used to keep track of accurate time and date. Unlike general-purpose microcontrollers that lose time when powered off, RTC modules can maintain time continuously using a dedicated crystal oscillator and a backup battery.

The DS1307 RTC module is widely used in embedded applications due to its simplicity and reliability. It communicates with microcontrollers like Arduino using the I2C protocol, which uses only two data lines: SDA (Serial Data) and SCL (Serial Clock).

The module can track the following time and date parameters:

  • Seconds
  • Minutes
  • Hours
  • Day of the week
  • Date
  • Month
  • Year

Key Features of DS1307

  • Operates at low voltage (typically 5V)
  • Includes a battery backup to maintain time when the system is powered down
  • Communicates with microcontrollers using the I2C protocol
  • Maintains a 24-hour clock format
  • Has internal registers to store time and calendar information
The DS1307 module ensures that even if Arduino loses power or is reset, it continues to maintain accurate time, making it highly suitable for long-term projects and applications requiring precise timekeeping.
Working Principle of RTC


The DS1307 module uses a 32.768 kHz crystal oscillator to maintain time. This crystal generates a constant frequency, which increments a series of internal counters to track seconds, minutes, hours, days, dates, months, and years.

Arduino communicates with the DS1307 module through the I2C bus. The microcontroller sends a request to the module to read the current time and receives the data stored in the RTC registers. This data can then be displayed on an LCD, sent to other devices, or used to trigger time-based operations.

The presence of a backup battery ensures that the time continues to be tracked even when the main Arduino is powered off. This is critical for projects where the device may be frequently turned off but accurate time data is essential.

Required Libraries

Proteus simulation does not include the DS1307 RTC module by default. Therefore, external libraries are required for both Arduino and Proteus to ensure accurate simulation.


The following libraries are required:

  1. RTC DS1307 library for Proteus – enables the module to be used in the virtual Proteus environment.
  2. Arduino library for Proteus – facilitates I2C communication between Arduino and the RTC module.
  3. LCD library – used to interface and display data on an LCD.

Library Installation Steps

  1. Download the required library files.
  2. Extract the downloaded ZIP or RAR folder.
  3. Copy the .LIB and .IDX files for Proteus.
  4. Paste these files into the Library folder of the Proteus installation directory.
  5. Restart Proteus.

After following these steps, the DS1307 RTC module will be available in the component library for use in circuit design.
Circuit Design in Proteus (Step-by-Step)

This section explains step-by-step how to build the RTC project in Proteus.

Step 1: Add Arduino Uno

  1. Open Proteus Design Suite.
  2. Click Pick Devices (P).
  3. Search for Arduino Uno.
  4. Select and place the Arduino on the workspace.
Step 2: Add DS1307 RTC Module

  1. Click Pick Devices.
  2. Search for DS1307 RTC Module (ensure library is installed). 
  3. Place it on working area.
Step 3: Add 20x4 LCD Display

  1. Click Pick Devices.
  2. Search for LM044L (20x4 LCD
  3. Place it next to the RTC and Arduino.
Step 4: Add Potentiometer

  1. Click Pick Devices.
  2. Search for POT-HG.
  3. Place it near the LCD.
  4. This will allow contrast adjustment for the display.
Step 5: Add Power and Ground

  1. Select Terminal Mode in Proteus.
  2. Add 5V and GND.
  3. Place them appropriately in the workspace.
Make sure all grounds (GND) are common to avoid simulation errors.

Making Connections in Proteus

  1. Connect VCC of RTC to 5V, GND to GND.
  2. Connect SDA to A4, SCL to A5 on Arduino. 
  3. Connect LCD pins as per the table above. 
  4. Connect potentiometer sides to 5V and GND, middle pin to LCD VO.
  5. Ensure all wires are neatly routed to avoid confusion during simulation.
When correctly connected, the circuit is ready for simulation. When converting it to hardware, it's recommended to use connectors, i.e., Molex connectors etc., for interfacing these components.

Generating Arduino Code for RTC

Now that the circuit is designed, the next step is writing the Arduino code to interface with the RTC module and display the time on LCD. 

  • Open the new project in Arduino IDE.
  • Paste the code there:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup() {
 lcd.begin(20, 4);
 Wire.begin();

 if (!rtc.begin()) {
   lcd.setCursor(0, 0);
   lcd.print("RTC NOT FOUND!");
   lcd.setCursor(0, 1);
   lcd.print("Check A4 & A5 Pins");
   while (1);
 }

 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

 lcd.clear();
 lcd.setCursor(2, 0);
 lcd.print("REAL TIME CLOCK");
 delay(2000);
 lcd.clear();
}

void loop() {
 DateTime now = rtc.now();

 lcd.setCursor(0, 0);
 lcd.print("Day : ");
 lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
 lcd.print("      ");

 lcd.setCursor(0, 1);
 lcd.print("Date: ");
 printDigits(now.day());
 lcd.print("/");
 printDigits(now.month());
 lcd.print("/");
 lcd.print(now.year());

 lcd.setCursor(0, 2);
 lcd.print("Time: ");
 printDigits(now.hour());
 lcd.print(":");
 printDigits(now.minute());
 lcd.print(":");
 printDigits(now.second());

 delay(500);
}

void printDigits(int number) {
 if (number < 10) lcd.print('0');
 lcd.print(number);
}
  • Hit the run button and wait for the loading. 
  • Copy the hex file address:
  • Got to the circuit and click on Arduino.
  • Paste the file address here:
  • Click OK.
  • Run the circuit to test.

Arduino Code Explanation


The Arduino code is the heart of this project. It ensures that the RTC module communicates properly with the Arduino, retrieves accurate date and time data, and displays it neatly on a 20x4 LCD. Below is a detailed explanation of each section of the code:

1. Library Inclusion

#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>
  • Wire.h library allows Arduino to communicate with I2C devices, such as the DS1307 RTC.
  • LiquidCrystal.h is used to interface the LCD with Arduino.
  • RTClib.h is a dedicated library for working with the DS1307 module and simplifies reading and writing time and date information.
These libraries are essential for the correct functioning of the project.

2. LCD and RTC Initialization

RTC_DS1307 rtc;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

  • The RTC_DS1307 rtc; object creates an interface to communicate with the RTC module.
  • LiquidCrystal lcd(...) sets the Arduino pins connected to the LCD (RS, E, D4-D7).
  • Using a 4-bit mode reduces the number of pins needed on Arduino while still providing full functionality of the LCD.

3. Array for Days of the Week

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

  • This array helps display the day name instead of a numeric value.
  • The dayOfTheWeek() function from RTClib returns a number (0–6), which is then mapped to a day string from this array.


4. Setup Function

void setup() {
 lcd.begin(20, 4);
 Wire.begin();

 if (!rtc.begin()) {
   lcd.setCursor(0, 0);
   lcd.print("RTC NOT FOUND!");
   lcd.setCursor(0, 1);
   lcd.print("Check A4 & A5 Pins");
   while (1);
 }

 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

 lcd.clear();
 lcd.setCursor(2, 0);
 lcd.print("REAL TIME CLOCK");
 delay(2000);
 lcd.clear();
}

  • lcd.begin(20, 4);
    initializes the LCD with 20 columns and 4 rows.
  • Wire.begin(); starts I2C communication.
  • RTC check: If the RTC module is not detected, the code displays an error and stops execution (while(1);).
  • rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); sets the RTC time to match the time when the Arduino code is compiled. This is optional if you want the RTC to maintain time independently.

5. Loop Function

void loop() {
 DateTime now = rtc.now();

 lcd.setCursor(0, 0);
 lcd.print("Day : ");
 lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
 lcd.print("      ");

 lcd.setCursor(0, 1);
 lcd.print("Date: ");
 printDigits(now.day());
 lcd.print("/");
 printDigits(now.month());
 lcd.print("/");
 lcd.print(now.year());

 lcd.setCursor(0, 2);
 lcd.print("Time: ");
 printDigits(now.hour());
 lcd.print(":");
 printDigits(now.minute());
 lcd.print(":");
 printDigits(now.second());

 delay(500);
}
  • DateTime now = rtc.now(); retrieves the current time and date from the RTC module.
  • The LCD displays:
    • Day of the week on row 0
    • Date on row 1 in DD/MM/YYYY format
    • Time on row 2 in HH:MM:SS format
  • delay(500); updates the LCD every 0.5 seconds to provide near-real-time information.


6. Helper Function for Formatting Digits

void printDigits(int number) {
 if (number < 10) lcd.print('0');
 lcd.print(number);
}
  • Ensures single-digit numbers are prefixed with 0 for proper time formatting.
  • For example, 7:5:3 becomes 07:05:03 on the LCD.

Generating the HEX File for Proteus Simulation


Proteus requires the HEX file (compiled Arduino code) to simulate the Arduino. Follow these steps:

Step 1: Open Arduino IDE

  • Open Arduino IDE and paste the RTC code above.

Step 2: Verify the Code

  • Click the Verify (✔) button to compile the code.
  • Ensure there are no errors.

Step 3: Enable Verbose Output

  • Go to File → Preferences
  • Enable Show verbose output during compilation
  • Click OK.

This allows you to see the path of the generated HEX file.

Step 4: Locate the HEX File

  • Compile the code again.
  • Scroll the output console to find a line ending with .hex.
  • This is your compiled Arduino program ready for Proteus simulation.

Step 5: Load HEX File in Proteus

  1. Double-click on Arduino in Proteus.
  2. In the Arduino properties, paste the path to your HEX file.
  3. Click OK.

Step 6: Simulate the Circuit

  • Click Play in Proteus.
  • Adjust the potentiometer to see the LCD contrast change.
  • The LCD will display the day, date, and time in real-time.

Working Principle of RTC Module


  1. Timekeeping by Crystal Oscillator
    • The DS1307 uses a 32.768 kHz crystal oscillator for accurate time measurement.
  2. I2C Communication
    • Arduino communicates with the RTC using SDA and SCL lines.
  3. Reading Time
    • Arduino sends a request to the RTC.
    • The RTC responds with current seconds, minutes, hours, date, month, year, and day of the week.
  4. Battery Backup
    • Even if Arduino is powered off, the RTC keeps time using a CR2032 battery.
  5. Displaying Time
    • Arduino converts raw data to readable format.
    • The formatted data is displayed on a 20x4 LCD.

This ensures continuous, reliable, and accurate timekeeping for embedded systems.


Applications of DS1307 RTC Module


RTC modules are versatile and used in numerous applications in both consumer and industrial systems. Here is a detailed breakdown:

1. Digital Clocks and Wall Timers

  • Simple digital clocks use DS1307 for accurate time display.
  • In wall timers, RTC ensures correct switching of devices at scheduled times.

2. Data Logging Systems

  • RTC is crucial in data loggers for industrial or laboratory use.
  • Records date and time of sensor measurements.
  • Enables tracking environmental parameters over time.
3. Alarm Systems

  • Home or industrial alarms depend on precise time triggers.
  • RTC modules ensure alarms are activated at the correct time.

4. Time-based Automation

  • In smart irrigation, RTC triggers pumps at set times.
  • In lighting automation, RTC turns lights on/off based on time.

5. Attendance Systems

  • RTC provides accurate timestamps for employee attendance.
  • Integrated with RFID or biometric systems for reliable time logging.

6. Scheduled Industrial Processes

  • RTC modules are used in factories for automation schedules.
  • Machines start and stop at precise times without human intervention.

7. Consumer Electronics

  • Microwave ovens, washing machines, and coffee makers use RTC for scheduling operations.

8. Event Logging in Embedded Systems

  • IoT devices use RTC to timestamp events.
  • For instance, smart sensors log temperature, humidity, or motion with exact timestamps.

9. Security Systems

  • Surveillance cameras use RTC to timestamp footage.
  • Ensures proper time tracking for recording and reviewing security events.

10. School and College Timetable Automation

  • RTC can trigger bells, displays, or announcements according to a schedule.