Interfacing DS3231 Precision RTC Module with Arduino

2,320 views

Introduction

RTC is the abbreviation for Real-Time Clock. RTC modules are TIME and DATE remembrance systems with a battery arrangement that allows the module to operate without external power. As a result, the TIME and DATE are maintained current. The RTC module can provide us with the exact Moment and DATE at any time. RTC modules come in a variety of forms. In this tutorial, we will interface “DS3231 Precision RTC Module with Arduino UNO”.

DS3231 RTC Module

The DS3231 is a six-terminal device with two pins that are not required to be used. So we’ve got four pins in all. These four pins are located on the other side of the module with the same name.

DS3231-RTC-Real-Time-Clock-Module

Technical Specifications and Features

  • Operating  voltage of  DS3231 MODULE: 2.3V – 5.5V
  • Can operate on LOW voltages
  • Consumes 500nA on battery backup
  • The maximum voltage at SDA, SCL: VCC + 0.3V
  • Operating temperature: -45ºC to +80ºC
  • RTC counts seconds, minutes, hours, and year
  • Accuracy: +2ppm to -2ppm for 0ºC to +40ºC , +3.5ppm to -3.5ppm for -40ºC to +85ºC
  • Digital temperature sensor with ±3ºC accuracy
  • Two Time-of-day alarms
  • Programmable square wave output
  • Register for Aging trim
  • 400Khz I2C interface
  • Low power consumption
  • Automatic power failure battery switch circuitry
  • CR2032 battery backup with two to three-year life
  • Potable size

Pinout

Pin NameDescription
VCCConnected to the positive power source.
GNDConnected to ground.
SDASerial Data pin (I2C interface)
SCLSerial Clock pin (I2C interface)
SQWSquare Wave output pin
32K32K oscillator output

Hardware Required

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Jumper Wires
4.Precision RTC ModuleDS32311
5.Bread Board1

Circuit Diagram

Connection Table

ArduinoDS3231 Precision RTC Module
GNDGND
5VVCC
A4SDA
A5SCL

Arduino Code

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

RTC_DS3231 rtc;

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

void setup () 
{
  Serial.begin(9600);
  delay(3000); // wait for console opening

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
	
	// Comment out below lines once you set the date & time.
    // Following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
	
    // Following line sets the RTC with an explicit date & time
    // for example to set January 27 2017 at 12:56 you would call:
    // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
  }
}

void loop () 
{
    DateTime now = rtc.now();
    
    Serial.println("Current Date & Time: ");
    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.println("Unix Time: ");
    Serial.print("elapsed ");
    Serial.print(now.unixtime());
    Serial.print(" seconds/");
    Serial.print(now.unixtime() / 86400L);
    Serial.println(" days since 1/1/1970");
    
    // calculate a date which is 7 days & 30 seconds into the future
    DateTime future (now + TimeSpan(7,0,0,30));
    
    Serial.println("Future Date & Time (Now + 7days & 30s): ");
    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.println();
    delay(1000);
}

Working Explanation

To Interface DS3231 Precision RTC Module with Arduino, connect the circuit according to the diagram given. Paste the above code into your Arduino. Upload that code. Open the Serial monitor to observe the readings. You will observe, the date, time, and other parameters there

Code Explanation

  • To communicate with the module, the code begins by adding the wire.h and RTClib.h libraries. Then, to hold days information, we construct an RTClib library object rtc and declare the daysOfTheWeek 2D character array.
  • In the void setup, we use different functions. For example, The begin() function checks if the RTC module is connected, while the isrunning() function examines the DS1307’s internal I2C registers to see if the chip has lost time. If this function answers false, we may set the date and time using the adjust() function.
  • In the void loop, We print the values on the serial monitor by using different functions. The function now() returns the current date and time. Its return value is typically saved in a DateTime datatype variable; year() returns the current year; month() returns the current month; day() returns the current day; dayOfTheWeek() returns the current day of the week. This function is often used as an index into a two-dimensional character array that maintains information about days; hour() delivers the current hour; minute() returns the current minute; second() returns the current seconds; and unixtime() returns the current unix time in seconds. Unix time is a way of describing a specific point in time. To add/subtract time from/to the current time, use the TimeSpan() method. Days, hours, minutes, and seconds can all be added or subtracted.

Application and Uses

  • Seconds, minutes, hours, days, dates, months, and years are all recorded by the RTC. As a result, it may be used in digital clocks.