Introduction: 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

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

Comments

author
chriswer made it! (author)2016-03-31

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

author
mannycalavera2 made it! (author)mannycalavera22016-05-16

put this

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

author
banjabi1 made it! (author)banjabi12017-07-21

i get this too:

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

author
banjabi1 made it! (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;

author
SindiO1 made it! (author)SindiO12017-06-13

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

error.jpg
author
DrThomasMorrow made it! (author)2017-07-02

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

author
NathanielS17 made it! (author)2017-02-06

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

thanks for reading

author
NathanielS17 made it! (author)2017-02-06

The character are...weird.

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

Screen Shot 2017-02-06 at 20.28.21.png
author
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.

DS3231 Project.jpg
author
MarkusS47 made it! (author)2016-10-12

my serial cant pirnt it out

aq.jpg
author
draikx21 made it! (author)draikx212017-01-14

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.

author
ThijsW2 made it! (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?
Thanks!

author
curtis.newton.104203 made it! (author)2016-08-03

include what ?

author
SudhanvaA made it! (author)SudhanvaA2017-01-07

#include <DS3231.h>

Before dng that make sure u hv downloaded the DS3231 library

author
lynchjg made it! (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.

author
MichelP28 made it! (author)MichelP282017-01-02

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

author
Aviyahalom made it! (author)Aviyahalom2016-11-29

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

thanks

author
shiharan made it! (author)shiharan2016-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 () {

Serial.begin(9600);

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 = rtc.now();

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

Serial.print('/');

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

Serial.print('/');

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

Serial.print(" (");

Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);

Serial.print(") ");

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

Serial.print(':');

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

Serial.print(':');

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

Serial.println();

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(now.unixtime());

Serial.print("s = ");

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

Serial.println("d");

// 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('/');

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

Serial.print('/');

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

Serial.print(' ');

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

Serial.print(':');

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

Serial.print(':');

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

Serial.println();

Serial.println();

delay(3000);

}

author
essakhi made it! (author)2016-10-31

HI

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

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

I tried but I get this (see pictures)

please help me

problem.png
author
michaelp511 made it! (author)michaelp5112016-10-11

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

Work fine for me.

author
LievenD3 made it! (author)LievenD32016-10-11

thx it works :D

author
mpavlakis made it! (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!!!

author
Alkson made it! (author)Alkson2016-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??

Thanks

author
mpavlakis made it! (author)mpavlakis2016-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.

Regards,

author
Gustavo albertoS made it! (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

THANKS MI FREND....

author
wkraus made it! (author)2016-04-01

Hi,

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)

author
DeanC23 made it! (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);

author
DeanC23 made it! (author)DeanC232016-03-29
author
MarcusO9 made it! (author)2016-03-24

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

author
CarlosK7 made it! (author)2016-03-15

Muchas gracias!

author
AECE made it! (author)2016-02-04

why it shows error while compling in arduino ide?

author
webstockxalapa made it! (author)webstockxalapa2016-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!

author
angier_allen made it! (author)2016-02-15

Very helpful, thank you!

author
Marctyp made it! (author)2016-01-30

Hey

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

author
freemarket made it! (author)2016-01-29

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

author
freemarket made it! (author)2016-01-29

what does the library do?

author
JoeS155 made it! (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);" !

author
curiosity36 made it! (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?

author
HeshamA14 made it! (author)HeshamA142016-01-17

void loop()

{

// Send current temperature

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

Serial.print("Temperature: ");

Serial.print(temp);

Serial.println(" C");

delay (1000);

}

author
curiosity36 made it! (author)curiosity362016-01-18

HeshamA14,
Thank you for your response.
Your suggestion didn't work for me exactly as your code, but it put me on the right track. A flag was thrown on "float temp". Another step in the learning process.

I declared two vars. int val=0; and int Ftemp; then inserted the following code just before delay(1000)

// Send current temp in F per HeshamA14 20160118
Ftemp = ((val*9/5)+32);
Serial.print("Temperatture: ");
Serial.print(Ftemp);
Serial.println("F");

// Wait one second before repeating :)
delay (1000);

author
Ruben Marc Speybrouck made it! (author)2015-11-16

Thanks for this Samuel, you prob saved me hours. One more question: Why does the following code not work?

if (rtc.getTimeStr() /= "12:04:20") {

digitalWrite(13,HIGH);

}

author
ocupmoc made it! (author)ocupmoc2015-12-11

Howdy RubenS13, instead of /= you should be using == but for some reason it will still not work. I found that using the Unix time did do the trick, so something like:

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

Hope this helps.

author
ocupmoc made it! (author)ocupmoc2015-12-23

The Unix time has a small disadvantage, it also includes date. So I had to reset the date every day, not really full proof. Hence I ended up using a different library with alarm function:

https://github.com/jarzebski/Arduino-DS3231

It actually has 2 alarms, as I am using it for my aquarium it is perfect to trigger an on and off cycle.

author
digiteknique made it! (author)digiteknique2015-12-09

Are you trying to set 13 to high when the time does not equal 12:04:20? If so you would want to use !=

I am not familiar with a /= operator

author
tylerr20 made it! (author)2015-12-21

Hey Sam,

I was wondering if you knew a way to add an hour to the DS3231 module via a button. I tried doing it via a button state == HIGH with rtc.hour = hour+1; but it didn't work. I took a look at the library, and it looks like there is a way to do it, I just can't figure out how. Any input is appreciated.


Thanks,


Tyler

DSC_2059.JPG
author
hkaynar made it! (author)2015-12-20

I also added temperature.

Serial.print("\t Temp: ");

Serial.println(rtc.getTemp());

hkaynarDS1323.jpg
author
jdalool made it! (author)2015-12-12

Well written, worked great! I also was confused by the strange output characters until I read another comment about setting the Serial Monitor baud rate to 115200.

author
webstockxalapa made it! (author)2015-10-27

Thanks for all these, I change el baud rate and works perfect just like zrmarin13 said. I have one question:
¿How can I use the hour and the second of the RTC? I want to use it for simulate the sun light intensity while the time it's increasing

author
AlexandreC16 made it! (author)2015-09-19

Thanks ^^

About This Instructable

219,324views

105favorites

License:

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: