Interfacing DS3231 RTC (Real time clock) module with Arduino

Interfacing DS3231 RTC (Real time clock) module with Arduino

In this tutorial, we’ll be interfacing the DS3231 RTC (Real Time Clock) module with an arduino.

 

First lets see what we need for this tutorial !!

Well, for this tutorial we only need:
                 1) Arduino Uno or Arduino Mega
                 2) DS3231 RTC
                 3) RTC library
                 4) PC or Laptop (For now we’ll be showing this on windows only)

Arduino Uno                                                                 DS3231 RTC

   

 

Connection Schematics

First, connect everything like this diagram below.

 

DS3231 | ArduinoUno
VCC     >  5V
GND    >  GND
SDA     >   A4
SCL     >   A5


 

Arduino-Mega-1024x402

 

DS3231 | ArduinoMega

VCC     >  5V
GND    >  GND
SDA     >   20
SCL     >   21


The Code!!

Download the library from the link below.

Download RTC Library

If you don’t know what arduino library is or how to use them, see my previous post about library installation.

Now connect your arduino to the computer and upload the following code!
You can also find in your arduino library under example > DS1307 > setTime

/*
 * this code syncs the RTC module's time
 * to the PC and you don't need to set
 * them manually
 */

//#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

2016-07-28Now open the serial monitor and you should be seeing this.


Now you can disconnect your arduino or even power off the module, the time set on the RTC module will keep intact for years.

 

2016-07-28

 

To check the time, simply go to example > DS1307 > readTime and upload the code.


The library i’ve used here is an older one with basic functionalities. Google a little to find other libraries according to your project needs.

Feel free to comment if you face trouble following any part of this tutorial or need additional help!

Thank You!


What are your thoughts?