Introduction: Real Time Clock Using DS3231 (EASY)

Picture of Real Time Clock Using DS3231 (EASY)

I have found that the use of the RTC chip- DS3231 is extremely hard using the arduino. Just setting the time is pretty complex, not mentioning the code. Therefore I have found a great and easy to use library which really enhances the use of the DS3231 chip. Let's get right onto it.

Step 1: Connections

Picture of Connections

Of course, you will need to connect the chip first. It is very easy, do it according to the pictures or below:

VCC -> Arduino 5V

GND -> Arduino GND

SCL -> SCL or A5

SDA -> SDA or A4

As long as I know, there are dedicated SCL and SDA pins on the Arduino UNO and MEGA.

There are also two other pins which are the32K and SQW ones but we will not use them as we get the full functionality through the I2C interface.

Step 2: Library

We will use a library from Henning Karlsen which is great. Thanks a lot for that! There are some basic functions such as reading time and date and writing time and date. Download the library here:

Step 3: The Code

I will just use an example sketch from the library which will include lots of comments for you to read, enjoy:

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved // web: // // A quick demo of how to use my DS3231-library to // quickly send time and date information over a serial link // // To use the hardware I2C (TWI) interface of the Arduino you must connect // the pins as follows: // // Arduino Uno/2009: // ---------------------- // DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin // SCL pin -> Arduino Analog 5 or the dedicated SCL pin // // Arduino Leonardo: // ---------------------- // DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin // SCL pin -> Arduino Digital 3 or the dedicated SCL pin // // Arduino Mega: // ---------------------- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin // // Arduino Due: // ---------------------- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin // // The internal pull-up resistors will be activated when using the // hardware I2C interfaces. // // You can connect the DS3231 to any available pin but if you use any // other than what is described above the library will fall back to // a software-based, TWI-like protocol which will require exclusive access // to the pins used, and you will also have to use appropriate, external // pull-up resistors on the data and clock signals. //


// 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 }

void loop() { // Send Day-of-Week Serial.print(rtc.getDOWStr()); Serial.print(" "); // Send date Serial.print(rtc.getDateStr()); Serial.print(" -- ");

// Send time Serial.println(rtc.getTimeStr()); // Wait one second before repeating :) delay (1000); }

Step 4: Done

There you go, this library is really easy to use. Have a good day!



Frank EdwinM (author)2018-01-16

no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)'

How do i rectify this Error?

chriswer (author)2016-03-31


I'm getting an error on this line:

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

no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)'

auck2015 (author)chriswer2018-01-08

same to me. i have tried to use

DS3231 rtc(A4, A5);

yet it doesn't change anything, any suggestion?

auck2015 (author)auck20152018-01-10

problem solved. It runs O.K. now

#include "DS3231.h"

// Init the DS3231 using the hardware interface

DS3231 rtc(A4, A5);

mannycalavera2 (author)chriswer2016-05-16

put this

#include <DS3231.h> and delete de #include line

banjabi1 (author)mannycalavera22017-07-21

i get this too:

"error: 'uint8_t' was not declared in this scope"
how to fix?

banjabi1 (author)banjabi12017-07-21

and it seems to start with this:

libraries\DS3231\DS3231.h:66:2: error: 'uint8_t' does not name a type
uint8_t hour;

SindiO1 (author)mannycalavera22017-06-13

already done that, but i still got "no matching function..." error. can i do something else to solve this kind of error?

jime586 (author)2017-11-30

works for me whoopee

eventually reset to voidsetup () to Serial.begin(9600);

reallized that rtc.setDate(30, 11, 2017) should be day/month/year

not what I tried

raviteja125 (author)2017-11-22

how to interface it with lcd module and coding for it

PouryaMec (author)2017-10-14


thanks for your easy guidance.

nickope (author)2017-08-24


I have double checked everything, but I am getting a bunch of random characters in the serial monitor.


Treblif (author)nickope2017-09-11

Hey Nick make sure that you are using the correct baud rate

Treblif (author)nickope2017-09-11

make sure that you are using the correct baud rate

MalikAbdulA made it! (author)2017-08-24

why my serial can't print it?
please, give me the solution

DrThomasMorrow (author)2017-07-02

Looking for a library with the 12 hour format - any advice?

NathanielS17 (author)2017-02-06

wait no fixed it. Just needed to change the baud to 9600

thanks for reading

NathanielS17 (author)2017-02-06

The character are...weird.

Yes I have the right board selected and I have the right port selected

draikx21 made it! (author)2017-01-14

Thanks for the instructions. It should be noted that "rtc.setDate(1, 1, 2014)" is "rtc.setDate(Day, Month, Year)", when setting the date for the initial configuration.

