How to Play with RTC Modules

The following article is a simple guide lets you get time and calendar functions with a real time clock chip and microcontroller. Hopefully this entryway can help you to walk through your own project ideas that keeps track of the current date and time. Let’s start!

The key hardware, apart from an Arduino Uno, is a Real-Time Clock (RTC) “Tiny RTC DS1307” module. This tiny module embraces DS1307 RTC chip and AT24C32 Memory chip connected to I2C bus. The little module, shipped with an onboard CR2032 backup battery, also provides an option to mount one DS18B20 temperature sensor chip.

DS1307 RTC Module

Following is the official schematic of the Tiny RTC DS1307 module.

Tiny RTC DS1307 Module Schematic

See below the hardware setup diagram for a quick start.

RTC DS1307 Arduino-Hardware Layout

Now pay close attention to the code i.e. the so called Arduino Sketch. Before you begin, you will need to download and install a special Arduino Library “DS1307” prepared by Watterott. If you are not familiar adding new libraries to your Arduino IDE, just follow this link to learn the trick – http://arduino.cc/en/Guide/Libraries. Once the library has been added successfully, open your Arduino IDE to copy-paste and upload the given code.

/*
  DS1307 RTC (Real-Time-Clock) Example Code
  Arduino Uno I2C:  A4 (SDA), A5 (SCL)
  Based on DS1307 RTC Lib for Arduino
  by Watterott electronic (www.watterott.com)
  T.K.Hareendran/2019
  Published by Codrey Electronics

 */

#include <Wire.h> // I2C Library
#include <DS1307.h> // RTC Library


DS1307 rtc;


void setup()
{
  /*Init Serial port*/
  Serial.begin(9600);
 
  /*Init RTC*/
  Serial.println("Initializing RTC...");

  /*Only set the date & time once - See comment/uncomment note in the article*/
  //rtc.set(0, 49, 18, 22, 12, 2018); /*SEC, MM, HH, DD, MM, YYYY*/

  /*Stop/pause RTC*/
  //rtc.stop();

  /*Start RTC*/
  rtc.start();
}


void loop()
{
  uint8_t sec, min, hour, day, month;
  uint16_t year;

  /*Get time from RTC*/
  rtc.get(&sec, &min, &hour, &day, &month, &year);

  /*Serial output*/
  Serial.print("\nTime: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(min, DEC);
  Serial.print(":");
  Serial.print(sec, DEC);

  Serial.print("\nDate: ");
  Serial.print(day, DEC);
  Serial.print(".");
  Serial.print(month, DEC);
  Serial.print(".");
  Serial.print(year, DEC);

  /*Wait a second*/
  delay(1000);
}

Remember, in this code it’s needed to set the correct date and time only once. After uploading the compiled code, comment the “rtc.set” line, and upload the updated code once more to complete the process. If this is not done (as observed by me) incase of a reset/power cycle, the date and time you set will be set over and over again and you will not be able to read the correct time and date. Now open the serial monitor to see the output. Here is a screenshot from my computer.

RTC DS1307 Arduino Serial Monitor

If you succeeded in getting this experiment to run, now it’s the best time to build a full-fledged digital alarm clock. Sadly, it’s going beyond the scope of this article but you can see such a fantastic project later!

RTC DS1307 Arduino - Setup

RTC DS1307 Arduino - Setup
Author’s Test Setup

Adding Temperature Readout

As described, you can attach one DS18B20 temperature to the vacant pads of the tinyRTC DS1307 module. Output of DS18B20 is then available through the “DS” header-pin of the module. The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a microcontroller.

DS18B20 Add On

Here you can find the article by Ronny Simon, packed with an experimental code based on the RTClib Library by Jean-Claude Wippler.                                                                       http://blog.simtronyx.de/en/tiny-rtc-ds1307-real-time-clock-with-an-additional-temperature-sensor-ds18b20/

Leave a Reply

Your email address will not be published. Required fields are marked *