Guide for Using Real Time Clock with Arduino



DS3231 is a low-cost , accurate I2C real-time clock ( RTC ) which keeps track of time. RTCs are present in almost any electronic device which needs to keep accurate time. It should not be confused with hardware clocks which are signals that oscillate between a high and a low state. RTCs have an alternate source of power , so they can continue to keep track of time while the primary source of power is off. This power source is usually a lithium ion battery. My DS3231 has a CR2032 lithium ion battery. These cells typically produce voltages from 1.5V to about 3.7V.


RaspberryPi and the BeagleBone Black do not have a real-time clock which is the reason it is unable to get the current time. You can connect to the internet to get the time from NTP servers. The NTP or the Network Time Protocol is a networking protocol for clock synchronization between computer systems over packet-switched , variable latency data networks ( Confused ? See here : ). The RTC maintains seconds , minutes , hours , day , date , month and year information. You can view the DS3231 datasheet from Here.


How Does it Work ?

The DS3231 is marketed as an extremely accurate I2C – Integrated TCXO. Let us break it down. The Inter-integrated Circuit or the I2C Protocol is a protocol which allows multiple Slave digital ICs to communicate with one or more Master chips. I will write a whole tutorial on SPI , I2C and other communication protocols. For the time being , you can see the wikipedia page from here.


If you look at the backside image of the module ( As shown in the picture ) , it uses two separate ICs on it. The first one is the DS3231 [ the big one ] which is the real-time clock and the second one is the Atmel 24C24N EEPROM which is used by the DS3231 to store the date , time and all other information the clock generates.


The DS3231 chip has an internal oscillator. It uses an integrated temperature-compensated crystal oscillator ( TXCO ) and crystal. The chip has 32kHz crystal integrated into the package with a built-in temperature sensor to periodically measure the temperature of the crystal and adjust the frequency so it remains constant to keep it accurate. What is a crystal oscillator or resonator ?

TXCO is a form of crystal oscillator widely used in frequency control applications due to its frequency stability and low cost. Crystal oscillators are stable but are affected by temperature. A TXCO adjusts the frequency of the oscillator to compensate for the changes that will occur as a result of temperature changes. Now, let us get the module running.


Parts Required

  1.  1 x Arduino UNO
  2.  1 x DS3231
  3.  Jumper Wires

Pin Wiring

Sensor Pin                                   Arduino Pin

  VCC                                            5V
  GND                                           GND
  SDA                                            A4
  SCL                                            A5

Downloading the Library

I’m going ahead with the DS3231 library by Rinky-Dink Electronics which can be downloaded from Here. Add the .zip library from the Arduino IDE to use it ( Go to Sketch > Use Library ).


#include <DS3231.h> // Include the library

DS3231  rtc(SDA, SCL); // SDA and SCL are the data and clock lines

void setup()

  Serial.begin(115200); // Baud rate : 115200
  rtc.begin(); // Initialize the rtc object
  // Parameters to set
  rtc.setDOW(SUNDAY);          // Set day of the week
  rtc.setTime(18, 03, 12);     // Set the time (24hr format)
  rtc.setDate(27, 8, 2017);    // Set the date/month/year

void loop()

  // Send Day-of-Week
  Serial.print(" ");
  // Send date
  Serial.print(" -- ");

  // Send time
  delay (1000); // 1 sec delay



Final Comments

Thank you for visiting the website and I would love see your progress and developments with the module. In case of any doubt , feel free to contact me.

Saumitra Kapoor

Leave a Reply