Description
The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature compensated crystal oscillator (TCXO) and crystal.The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.
Features
- Highly Accurate RTC Completely Manages All Timekeeping Functions
- Simple Serial Interface Connects to Most Microcontrollers
- Battery-Backup Input for Continuous Timekeeping
- Operating Temperature Ranges: Commercial (0°C to +70°C) and Industrial (-40°C to +85°C)
Getting started with the Real Time Clock Module DS3231
This post is about how to use the DS3231 Real Time Clock (RTC) module with the Arduino. This example displays date and time on the serial monitor.
Hardware required
Connecting the Hardware
Connect your Real Time Clock module to your Arduino as in the schematics below.
Wiring the RTC module is pretty straightforward
Pin | Wiring to Arduino Uno |
SCL | A5 |
SDA | A4 |
VCC | 5V |
GND | GND |
If you’re using other Arduino board rather than the uno, chek out what are their SCL and SDA pins.
- Nano: SDA (A4); SCL(A5)
- MEGA: SDA (20); SCL(21)
- Leonardo: SDA (20); SCL(21)
Installing RTClib library
Communicating with a RTC module is a bunch of work. Fortunately, RTClib library was written to hide away all the complexities so that we can issue simple commands to read the RTC data.
To install the library navigate to the Sketch > Include Library > Manage Libraries…Wait for Library Manager to download libraries index and update list of installed libraries.
Filter your search by typing ‘rtclib’. There should be a couple entries. Look for RTClib by Adafruit. Click on that entry, and then select Install.
Arduino Code – Reading Date & Time
The following sketch will give you complete understanding on how to set/read date & time on DS3231 RTC module and can serve as the basis for more practical experiments and projects.
#include <Wire.h> #include "RTClib.h" RTC_DS3231 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void setup () { Serial.begin(9600); delay(3000); // wait for console opening if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (rtc.lostPower()) { Serial.println("RTC lost power, lets set the time!"); // Comment out below lines once you set the date & time. // Following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Following line sets the RTC with an explicit date & time // for example to set January 27 2017 at 12:56 you would call: // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0)); } } void loop () { DateTime now = rtc.now(); Serial.println("Current Date & Time: "); 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.println("Unix Time: "); Serial.print("elapsed "); Serial.print(now.unixtime()); Serial.print(" seconds/"); Serial.print(now.unixtime() / 86400L); Serial.println(" days since 1/1/1970"); // calculate a date which is 7 days & 30 seconds into the future DateTime future (now + TimeSpan(7,0,0,30)); Serial.println("Future Date & Time (Now + 7days & 30s): "); 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.println(); delay(1000); }
NOTE: IF you get stray ‘223’ errors The problem is with your “
and ”
characters. Replace them with ordinary quotes, "
, and you should be fine.
Open the serial monitor at a baud rate of 9600 and you’ll see the results.Here’s the Serial Monitor displaying the current date and time.