Real time clock (RTC) DS3231 with Arduino Uno

5
(3)

To control the system with the time and date, Real Time Clock (RTC) module (DS3231) is best to use. The time and date can store and it works for a long time as it contains the cell.

In this tutorial, I’m going to show you how to interface the real-time module (DS3231) with Arduino Uno and display real-time & date on the serial monitor. In this work, Arduino code, wiring diagram and component list presents for experimentation.

Follow peppe8o posts on -> Twitter

DS3231 Description

DS3231 Time Module
DS3231 pinout

DS3231 (RTC) is a low-cost, highly accurate Real Time Clock with having built-in temperature sensor. It can maintain hours, minutes and seconds, as well as, day, month and year information. RTC modules use in computers, mobiles, embedded system devices, etc. to provide time and date. RTC module works on the I2C protocol. The module provides details such as second, minute, hour, day of the week, day of the month, month, and year including correction for leap year. It can operate in 12 and 24-hour time formats. It uses in projects like data-logging, time stamping, clocks and alarms.

Working Phenomena

RTC DS3231 module contains a backup battery which provides supply when the main supply is cut off. DS3231 RTC maintains its clock by counting the cycles of an oscillator, usually an external crystal oscillator circuit, an internal capacitor-based oscillator, or a quartz crystal. DS3231 module needs an Arduino-compatible microcontroller. It works on the I2C communication protocol. The Arduino gets the time & date information and other time-relevant flags from the RTC module by using the I2C protocol,

What We Need

Arduino Uno R3 board

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with the following links:

Amazon Arduino Uno box
amazon-rtc-ds3231-box
Amazon Dupont Wiring box

Step-by-Step Procedure

DS3231 contains 6 pins, which are VCC, GND, SCL, SDA, SQW and 32K. At this time we will use only VCC, GND, SCL and SDA (I2C pins). Every microcontroller has dedicated pins of I2C. For Arduino Uno are A4 (SDA) and A5 (SCL).

BoardI2C Dedicated Pins
Uno, EthernetA4 (SDA), A5 (SCL)
Arduino Mega 256020 (SDA), 21 (SCL)
Leonardo2 (SDA), 3(SCL)
Due20(SCA), 21 (SCL
I2C pins in different microcontrollers

Wiring Diagram of DS3231 clock with Arduino Uno

To connect the DS3231 module to Arduino we need to:

  • Connect Ground to Ground.
  • Connect the VCC pin to the Arduino 5v pin.
  • Connect SDA pin to Arduino analogue pin A4
  • Connect SCL pin to Arduino analogue pin A5.

Please follow the wiring from the following picture, according to Arduino Uno Pinout:

DS3231Wiring diaigarm with arduino
DS3231Wiring diagram with Arduino

Get the code and DS3231 Library (RTClib)

Connect your PC to Arduino and open Arduino IDE. For the very first steps, you can refer to Connecting Windows PC with Arduino tutorial. You can get the .ino code and libraries from my download area with the following link:

You also need to install the following libraries, according to my Install Arduino Libraries: methods to add libraries with Arduino IDE tutorial.

RTClib library addition from Library Manager
RTClib library addition from Library Manager
Arduino-IDE-manage-library-Adafruit-BusIO

Code Explanation

Section 1;

This is the section before setup which uses for globe variables defining and libraries additions. RTClib.h is the library for real-time clocks. Here we have defined the instance (etc) of the class (RTC_DS3231) and array for the days of the week.

#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Section 2;

This is the setup section in which Serial.begin(9600) initialise. rtc. begin() use to check whether the DS3231 module attaches or not. when we use the DS3231 for the first time, it doesn’t know the current time and date. So we use this line (rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));) it will automatically get the date and time from your pc or we can manually set it by uncommenting the below line.

void setup () 
{
Serial.begin(9600);
if (! rtc.begin()) 
{
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
    Serial.println("Setting the time...");
    // When time needs to be set on a new device, or after a power loss, the
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // rtc.adjust(DateTime(2022, 08, 27, 4, 0, 0));
  }

Section 3;

In the loop section DateTime now = rtc.now() use get the current date and time from the DS3231 module. now.year() is use for current year, now.month() is use for current month, now.day() is use for current date, daysOfTheWeek[now.dayOfTheWeek() is use for the current day (Monday to Sunday), now.hour() is use for current hour, now.minute() is use for current minutes and now.second() is use for the current seconds.

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();
    delay(2000);
}

Run the code

From your Arduino IDE, compile the code. Upload the code and open the serial monitor, On the serial monitor day and time display. On the serial monitor, time displays based on a two-second delay intentionally so that there should be rest time for the whole system. Otherwise second last line delay (2000) can change to delay(1000) for each second delay. Time, day and date prints on the serial monitor at baud rate 9600.

arduino DS3231 time and date display on Serial monitor
The DS3231 time display on the serial monitor

What’s Next

Please find more tutorials on Arduino in peppe8o Arduino archives.

Enjoy!

Umar Jamil

For any queries and help for work, please contact me at:
Whatsapp: +92-346-6617017/ Link
Email: umarjamil0007@gmail.com

How useful was this post?

Click on a star to rate it anonymously!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?