Introduction

One of the essential aspects of many Arduino projects is time management. Whether you want to create an alarm clock, a data logger or any other device requiring precise time synchronization, using an RTC (Real Time Clock) module is the ideal solution.
In this course, we’ll explore the RTC module for Arduino and explain how it can improve the precision and reliability of your projects.

DS1307 module pins

GND: Connect to Arduino board ground

5V: Connect to 5V on Arduino board

SCL: To be connected to pin A5 of the analog part

SDA : Connect to pin A4 on Arduino board

What is an RTC module and how does it work?

An RTC module is an electronic circuit incorporating a precise oscillator and non-volatile memory to maintain time and date, even in the event of a power failure. It is designed to provide your Arduino with a stable and accurate time reference.
The RTC module communicates with the Arduino via a communication interface, such as the I2C or SPI protocol, enabling the Arduino to read and write the time and date.
You’ll need a 3.3V battery to put in your rtc module so that it works when it’s no longer powered by the Arduino board. When the Arduino board is undervoltage, it powers the rtc module, so it doesn’t wear out the battery.

Advantage of the Real time Clock for your Arduino Project

  1. Greater precision

Unlike using the Arduino’s internal clock function, which can drift over time, an RTC module uses a high-precision external oscillator to guarantee precise time measurement.

2 Keeping time in the event of a power cut

Thanks to its non-volatile memory, the RTC module retains the time and date even when the power supply is cut, eliminating the need to reset the time each time the Arduino is started up.

3 Low power consumption

The RTC module generally uses very little power, making it ideal for battery-powered or stand-alone projects.

Electronic schematic

Here’s how to connect an RTC module. We use the i2c on the Arduino board:

Programming

To program the RTC module, you’ll need to install the RTClib library. To do this on Arduino ide, type the name RTClib in the search bar:

Once the library has been installed, you can download the program to the Arduino board:

#include "RTClib.h"
 
RTC_DS1307 rtc;
 
void setup () {
  Serial.begin(9600);

  while (! rtc.begin()) {
    Serial.println("Waiting for RTC module...");
    delay(1000);
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

}
 
void loop () {
    DateTime now = rtc.now();
    char heure[10];
    Serial.print("RTC module clock : ");
    sprintf(heure, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
    Serial.println(heure);
     
    delay(1000);
}

Here’s the result on the Arduino ide serial monitor:

Here is the program to change the date of the RTC module:

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 rtc;

void setup() {
  Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  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(F(__DATE__), F(__TIME__)));
  }

  // Uncomment the following line to set the time manually
  // rtc.adjust(DateTime(YYYY, MM, DD, HH, MM, SS)); // YYYY-MM-DD HH:MM:SS
}

void loop() {
  // Set the time here
  DateTime now = DateTime(2024, 3, 22, 12, 30, 0); // YYYY-MM-DD HH:MM:SS

  rtc.adjust(now);

  Serial.println("Time successfully set!");
  delay(1000);
}

Conclusion

The RTC module is a valuable addition to any Arduino project requiring an accurate time reference. Whether you want to create an alarm clock, a data logger or a timing system, using an RTC module guarantees a reliable clock and precise time management.

Installation and operation are relatively simple, thanks to the software libraries available. By integrating an RTC module into your Arduino projects, you’ll benefit from precise time synchronization and enhanced functionality.