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
80 Comments
2 years ago on Step 4
Wow. Yet another "example" that is completely broken. the FIRST line that is not a comment is wrong. How is it possible to have so many incompetent people writing examples for this part.
Question 2 years ago
Could somebody explain to me why i have sent the code successfully and its not working. Wires are correctly paired.
Tip 3 years ago on Step 3
If you use A4 and A5
Don't forget to #define Sda A4 and scl A5
Question 4 years 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
Answer 4 years 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.
Question 4 years ago on Step 3
can i get library which can be bridge display in I2C 16x2
4 years ago
1 - #include <DS3231.h>
2 - set baudrate to 9600 instead 117000
4 years ago on Step 4
This code is not accepted :-
DS3231 rtc(SDA, SCL);
5 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&)'
Reply 5 years ago
please help me
6 years ago
my serial cant pirnt it out
Reply 5 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.
Reply 6 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.
5 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.
Question 5 years ago on Introduction
Second is moving faster than normal time second...
Question 5 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 ?.
5 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?
Reply 5 years ago
https://www.instructables.com/id/Arduino-Timer-With-OnOff-Set-Point/
Question 5 years ago
My serial didn't print. How can I do ?
5 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!