Real Time Clock Using DS3231 (EASY)

292,366

118

73

About: 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 worldwide treasure hunt game. I hope you enjoy my instructables! Samuel

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:

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

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: http://www.RinkyDinkElectronics.com/ // // 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. //

#include

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

Samuel

6 People Made This Project!

Recommendations

  • PCB Contest

    PCB Contest
  • First Time Author

    First Time Author
  • Big and Small Contest

    Big and Small Contest

73 Discussions

0
None
wleedantar

4 weeks ago on Step 4

This code is not accepted :-
DS3231 rtc(SDA, SCL);


0
None
SaiS159

3 months ago

Hi!

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

1 reply
0
None
Jadem52MarkusS47

Reply 5 months ago

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.

0
None
draikx21MarkusS47

Reply 1 year ago

Serial.begin(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.

0
None
Jadem52

5 months ago

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.

0
None
chethanv

Question 8 months ago on Introduction

Second is moving faster than normal time second...

0
None
varnakumarv

Question 8 months ago

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 ?.

0
None
BlakeB66

8 months ago

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
0
None
MargauxK

Question 8 months ago

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

0
None
bohicarico

8 months ago on Step 4

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!

0
None
jime586

1 year ago

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
0
None
paulbrandt2jime586

Reply 9 months ago

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

0
None
Frank EdwinM

11 months ago

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

How do i rectify this Error?

0
None
chriswer

2 years ago

Hi!

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

3 replies
0
None
auck2015chriswer

Reply 11 months ago

same to me. i have tried to use

DS3231 rtc(A4, A5);

yet it doesn't change anything, any suggestion?

0
None
auck2015auck2015

Reply 11 months ago

problem solved. It runs O.K. now

#include "DS3231.h"

// Init the DS3231 using the hardware interface

DS3231 rtc(A4, A5);