Getting Started with DS3231 RTC Module - Interface DS3231 Module with Arduino Uno

Getting Started with DS3231 RTC Module - Interface DS3231 Module with Arduino Uno

0 comments / Posted on by Quartz Components

Getting Started with DS3231 RTC Module - Interface DS3231 Module with Arduino Uno

DS3231 is a low-cost Real-Time Clock IC. It is highly accurate and can precisely store “Hours, Minutes and Seconds along with Day, Date, Month and year”

You may ask, what is the need of an external module for managing time when Arduino itself has a clock built-in? Well, the clock inside Arduino resets every time you upload a new code or disconnect the power. But, DS3231 comes with a CR2032 3V lithium cell through which it keeps the time on track without power too.

 

DS3231 RTC Module Features

DS3231 Module comes with a load of features. For starters, it is very low cost and anyone looking for cheap RTC Modules may go for it. It can store and maintain, Time including Hours, Minutes, and Seconds. It can also store the day of the week, date, month, and year. Also, it has automatic compensation for leap-years and months with fewer than 31 days.

Features Of DS3231 RTC Module

As the module works with 3.3V as well as 5V input it becomes compatible with most of the development boards available in the market. The battery though is a 3V one and can maintain the information for more than a year without power.

 

Arduino DS3231 RTC Module Circuit Connections

As DS3231 uses the I2C communication protocol, it becomes very easy to wire up with Arduino. For connecting the module only four pins need to be connected which are, VCC and GND for power along with SDA and SCL for communication.

The connections to be done are as follows:- 

Arduino DS3231 RTC Module Circuit Connections

DS3231 ---------> Arduino

VCC ---------> 5V

GND ---------> GND

SCL ---------> SCL or A5

SDA ---------> SDA or A4

 

Getting started: Installing Libraries

Before moving into the coding part we need to install the DS3231 library in order to make our coding process easy and simple. We will be installing the DS3231.h library as it comes with a bundle of pre-written statements to make the coding part easier for us.

For downloading the library you can visit this link:

http://www.rinkydinkelectronics.com/library.php?id=73 

 

After downloading the library, unzip it and place it in the following directory.

C:\Users\<UserName>\Documents\Arduino\libraries

Library Installation

After you have installed the library. It is now time to open Arduino IDE and start coding.

 

Arduino DS3231 RTC module Coding

The DS3231 library already comes with several examples to get you started. The code we will be using is:

File > Examples > DS3231 > Arduino > DS3231_Serial_Easy

Library Of DS3231

You’ll see this code is just for printing the date and time in the Serial monitor. But before that, we need to set the time and date. 

 For doing so just uncomment the following lines in the code.

 // The following lines can be uncommented to set the date and time

 //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY

 //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)

  //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014

 

After uncommenting set the date and time properly and upload the code. Now again comment those lines and upload the code.

The re-upload is to stop the RTC from getting reset every time it is powered on.

 

Getting started: Testing

Once you have uploaded the sketch. Open the serial monitor you will see the date, time, and day of the week getting printed there.

Testing On Serial Monitor

This ensures the working of the module and now we will try to make a clock using it.

 

Arduino DS3231 RTC Based Digital Clock

So now we will be connecting one 16x2 LCD in the circuit and will print the date, time, and day of the week in it. 

Testing Of DS3231

 

For that first do the connections as follows:

Arduino DS3231 RTC Clock Circuit Diagram

Arduino DS3231 RTC Clock

 After making the connections. Let us jump to the coding part.

 

Here is the complete code for the project :-

 

Code:

#include <DS3231.h>

#include <LiquidCrystal_I2C.h>

DS3231 rtc(SDA, SCL);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

void setup() {

  // put your setup code here, to run once:

lcd.begin(16,2);

lcd.backlight();

rtc.begin();

lcd.clear();

lcd.setCursor(2,0);

lcd.print("Designed by");

lcd.setCursor(2,1);

lcd.print("Quartz Comp.");

delay(3000);

}

 

void loop() {

  // put your main code here, to run repeatedly:

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Time:-");

lcd.setCursor(4,1);

lcd.print(rtc.getTimeStr());

delay(3000);

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Date:-");

lcd.setCursor(3,1);

lcd.print(rtc.getDateStr());

delay(2000);

lcd.clear();

lcd.setCursor(6,0);

lcd.print("DOW:-");

lcd.setCursor(5,1);

lcd.print(rtc.getDOWStr());

delay(2000);

}

 

The code is fairly simple to understand.

Firstly, we are including the libraries and initiating the RTC module as well as the I2C display.

#include <DS3231.h>

#include <LiquidCrystal_I2C.h>

DS3231 rtc(SDA, SCL);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

 

After this in the setup part we are initializing the RTC and display and printing some messages.

void setup() {

  // put your setup code here, to run once:

lcd.begin(16,2);

lcd.backlight();

rtc.begin();

lcd.clear();

lcd.setCursor(2,0);

lcd.print("Designed by");

lcd.setCursor(2,1);

lcd.print("Quartz Comp.");

delay(3000);

}

 

In the loop section of the code we are fetching the Date, Time and DOW string from the RTC module and printing that to the LCD panel.

void loop() {

  // put your main code here, to run repeatedly:

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Time:-");

lcd.setCursor(4,1);

lcd.print(rtc.getTimeStr());

delay(3000);

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Date:-");

lcd.setCursor(3,1);

lcd.print(rtc.getDateStr());

delay(2000);

lcd.clear();

lcd.setCursor(6,0);

lcd.print("DOW:-");

lcd.setCursor(5,1);

lcd.print(rtc.getDOWStr());

delay(2000);

}

 

That’s pretty much about this project. Now just upload the code and you will see the Time, Date, and day of the week getting printed in the LCD.

