Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this article, we are going to interface the real time clock module DS3231 with the Arduino and we will make a Arduino real time clock and temperature monitor using this module. The DS3231 is a highly accurate and a low cost module which keeps track of the day, time and date. The DS3231 module also measures the temperature.

The Arduino itself has a time keeping function by using which we can keep track of time but the reason to use the DS3231 module is that it runs on battery and can keep track of time even when the module is not powered.

Also Read

I made a Alarm device using DS3231. Have a look at it | Arduino Alarm Clock

Required Components for Arduino Real time Clock

The components required for this project are as follows

  • Arduino Uno
  • 16 X 2 LCD
  • DS3231 Real Time Clock (RTC) module
  • 10k potentiometer
  • 220 ohm resistor
  • Connecting wires
  • Breadboard

Showing DS2331 Data on Serial Monitor

First, we are going to display the DS3231 data on the Serial monitor. To do it, make the connections as shown in the below diagram.

Arduino real time clock

Then download the library of DS3231 module, go to the following link and download it from there

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

Code

//Including library for DS3231
#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup() {
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // 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

  Serial.println("DAY           DATE            TIME         Temperature");
}

void loop() {
  
  // Show Day
  Serial.print(rtc.getDOWStr());
  Serial.print("    ");
  
  // Show Date
  Serial.print(rtc.getDateStr());
  Serial.print("     ");

  // Show Time
  Serial.print(rtc.getTimeStr());
  Serial.print("       ");

  // Show Temperature
  Serial.print(rtc.getTemp());
  Serial.println(" C");
  // Wait one second before repeating ūüôā
  delay (1000);
}

Show DS3231 Data on LCD

Now we are going to show the data of DS3231 on the 16X2 LCD. The connections of the LCD module with the Arduino are as follows

  • Connect pin 1 of LCD (VSS) to the ground of Arduino.
  • Connect pin 2 of LCD (VDD) to the 5V of Arduino.
  • Connect pin 3 of LCD (V0) to the middle pin of 10K potentiometer. Connect other two pins of the LCD to the 5V and the ground. The potentiometer will control the contrast of LCD. Potentiometers of other values will work too.
  • Connect pin 4 of LCD (RS) to the digital pin 7 of Arduino.
  • Connect pin 5 of LCD (R/W) to the ground of Arduino. This will put the LCD in read mode.
  • Connect pin 6 of LCD (E) to the pin 6 of Arduino.
  • Connect the data pins (D4-D7) to the pin 5, 4, 3, 2 of Arduino.

The next two pins are for the back light of LCD.

  • Connect the pin 15 (A) to the 5V pin through the 220 ohm resistor. This is the positive pin of back light.
  • Connect the pin 16 (K) to the ground of Arduino. This is the negative pin of backlight.

If you want to read more about the interfacing of LCD module with Arduino, then read this Tutorial | Arduino LCD Interfacing

After making the connections of LCD with Arduino, connect the DS3231 RTC module with Arduino. The DS3231 works with Arduino through the I2C communication which makes the connections of DS3231 with Arduino very easier.

The connections of DS3231 module with Arduino are as follows

  • Connect the GND of DS3231 to the GND of Arduino.
  • Connect the VCC of DS3231 to the 5V of Arduino.
  • Connect the SDA of DS3231 to the A4 of Arduino.
  • Connect the SCL of DS3231 to the A5 of Arduino.

The A4 and A5 pins of the Arduino are the SDA and SCL. So, only connect these pins with the SDA and SCL of DS3231 module. If you are using the Arduino mega 2560, then SDA and SCL pins will be digital pin 20 and digital pin 21. If you are using Leonardo or Micro, then SDA and SCL pins will be digital pin 2 and digital pin 3.

After all these connections, the hardware part of the Arduino real time clock and temperature monitor will be ready.

Arduino real time clock and temperature monitor

Code

 

  • Download the library for LCD module from the following link

16 X 2 LCD Module

#include <DS3231.h>
#include <LiquidCrystal.h>
DS3231  rtc(SDA, SCL);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
 rtc.begin();
 lcd.begin(16,2);
  //rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(1, 15, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(6, 6, 2017);   // Day, Month, Year
}

void loop() {
 lcd.setCursor(0,0);
 lcd.print("Real Time Clock  ");

 lcd.setCursor(0,1);
 lcd.print("Time: ");
 lcd.print(rtc.getTimeStr());
 delay(3000);

 lcd.setCursor(0,1);
 lcd.print("Date: ");
 lcd.print(rtc.getDateStr());
 delay(3000);

 lcd.setCursor(0,1);
 lcd.print("Day: ");
 lcd.print(rtc.getDOWStr());
 lcd.print("           ");
 delay(3000);

 lcd.setCursor(0,1);
 lcd.print("Temp: ");
 lcd.print(rtc.getTemp());
 lcd.print(" C");
 lcd.print("          ");
 delay(3000);
}

Code Explanation

First of all, we added the libraries for the DS3231 real time clock and the LCD. After that, we initialized a variable of name rtc and of type DS3231 to use the library functions. Similarly, we initialized a variable of name lcd and of type LiquidCrystal to use the library functions.

#include <DS3231.h>
#include <LiquidCrystal.h>
DS3231  rtc(SDA, SCL);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

The following commented lines in the setup functions are to set the Day, time and date. If you are using the DS3231 module for the first time, then set the day, time and day in these lines of code and then uncomment these lines. After un-commenting these lines, run the code and your day, time and date will be updated. Now again comment these lines.

//rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(1, 15, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(6, 6, 2017);   // Day, Month, Year

In the loop function, we set the cursor at the start of first line and printed ‚ÄúReal Time Clock‚ÄĚ there. Then we set the cursor on the second line and Printed the Time, Date, Day and temperature one by one.

lcd.setCursor(0,0);
 lcd.print("Real Time Clock  ");
 lcd.setCursor(0,1);
 lcd.print("Time: ");
 lcd.print(rtc.getTimeStr());
 delay(3000);
 lcd.setCursor(0,1);
 lcd.print("Date: ");
 lcd.print(rtc.getDateStr());
 delay(3000);
.
.
.

Video


If you have any comments, feel free to ask us in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar