Real Time Clock Using DS3231 (EASY)




Introduction: Real Time Clock Using DS3231 (EASY)

Hello, My name is Samuel, I am a student. I love micro controllers like arduino, they are my fav...

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

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!


6 People Made This Project!


  • Creative Misuse Contest

    Creative Misuse Contest
  • Tiny Home Contest

    Tiny Home Contest
  • Fix It! Contest

    Fix It! Contest

70 Discussions

In the bottom righthand corner of your serial monitor is the baud rate at which the serial monitor runs at. The baud rate in this code is set at 115200, while you serial monitor is set to 9600 baud. To fix this you must make the baud rates match by either change the line of code from Serial.begin(115200); to Serial.begin(9600);, or you can just change the baud rate in the serial monitor itself to 115200.


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.

This is a relatively disappointing 'ible in my opinion! You simply took the fine work of Henning Karlsen and pasted it onto an Instructables document without adding any useful tips, insights, or implications for this RTC module. You also failed to explain how to use the code properly as well as leaving out the, #include <DS3231.h> part in the code itself.

Second is moving faster than normal time second...

If i compile the code it shows, no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&)'.

what should i do now ?.

Hello, amateur here: I am looking for a way to access hours as an int & a way to add 12 hours to rtc.getTimeStr(), any advice?

1 reply

My serial didn't print. How can I do ?

I'm looking forward to giving this a go. The last several RTC's I've tried using for datalogging simply didn't work at all. Thanks for sharing your successful effort!

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

1 reply

Thanks, I could not figure out why the date would not set on mine

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

How do i rectify this Error?


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&)'

6 replies

same to me. i have tried to use

DS3231 rtc(A4, A5);

yet it doesn't change anything, any suggestion?

problem solved. It runs O.K. now

#include "DS3231.h"

// Init the DS3231 using the hardware interface

DS3231 rtc(A4, A5);

i get this too:

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

and it seems to start with this:

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

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