Set Time on DS3231 Real Time Clock With Arduino UNO

The DS3231 is a very accurate & widely used Real Time Clock (RTC) module. This module is used in Arduino based projects. This module also works with Raspberry Pi. In this post, I will share how to Set Time on DS3231 Real Time Clock Module with Arduino UNO board.

ds3231 rtc module with cr2032 coin battery

Before connecting DS3231 with Arduino, make sure you have inserted CR2032 battery into DS3231. It is recommended to use Rechargeable cell. But you can also use non rechargeable cell.

Components Used:(Click to Buy)

Download Library of DS3231

Now open your Arduino IDE and go to Library manager from menu bar.
Now search for RTCLib and select the latest version to download. This library is provided by Adafruit as you see in the image below.

rtclib for ds3231 by adafruit in arduino ino

Connect DS3231 with Arduino UNO

After adding the library we have to connect DS3231 with Arduino UNO. Connection is very simple as you can see the diagram below.

Set Time on DS3231 Real Time Clock Module With Arduino UNO

If you face any issue after connection in the above diagram, then reconnect SCL and SDA to A5 and A4.

Clock with Temperature using DS3231 & SSD1306 in Arduino

Update current time using program

After all the connection done we have to write a simple code to update the current into DS3231 module.

Remember only write time once after installing the CR2032 coin battery. As long as there is power in the battery, you will not get any error in the time.

You can use the following code to update time.

ds3231 rtc module with cr2032 coin battery

Code Explain

The code is very simple one. It will set the time & then display the time in serial monitor from DS3231.


In line 18, rtc object is setting the time according to your computer time. It will set present clock time of your system.

rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

In line 19, you can set the time manually just sending the date time value to the function in the order of year, month, date, hour, min, second.
In the following code, we are setting time if system. So we commented the line 19.

Arduino Sketch

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

RTC_DS3231 rtc;

char t[32];

void setup() 

  //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));


void loop()
  DateTime now =;

  sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d",  now.hour(), now.minute(), now.second(),, now.month(), now.year());  
  Serial.print(F("Date/Time: "));



After opening serial monitor in 9600 baud rate you can see the following output i.e. Time fetched from DS3231.

Set Time on DS3231 Real Time Clock Module With Arduino UNO


  1. Very good tutorial, short but very effective.
    I’ve spent several hours adjusting my clock and downloading different codes. But nothing worked.
    Now everything is perfect.

    Thank you very much
    Stephan from Switzerland

  2. How can you get a 4 digits formatted year when using use the %02d placeholder in the sprintf() call ? What I am missing ?

Comment Box