Real Time Clock Using DS3231 (EASY)

265,909

115

65

Posted in TechnologyArduino

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

  • Make it Move Contest

    Make it Move Contest
  • Microcontroller Contest

    Microcontroller Contest
  • Casting Contest

    Casting Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

3 Questions

Second is moving faster than normal time second...

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

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

65 Comments

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

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!

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

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

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

How do i rectify this Error?

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

6 replies

same to me. i have tried to use

DS3231 rtc(A4, A5);

yet it doesn't change anything, any suggestion?

problem solved. It runs O.K. now

#include "DS3231.h"

// Init the DS3231 using the hardware interface

DS3231 rtc(A4, A5);

put this

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

i get this too:

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

and it seems to start with this:

libraries\DS3231\DS3231.h:66:2: error: 'uint8_t' does not name a type
uint8_t hour;

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

error.jpg

how to interface it with lcd module and coding for it

Hi,

I have double checked everything, but I am getting a bunch of random characters in the serial monitor.

thanks.

2 replies

Hey Nick make sure that you are using the correct baud rate

make sure that you are using the correct baud rate

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

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

thanks for reading