Real Time Clock Using DS3231 (EASY)

343,107

123

78

Introduction: Real Time Clock Using DS3231 (EASY)

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

  • The 1000th Contest

    The 1000th Contest
  • Battery Powered Contest

    Battery Powered Contest
  • Hand Tools Only Challenge

    Hand Tools Only Challenge

78 Discussions

1
Tchiii35
Tchiii35

Tip 9 months ago on Step 3

If you use A4 and A5
Don't forget to #define Sda A4 and scl A5

0
Juv1
Juv1

Question 1 year ago

I've connected the RTC to the arduino exactly as it shown above also run the code.The only problem is whenever i turn on my arduino, it displays the time and date which i've set in my code.Any explanation for this? Thanks

2
bayonino
bayonino

Answer 1 year ago

I had the same problem too but here's what I did:
1. Uncomment the lines with rtc.setDOW, rtc.setTime and rtc.setDate
2. Set the day and time correctly
3. Set the date by day, month and year first (rather than month, day and year as the comment in the code says)
4. Upload the sketch
5. Comment back again the lines with rtc.setDOW, rtc.setTime and rtc.setDate
6. Upload the sketch again

That should do it.

0
EzekielC4
EzekielC4

Question 1 year ago on Step 3

can i get library which can be bridge display in I2C 16x2

1
ArthurM106
ArthurM106

1 year ago

1 - #include <DS3231.h>
2 - set baudrate to 9600 instead 117000

0
wleedantar
wleedantar

1 year ago on Step 4

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


2
SaiS159
SaiS159

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

0
SaiS159
SaiS159

Reply 2 years ago

please help me

0
MarkusS47
MarkusS47

4 years ago

my serial cant pirnt it out

aq.jpg
0
Jadem52
Jadem52

Reply 2 years 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
draikx21
draikx21

Reply 3 years 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.

3
Jadem52
Jadem52

2 years 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
chethanv
chethanv

Question 2 years ago on Introduction

Second is moving faster than normal time second...

0
varnakumarv
varnakumarv

Question 2 years 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
BlakeB66
BlakeB66

2 years 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?

0
MargauxK
MargauxK

Question 2 years ago

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

0
bohicarico
bohicarico

2 years 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
jime586
jime586

2 years 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

0
paulbrandt2
paulbrandt2

Reply 2 years ago

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