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