When I set it for today's date (1, 14, 2017), it was going back to January 1, 1970. If you see that, simply update the date entry, and Verify/Upload to correct.

MarkusS47 (author)2016-10-12

my serial cant pirnt it out

draikx21 (author)MarkusS472017-01-14


You have to change the baud at the bottom right from the current/default 9600, to the one in the code; 155200. Click on the drop-down menu, and choose "155,200 baud" and it will start to work for you.

ThijsW2 (author)2017-01-07

Hi, I am trying to use my DS3231 with ArduinoUno. Somehow the thing seems to "freeze" as soon as it interacts with the RTC.
I've tried using the code suggested above, and it stops right at "Serial.print(rtc.getDOWStr());". Anything I put in loop () before that will be executed (turning LEDs on, or some serial.print). But nothing after happens.
I have double checked all the connections and I already replaced the battery of the DS3231 to make sure that is not empty. The red LED on the DS is on, so it has power.
Any suggestions how to fix this or pin down what the problem might be?

curtis.newton.104203 (author)2016-08-03

include what ?

#include <DS3231.h>

Before dng that make sure u hv downloaded the DS3231 library

lynchjg (author)2016-01-25

I am looking to use the DS3231 library and display the time digits on a 4 digit 7-segment display. I don't want to use the string functions which the document shows the return format. I am thinking I want to use the rtc.getTime() function, however, there are no examples of what is returned and what data types should I be using for hour, minute, second.

Any help would be appreciated.

MichelP28 (author)lynchjg2017-01-02

rtc.getTime() retourne un Time.... mais pourquoi ne veux-tu pas utiliser les strings function ?

Aviyahalom (author)lynchjg2016-11-29

Did you ever work this out? I'm trying to do the same thing.


shiharan (author)Aviyahalom2016-12-04

Try this Code

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib. Use a 4 digit common anode 7 segment display & 2 shift registers (74HC595).

#include <Wire.h>

#include "RTClib.h"

RTC_DS3231 rtc;

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

int unitHour, tenHour, unitMinute, tenMinute;

int clockPin = 8; // Pin 8 of Arduino connected in the pin 11 of 74HC595 (Clock)

int latchPin = 9; // Pin 9 of Arduino connected in the pin 12 of 74HC595 (Latch)

int dataPin = 10; // Pin 10 of Arduino connected in the pin 14 of 74HC595 (Data)

//Digits Matrix - 0 a 9

byte num[] = {

B01111110, // Zero

B00110000, // One

B01101101, // Two

B01111001, // Three

B00110011, // Four

B01011011, // Five

B01011111, // Six

B01110000, // Seven

B01111111, // Eight

B01111011, // Nine


void setup () {


delay(3000); // wait for console opening

if (! rtc.begin()) {

Serial.println("Couldn't find RTC");

while (1);

pinMode(latchPin, OUTPUT); // Define the 3 digital pins as output

pinMode(clockPin, OUTPUT);

pinMode(dataPin, OUTPUT);


if (rtc.lostPower()) {

Serial.println("RTC lost power, lets set the time!");

// following line sets the RTC to the date & time this sketch was compiled

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

// This line sets the RTC with an explicit date & time, for example to set

// January 21, 2014 at 3am you would call:

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



void loop () {

DateTime now =;

Serial.print(now.year(), DEC);


Serial.print(now.month(), DEC);


Serial.print(, DEC);

Serial.print(" (");


Serial.print(") ");

Serial.print(now.hour(), DEC);


Serial.print(now.minute(), DEC);


Serial.print(now.second(), DEC);


unitHour = now.hour() % 10;

tenHour = now.hour() / 10;

unitMinute = now.minute() % 10;

tenMinute = now.minute() / 10;

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 8); //Set DISPLAY 1 (top view from left to right)

shiftOut(dataPin, clockPin, LSBFIRST, ~num[tenHour]); //Set the Hour (ten)

digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 4); //Set DISPLAY 2

shiftOut(dataPin, clockPin, LSBFIRST, ~num[unitHour]); //Set the Hour (unit)

digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 2); //Set DISPLAY 3

shiftOut(dataPin, clockPin, LSBFIRST, ~num[tenMinute]); //Set the Minute (ten)

digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 1); //Set DISPLAY 4

shiftOut(dataPin, clockPin, LSBFIRST, ~num[unitMinute]); //Set the Minute (unit)

digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 4); //Set LED of dots

shiftOut(dataPin, clockPin, LSBFIRST, ~B10000000); //Set LEDs of double dots

digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, 1); //Set DISPLAY 4

