Interface DS3231 RTC Module with Arduino – Set/Read Date and Time

DS3231 RTC module is a famous real-time clock in the market for its accurate results. Here, RTC stands for real-time clock. It is an affordable system that manages Time and Date with high precision. This small-sized module has six pins, an inter-integrated (I2C) interface for data transmission, and a battery cell for power-backup. DS3231 Real-time Module comes with a TWI interface, two alarm clocks, and an inbuilt temperature-compensated crystal Oscillator. RTC module finds its applications in consumer products to the industrial level.

DS3231 RTC Module

This tutorial will discuss pin configuration, specifications, features, interfacing with Arduino, and applications.

DS3231 RTC Module Pinout

The following figure shows the pinout diagram of the DS3231 RTC  Module:

DS3231 RTC Module pinout diagram

 Pin Configuration

Let us discuss the pinout of the DS3231 Real-time Module. The pin configuration detail in tabular is mentioned below:

Pin NameFunction
VCCPower Supply pin
GNDGround pin
SQWSquare-wave Output pin
SCLSerial Clock pin
SDASerial Data pin
32K32KHz Open-drain Output pin
  • SQW: It is used to generate square wave or to be used as an interrupt for the alarms
  • SCL: A serial clock line used to synchronize data transmissions. It is a part of the I2C interface.
  • SDA: A serial data transmission line. It is a part of the I2C interface.
  • 32K: This pin is used for a reference clock.

DS3231 RTC Module Features and Specifications

  • Operating Voltage: 2.3 – 5.5 Volts
  • Operating Temperature: -45 – 800C
  • Maximum Voltage: VCC+0.3 Volts
  • Battery Backup Current: 500 mA
  • Accuracy at -40 – 800C: ±3.5 ppm
  • Accuracy at 0 – 400C: ± 2.0 ppm
  • Temperature Accuracy: 30C
  • Package-Type: 16, 300-mil SO package

Detailed Features

Some of the detailed features are listed below:

  • Can function with low voltages
  • A programmable Square-wave output as per requirement
  • A battery backup to stay updated even if there is no power
  • A dual-directional, 400 kHz of I2C interface for speedy transmission
  • 32 bytes of EEPROM for to read/write or to save data
  • 2 Time-of-day alarm clocks
  • A pushbutton to reset time
  • RTC can be used either in 12hrs or 24hrs format
  • An aging trim register to set a user-provided value as an offset with reference to the factory value
  • Maintains seconds, minutes, hours, days, weeks, months, years information
  • Switches automatically from a power source to an in-built battery source

DS3231 RTC Schematic Diagram

The schematic of the module is provided to understand the connections which may turn helpful while repairing the module:

DS3231 RTC Module internal circuit diagram

How to interface DS3231 RTC Module with a microcontroller?

The interfacing is straightforward. Just power-up(not more than 5 Volts to avoid damage) the RTC either through a power supply or through the controller and then wire the SCL to the SCL and SDA to the SDA of the module and the microcontroller, respectively. The transmission takes place through synchronous inter-integrated circuit (I2C) protocol, which is quite complex.

The libraries written for the RTC module can help to ease up things. If they are included in the program, they will automatically provide the date and time to the user. Furthermore, these libraries can help us to change or set the alarm clock.

The module comes with a battery in case there is a power-out situation. It will help the module to stay updated with the date and time.

DS3231 RTC module coin cell holder

Interfacing DS3231 RTC with Arduino

As explained earlier, we will interface an Arduino UNO with RTC as an example:

DS3231 RTC Module interfacing with Arduino

Connect the pins accordingly.

Arduino UNORTC Module
5VVCC
GNDGND
ICSP2 HeaderSDA
ICSP2 HeaderSCL

Install DS3231 RTC Library in Arduino IDE

To program a DS3231 RTC module, we need to use I2C communication pins of Arduino. If you don’t know about I2C communication, you can check these articles:

But it requires an effort to write a code from scratch. Fortunately, an Arduino RTClib library is available which provides callback functions to take time measurements from an RTC module. This library hides all the complexity to communicate with an RTC module over I2C communication. We can use simple callback functions implemented by RTClib to read data from DS3231. 

Therefore, to program a DS3231 RTC module with Arduino, you will use the RTClib library available in Arduino IDE. 

To use this library, first, we need to install this library in Arduino IDE by going to Arduino’s library manager. Open Arduino IDE, go to Tools > Manage Libraries.

Install DS3231 library in Arduino IDE

After that, this window library manager window will appear. Search for “RTClib” by typing in the search bar. You will see many options for DS3231. But the one that we will use in this tutorial is RTClib by Adafruit. Select RTClib by Adafruit and click on the install button. 

Install DS3231 RTC module library in Arduino IDE

When you click on the install button, you may get a message that RTClib may require other libraries dependencies which is currently not installed in your Arduino IDE. We recommend you to install all dependencies by clicking on install button. Otherwise, RTClib will not work properly.

DS3231 library software dependency libraries

Arduino Code

This example code performs time settings. Also, this Arduino sketch read time and data from DS3231 RTC module and prints in on Arduino serial monitor.

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

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

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

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

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(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");

    // calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));

    Serial.print(" now + 7d + 12h + 30m + 6s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();

    Serial.print("Temperature: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C");

    Serial.println();
    delay(3000);
}

The results can be observed through the Serial Monitor. It will display the updated time and date.

DS3231 Alternative Options

  • DS1302
  • DS1307

Applications

  • DIY projects
  • Global Positioning System
  • Telematics
  • Servers
  • Embedded Systems
  • Mobiles

2D Diagram

The following figure depicts the 2d model of the DS3231 Real-Time Module. It shows us the physical dimensions required when a PCB card is designed.

2D diagram

Related Articles:

Leave a Comment