Arduino DS3231 RTC Testing

Video

DS3231 is a low-cost Real-Time Clock IC. It is highly accurate and can precisely store “Hours, Minutes and Seconds along with Day, Date, Month and year”

You may ask, what is the need of an external module for managing time when Arduino itself has a clock built-in? Well, the clock inside Arduino resets every time you upload a new code or disconnect the power. But, DS3231 comes with a CR2032 3V lithium cell through which it keeps the time on track without power too.

 

DS3231 RTC Module Features

DS3231 Module comes with a load of features. For starters, it is very low cost and anyone looking for cheap RTC Modules may go for it. It can store and maintain, Time including Hours, Minutes, and Seconds. It can also store the day of the week, date, month, and year. Also, it has automatic compensation for leap-years and months with fewer than 31 days.

Features Of DS3231 RTC Module

As the module works with 3.3V as well as 5V input it becomes compatible with most of the development boards available in the market. The battery though is a 3V one and can maintain the information for more than a year without power.

 

Arduino DS3231 RTC Module Circuit Connections

As DS3231 uses the I2C communication protocol, it becomes very easy to wire up with Arduino. For connecting the module only four pins need to be connected which are, VCC and GND for power along with SDA and SCL for communication.

The connections to be done are as follows:- 

Arduino DS3231 RTC Module Circuit Connections

DS3231 ---------> Arduino

VCC ---------> 5V

GND ---------> GND

SCL ---------> SCL or A5

SDA ---------> SDA or A4

 

Getting started: Installing Libraries

Before moving into the coding part we need to install the DS3231 library in order to make our coding process easy and simple. We will be installing the DS3231.h library as it comes with a bundle of pre-written statements to make the coding part easier for us.

For downloading the library you can visit this link:

http://www.rinkydinkelectronics.com/library.php?id=73 

 

After downloading the library, unzip it and place it in the following directory.

C:\Users\<UserName>\Documents\Arduino\libraries

Library Installation

After you have installed the library. It is now time to open Arduino IDE and start coding.

 

Arduino DS3231 RTC module Coding

The DS3231 library already comes with several examples to get you started. The code we will be using is:

File > Examples > DS3231 > Arduino > DS3231_Serial_Easy

Library Of DS3231

You’ll see this code is just for printing the date and time in the Serial monitor. But before that, we need to set the time and date. 

 For doing so just uncomment the following lines in the code.

 // The following lines can be uncommented to set the date and time

 //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY

 //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)

  //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014

 

After uncommenting set the date and time properly and upload the code. Now again comment those lines and upload the code.

The re-upload is to stop the RTC from getting reset every time it is powered on.

 

Getting started: Testing

Once you have uploaded the sketch. Open the serial monitor you will see the date, time, and day of the week getting printed there.

Testing On Serial Monitor

This ensures the working of the module and now we will try to make a clock using it.

 

Arduino DS3231 RTC Based Digital Clock

So now we will be connecting one 16x2 LCD in the circuit and will print the date, time, and day of the week in it. 

Testing Of DS3231

 

For that first do the connections as follows:

Arduino DS3231 RTC Clock Circuit Diagram

Arduino DS3231 RTC Clock

 After making the connections. Let us jump to the coding part.

 

Here is the complete code for the project :-

 

Code:

#include <DS3231.h>

#include <LiquidCrystal_I2C.h>

DS3231 rtc(SDA, SCL);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

void setup() {

  // put your setup code here, to run once:

lcd.begin(16,2);

lcd.backlight();

rtc.begin();

lcd.clear();

lcd.setCursor(2,0);

lcd.print("Designed by");

lcd.setCursor(2,1);

lcd.print("Quartz Comp.");

delay(3000);

}

 

void loop() {

  // put your main code here, to run repeatedly:

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Time:-");

lcd.setCursor(4,1);

lcd.print(rtc.getTimeStr());

delay(3000);

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Date:-");

lcd.setCursor(3,1);

lcd.print(rtc.getDateStr());

delay(2000);

lcd.clear();

lcd.setCursor(6,0);

lcd.print("DOW:-");

lcd.setCursor(5,1);

lcd.print(rtc.getDOWStr());

delay(2000);

}

 

The code is fairly simple to understand.

Firstly, we are including the libraries and initiating the RTC module as well as the I2C display.

#include <DS3231.h>

#include <LiquidCrystal_I2C.h>

DS3231 rtc(SDA, SCL);

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

 

After this in the setup part we are initializing the RTC and display and printing some messages.

void setup() {

  // put your setup code here, to run once:

lcd.begin(16,2);

lcd.backlight();

rtc.begin();

lcd.clear();

lcd.setCursor(2,0);

lcd.print("Designed by");

lcd.setCursor(2,1);

lcd.print("Quartz Comp.");

delay(3000);

}

 

In the loop section of the code we are fetching the Date, Time and DOW string from the RTC module and printing that to the LCD panel.

void loop() {

  // put your main code here, to run repeatedly:

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Time:-");

lcd.setCursor(4,1);

lcd.print(rtc.getTimeStr());

delay(3000);

lcd.clear();

lcd.setCursor(5,0);

lcd.print("Date:-");

lcd.setCursor(3,1);

lcd.print(rtc.getDateStr());

delay(2000);

lcd.clear();

lcd.setCursor(6,0);

lcd.print("DOW:-");

lcd.setCursor(5,1);

lcd.print(rtc.getDOWStr());

delay(2000);

}

 

That’s pretty much about this project. Now just upload the code and you will see the Time, Date, and day of the week getting printed in the LCD.

Arduino DS3231 RTC Testing

Video

0 comments

Leave a comment

All blog comments are checked prior to publishing