shiftOut(dataPin, clockPin, LSBFIRST, 255); //Reset the DISPLAY 4 (to avoid some flicking)

digitalWrite(latchPin, HIGH);

Serial.print(" since midnight 1/1/1970 = ");


Serial.print("s = ");

Serial.print(now.unixtime() / 86400L);


// calculate a date which is 7 days and 30 seconds into the future

DateTime future (now + TimeSpan(7,12,30,6));

Serial.print(" now + 7d + 30s: ");

Serial.print(future.year(), DEC);


Serial.print(future.month(), DEC);


Serial.print(, DEC);

Serial.print(' ');

Serial.print(future.hour(), DEC);


Serial.print(future.minute(), DEC);


Serial.print(future.second(), DEC);





essakhi (author)2016-10-31


i used DS 3231 it work but i have a problèm when u i want
to change the time (add one hour:summer hour),so i used the button with 2
states but dosn't work ,plz help

LievenD3 made it! (author)2016-10-05

I tried but I get this (see pictures)

please help me

michaelp511 (author)LievenD32016-10-11

Change the Serial.begin () into serial.begin(9600).

Work fine for me.

LievenD3 (author)michaelp5112016-10-11

thx it works :D

mpavlakis (author)2016-09-16

Works perfectly on arduino nano 3.0. Just follow the instructions on the web page or the pdf manual. The only mistake was to exchange SCL with SDA. Just correct and everything is fine. Thanks a lot!!!

Alkson (author)mpavlakis2016-09-18

hi, did you change the exchange the wiring for SCL and SDA or was it in the code?

I've been trying but it's not working for me. HELP!!! Also, i just want to make sure the SDA goes to A4 and SCL goes to A5 right??


mpavlakis (author)Alkson2016-09-19

Yes, SDA goes to A4 and SCL goes to A5. I had placed them the wrong way and of course it did not operate until I fixed the mistake. My intention was just to assure that it works fine on arduino nano also.

Just make sure that you have the correct pinout for the version of the arduino you are using.


Gustavo albertoS (author)2016-06-07

hello my friend good day.

if you will send mi the code, because i have a proyect in my colleg and i has have dificult.

can you send mi the librery from arduino uno of DS3231 RTC


wkraus (author)2016-04-01


sorry, I get an error

libraries\DS3231\DS3231.cpp:83:49: error: 'uint8_t' was not declared in this scope

void DS3231::setTime(uint8_t hour, uint8_t min, uint8_t sec)

DeanC23 (author)2016-03-29

Thank you for the guide, it worked great!

i have a small question though, i want to turn on an led at the same time every day, looking at the RTC side of thing i have come up with the following, however correct me if i am wrong but unix time includes the date too? i just want the time every day

if (rtc.getUnixTime(rtc.getTime()) == 1459255200)

analogWrite(led, 64);

MarcusO9 (author)2016-03-24

Very useful and easy to follow, thanks alot! :D

CarlosK7 (author)2016-03-15

Muchas gracias!

AECE (author)2016-02-04

why it shows error while compling in arduino ide?

webstockxalapa (author)AECE2016-02-24

You must have to set the include library, the line is not finished. Another thing that you may have to change is the serial conection baud rate, the code says "115200" I use "9600" and work fine. Thanks for this instructable!

angier_allen (author)2016-02-15

Very helpful, thank you!

Marctyp (author)2016-01-30


available at this code a Arlam or a simple if statement ?

freemarket (author)2016-01-29

is it a application or files? how do i run it?

freemarket (author)2016-01-29

what does the library do?

JoeS155 (author)2016-01-26

Good stuff Samuel!

What confusion I was causing myself when I set the RTC to January 25, 2016 using the US convention. For anyone not reading the DS3231 library PDF, you must use the European standard date format of: "rtc.setDate(25, 1, 2016);"

Expect *bad things* to happen if you use "rtc.setDate(1, 25, 2016);" !

curiosity36 (author)2016-01-16

My first use of the RTC. Works great. Thank you. Can you are anyone help with where and how to insert code into the sketch to convert the C deg to F deg?

HeshamA14 (author)curiosity362016-01-17

void loop()


// Send current temperature

float temp = ( ( rtc.getTemp() * ( 9/5 )) + 32 );

Serial.print("Temperature: ");


Serial.println(" C");

delay (1000);


About This Instructable




Bio: Hello, My name is Samuel, I am a student. I love micro controllers like arduino, they are my favorite interest. I also do geocaching, a ... More »
More by samuel123abc:Real time clock using DS3231 (EASY)Controlling 120-240 VAC with a relay using arduinoNokia 5110 graphics tutorial
Add instructable to: