Introduction
It is currently 4:34 p.m., and my laptop is displaying that time at the very bottom right corner of my screen while I write this piece. Although I’m not sure what time it will be when you read this post, I’m sure that whatever device you are using to read it—a laptop, computer, tablet, or smartphone— will display the time. Have you ever wondered how these devices, which include a variety of circuits, manage to display the correct time and date? The answer is An RTC component. So, in this tutorial, we are going to Interface “DS3231 RTC Module with Arduino”.
DS3231 RTC Module
The DS3231 RTC module is a timer that displays the current time and date. RTC stands for Real Time Clock. The I2C protocol is used by this RTC module. The module includes information such as the second, minute, hour, day of the week, day of the month, month, and year, as well as a leap year adjustment. Another intriguing feature is that it may function in either a 12-hour or a 24-hour mode.
Hardware Overview
This compact module features a battery cell for backup power, an inter-integrated (I2C) interface, and six pins for data transfer. The TWI interface, two alarm clocks, and an integrated temperature-compensated crystal oscillator are all included in the DS3231 Real-time Module.
Pinout
Pin Name | Description |
---|---|
VCC | Power Supply pin |
GND | Ground pin |
SQW | Square-wave Output pin |
SCL | Serial Clock pin |
SDA | Serial Data pin |
32K | 32KHz Open-drain Output pin |
Hardware Required
S.no | Components | Value / Model | Qty |
---|---|---|---|
1 | Arduino UNO | – | 1 |
2 | USB Cable Type A to B | – | 1 |
3 | RTC Module | DS3231 | 1 |
4 | Jumper Wires | – | – |
Circuit Diagram
Connection Table
Arduino UNO | DS3231 RTC Module |
---|---|
5V | VCC |
GND | GND |
A4 | SDA |
A5 | SCL |
Arduino Code
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
Serial.println();
delay(3000);
}
Working Explanation
The interface is simple to use. Simply turn on the RTC using either the controller or a power source, and then connect the SCL and SDA of the module to the Arduino. You can follow the circuit schematic shown above. Once the code has been uploaded, use a serial monitor to view the outcomes.
Code Explanation
- At first, we need to install the RTC library and for this, we Open the Arduino IDE and navigate to Tools > Manage Libraries. The window library management window will then emerge. Enter RTClib into the search bar to find it. Choose RTClib by Adafruit and press the install button. When you click the install button, you could get a popup warning that RTClib might need additional libraries that aren’t already installed in your Arduino IDE. We advise you to click the install button to install all requirements to operate the library RTClib correctly.
- Now include the RTC library that you have installed. Next, we build an RTClib library object and a 2D array containing strings for the days of the week.
- In the void setup, we first give the Serial.begin function to initialize the serial monitor. Next, we provide #ifndef. This preprocessor command verifies the specified condition. Here, we’ve provided the function while (!Serial); to determine whether or not it’s connected to the serial port. If so, the code goes further. Then To determine if the DS3231 module connects or not, we use rtc. begin(). Here we give the condition that if the module doesn’t connect yet, the serial monitor will display Couldn’t find RTC. As a result, the data supplied over the Arduino Serial Port is flushed. And we employ the serial.flush function for this.
- The DS3231 does not have access to the time and date when it is first used. To change the time and date, we must first use (rtc.adjust(DateTime(F( DATE , F( TIME )));) before manually setting it in the next line.
- We provide the different functions in the void loop. DateTime to get the module’s current date and time. The functions now.year(), now.month(), now.day(), daysOfTheWeek[now.dayOfTheWeek()], now.hour(), now.minute(), and now.second() are used to refer to the current year, month, day, week, current hour, minute, and second, respectively.
- The function unixtime() returns the Unix time in seconds. A technique for describing a moment in time is called Unix time. It is the number of seconds that have passed since Thursday, January 1, 1970, at 00:00:00, or Coordinated Universal Time. now() + TimeSpan() provides the future time by adding seconds to the current time.
- Then to get the temperature from the module we use rtc.getTemperature function
Application and Uses
- To give time and date, this module is often utilized in computers, laptops, mobile devices, embedded system applications
- It may be applied to tasks like data logging, clock construction, timers, etc