Keeping real-time clock with mini DS3231 on Arduino

  • avatar
  • 2.5K Views
  • 2 Likes
  • 10 mins read

When it comes to accurate timekeeping in the world of electronics, the DS3231 real-time clock (RTC) module is a reliable and precise solution. This compact yet robust module not only keeps time with exceptional accuracy but also integrates smoothly with popular microcontrollers like the Arduino Nano. In this article, we'll explore the features, wiring, and potential applications of the DS3231.

Components

arduino-nano

1x Arduino Nano (or another Arduino module)

$3.18

Buy now

mini-breadboard

1x Mini-breadboard

$1.17

Buy now

DS3231

1x mini DS-3231 module

$1.62

Buy now

dupont

Dupont wires

$1.61

Buy now

github

DS3231 library

Download here

The DS3231 module

The DS3231, encased in a modest 16-pin package, is more than just a timekeeping device. Featuring an integrated temperature-compensated crystal oscillator and a built-in temperature sensor, it maintains time with a deviation of mere seconds per month. This level of precision is a game-changer for applications where accurate timekeeping is crucial, such as data logging, time-stamping, or even in DIY clocks.

DS1302 vs DS3231 modules

When comparing the DS3231 and DS1302 real-time clock (RTC) modules, one notable difference lies in their size. The DS3231, with its compact 16-pin package, excels in miniaturization, making it an ideal choice for projects with limited space.

Wiring schema

Connecting the DS3231 to an Arduino Nano is a straightforward process, requiring minimal components. Begin by linking the VCC and GND pins of the module to the corresponding power supply pins on the Arduino Nano. The SDA and SCL pins should be connected to the A4 and A5 pins, respectively, establishing communication through the I2C protocol. The module has a fifth pin labeled NC, denoting Not Connected and remains unused in the configuration.

Arduino Nano wiring with DS3231 RTC module

Once wired up, the DS3231 becomes a reliable timekeeper, capable of maintaining accurate time even in the absence of external power. The built-in battery backup ensures that the module continues to keep time, making it ideal for applications requiring uninterrupted operation.

Install library for DS3231

To interact with the DS3231 sensor, we will use an existing library. This library, provides an interface that facilitates communication with the module, saving you significant time and providing a reliable and extensively tested code base. It can be downloaded from our official repository.

To import a library, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library and select the library file downloaded from our GitHub repository.

Arduino IDE library import

Then you can simply use include statement:

#include "DS3231.h"

It will include the library with predefined functions to interact with the module.

Arduino code

This Arduino code uses the DS3231 library to interface with the DS3231 real-time clock module. The setup function initializes the DS3231, setting the initial clock time to the moment the sketch was last compiled. The loop function continuously retrieves the current date and time from the DS3231 module and prints the information to the serial monitor.

#include "DS3231.h"

DS3231 clock;
RTCDateTime dt;

void setup()
{
Serial.begin(115200);

// Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();

// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);

// Manual from UNIX timestamp
// clock.setDateTime(1397408400);

// Manual from date (YYYY, MM, DD, HH, II, SS)
// clock.setDateTime(2024, 1, 1, 00, 00, 00);
}

void loop()
{
dt = clock.getDateTime();

Serial.print("Long number format:\\t\\t");
Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));

Serial.print("Long format with month name:\\t");
Serial.println(clock.dateFormat("d F Y H:i:s", dt));

Serial.print("Short format witch 12h mode:\\t");
Serial.println(clock.dateFormat("jS M y, h:ia", dt));

Serial.print("Today is:\\t\\t\\t");
Serial.print(clock.dateFormat("l, z", dt));
Serial.println(" days of the year.");

Serial.print("Actual month has:\\t\\t");
Serial.print(clock.dateFormat("t", dt));
Serial.println(" days.");

Serial.print("Unixtime:\\t\\t\\t");
Serial.println(clock.dateFormat("U", dt));

Serial.println();

delay(1000);
}

The code demonstrates a simple way to access and display accurate date and time data using the DS3231 module with an Arduino Nano. As an alternative, one can acquire each individual date element separately.

void loop()
{
dateTime = clock.getDateTime();

Serial.print("Date: ");

// Date
Serial.print(dateTime.year);
Serial.print("-");
Serial.print(dateTime.month);
Serial.print("-");
Serial.print(dateTime.day);

// Separator
Serial.print(" ");

// Time
Serial.print(dateTime.hour);
Serial.print(":");
Serial.print(dateTime.minute);
Serial.print(":");
Serial.print(dateTime.second);
Serial.println();

delay(1000);
}

In this Arduino code snippet, the loop function continuously retrieves the current date and time from the DS3231 module and showcases different formatting options for displaying this information on the serial monitor.

Date format options

There are multiple options to customize the date output using the date format function. Below, you'll find the available keywords for formatting the dates.

Date formats (Day):

  • d : Day of the month, 2 digits with leading zeros (01 to 31)

  • D : A textual representation of a day, three letters (Mon through Sun)

  • j : Day of the month without leading zeros (1 to 31)

  • l : A full textual representation of the day of the week (Sunday through Saturday)

  • N : ISO-8601 numeric representation of the day of the week (1 for Monday through 7 for Sunday)

  • S : English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)

  • w : Numeric representation of the day of the week (0 for Sunday through 6 for Saturday)

  • z : The day of the year (0 through 365)

Date formats (Month):

  • F : A full textual representation of a month, such as January or March (January through December)

  • m : Numeric representation of a month, with leading zeros (01 through 12)

  • M : A short textual representation of a month, three letters (Jan through Dec)

  • n : Numeric representation of a month, without leading zeros (1 through 12)

  • t : Number of days in the given month (28 through 31)

Date formats (Year):

  • L : Whether it's a leap year (1 if it is a leap year, 0 otherwise)

  • Y : A full numeric representation of a year, 4 digits (Examples: 1999 or 2003)

  • y : A two digit representation of a year (Examples: 99 or 03)

Date formats (Time):

  • a : Lowercase Ante meridiem and Post meridiem (am or pm)

  • A : Uppercase Ante meridiem and Post meridiem (AM or PM)

  • g : 12-hour format of an hour without leading zeros (1 through 12)

  • G : 24-hour format of an hour without leading zeros (0 through 23)

  • h : 12-hour format of an hour with leading zeros (01 through 12)

  • H : 24-hour format of an hour with leading zeros (00 through 23)

  • i : Minutes with leading zeros (00 to 59)

  • s : Seconds, with leading zeros (00 through 59)

Dare formats (Full Date/Time):

  • U : Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Testing

This testing section features the Arduino Serial Monitor displaying output dates in various formats. The provided code continuously retrieves the current date and time from the DS3231 module and uses different formatting options to present this information.

Serial monitor: DS3231 formatted output testingThe image below displays the raw date output in the Arduino Serial Monitor, constructed from each date element obtained separately. The code provides an alternative approach, showcasing how individual date components can be accessed.

Serial monitor: DS3231 simple output testing

Understanding how to access and manipulate these elements independently can be advantageous in scenarios where a more granular control over date and time information is necessary.

Conclusion

To sum it up, the DS3231 real-time clock module is a standout in the world of accurate timekeeping for Arduino projects. Its integration with the Arduino Nano opens up numerous possibilities, from building precise data loggers to creating sophisticated digital clocks. With its compact size, impressive accuracy, and easy connectivity, the DS3231 proves to be a valuable addition to any electronics enthusiast's toolkit.

Credits

Official GitHub: https://github.com/hibit-dev/DS3231

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

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.