DS3231
Communicates between Arduino-programmed AVR and Maxim DS3231 RTC: splice of Ayars' (http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html) and Jeelabs/Ladyada's (https://github.com/adafruit/RTClib) libraries
Install / Use
/learn @NorthernWidget/DS3231README
DS3231 Library
An Arduino library for the DS3231 real-time clock (RTC).
Description
The library provides easy-to-use methods to:
- set and read the current date and time;
- set, clear, and detect two, independent, Time-of-Day alarms;
- perform certain conversions and calculations with time data;
- manage certain hardware inside the DS3231 RTC module.
This document explains the installation and usage of the Library with the Arduino IDE.
You do have to install the Library in your Arduino IDE environment before you can use it. Installation instructions are provided, below.
Contents
- Summary
- How to Cite
- About the DS3231
- How to Install the Library
- Functions Provided in the Library
- Examples of Using the Library
- Helpful Resources
- Contributing, Credits and License
- To-Do List
Summary
After installing the Library in your Arduino IDE, using it in a program starts with three, simple steps:
<ol start="1"> <li>Import the Library into the program code:</li> </ol>#include <DS3231.h>
<ol start="2">
<li>Declare a DS3231 object, for example:</li>
</ol>
DS3231 myRTC;
<ol start="3">
<li>Start the Wire library to enable I2C communications with the DS3231 hardware, typically in the setup() code block:</li>
</ol>
Wire.begin();
Then, Library functions are typically accessed through the DS3231 object. For example, to read the current date of the month (1 through 28, 29, 30 or 31, depending on the month and the year):
unsigned int theDate = myRTC.getDate();
The Library incorporates two other classes to assist with managing date and time data:
DateTimeenables a versatile object for managing date and time data. A variable of the DateTime type can represent a specific date and time in two different ways:- as distinct values for year, month, day, hour, minute and second, or
- as a single, unsigned integer. The latter is handy for doing arithmetic with dates.
RTClibinstitutes a convenientRTClib::now()function for receiving a date/time snapshot, as a DateTime object, from the DS3231 device.
How to Cite
If you use this library in a publication, please cite it in one or both of the following two ways:
- Through the
CITATION.cfffile here, which should be up to date with the permanent archive available from Zenodo - If you need an academic journal reference and/or you are discussing the integration of the DS3231 into a larger hardware + firmware ecosystem,<br/> Wickert, A. D., Sandell, C. T., Schulz, B., & Ng, G. H. C. (2019), Open-source Arduino-compatible data loggers designed for field research, Hydrology and Earth System Sciences, 23(4), 2065-2076, doi:10.5194/hess-23-2065-2019.<br/> This option should not be the only one used because it does not credit the original library developer, Eric Ayars.
About the DS3231
DS3231 is a low-cost integrated circuit (IC) providing a highly accurate, real time clock for use with Arduino, Raspberry Pi, BBC micro:bit and other popular small computing devices.
The IC is typically mounted on a circuit board or module, along with other hardware, such as header pins, supportive electrical components, and even EEPROM memory chips, for convenient attachment to a breadboard or an Arduino.
Several different modules are available from a number of competing vendors. This Library aspires to work with any DS3231 module that supports I2C communications with the IC.
DS3231 runs independently and can be kept running for a considerable length of time by a small, backup battery, even if power to the Arduino is turned off.
According to the datasheet, the DS3231 hardware "completely manages all timekeeping functions (including):
- Seconds,
- Minutes,
- Hours
- 12-hour format with AM/PM indication, or
- 24-hour format,
- Day of the Week,
- Date of the Month,
- Month, and
- Year, with Leap-Year Compensation Valid Up to 2100"
Data for the time and date are stored in registers (memory locations) on the DS3231. Each, distinct value is stored separately. This means the seconds are in one register, the minutes in another, and so forth. The DS3231 updates the values in the date and time registers every second.
The device keeps track of time by operating its own 32.768 kHz crystal oscillator, similar to the timekeeper in an electronic watch. Temperature can affect oscillator speed. Accordingly, the DS3231 takes further steps to maintain accuracy. It senses the temperature around the crystal and adjusts the speed of the oscillator.
The oscillator can be accessed directly, independent of the date and time registers, for use as an external timer or source of interrupts.
The temperature can be read from the DS3231 using a Library function. The data sheet declares it to be accurate to within 3 degrees, Celsius.
Power Supply and Battery Backup
The DS3231 can run in a range between 2.3 volts and 5.5 volts. The device actually has two power supply pins: the primary source, V<sub>CC</sub>, and a secondary, backup source, V<sub>BAT</sub>.
Some popular modules mounting a DS3231 provide a receptacle for a coin battery, attaching it to the V<sub>BAT</sub> pin. If a sufficiently-charged battery is present, the DS3231 will switch automatically to the battery after detecting a drop in V<sub>CC</sub> voltage below a certain "power-fail" level.
It will switch back to V<sub>CC</sub> automatically, if and when that voltage rises back up above both the power-fail and the battery voltage level.
One point regarding the choice of battery may deserve consideration: the question of whether to install a rechargeable coin battery, or to disable a charging circuit if such a thing is provided on the module being used. The topic is controversial and the authors of this Library do not express any opinion about it. Readers may choose to search online for more information.
<hr>Installation
First Method

- In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries
- Then the Library Manager will open and you will find a list of libraries that are already installed or ready for installation.
- Then search for DS3231 using the search bar.
- Click on the text area and then select the specific version and install it.
Second Method
- Navigate to the Releases page.
- Download the latest release.
- Extract the zip file
- In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library
Dependencies
The user must also ensure that two, other, required libraries are available to the Arduino IDE. This DS3231 library takes care to #include the following in a program, but it does not install them in your Arduino IDE:
- Wire.h : a widely-used Arduino library for I2C communications
- time.h : a modified C language header file packaged with avr-libc and the AVR-GCC compiler
Note: At the time of writing, both of these libraries were included by default with a standard installation of the 1.8.x version of Arduino IDE for AVR-based devices.
A simple way to check for the availability of the two libraries is to compile the following, blank Arduino sketch. If the IDE does not complain that anything is missing, then the required libraries are available for use with this DS3231 library.
#include <Wire.h>
#include <time.h>
void setup() {}
void loop() {}
<hr>
Functions
Readers are encouraged to visit the Documentation directory for detailed information about the functions in this Library. Additional information is available in the Examples of Use described below, and in the code source files of this repository:
Read the Date or Time
- RTClib::now() <sup>*</sup>
- getSecond()
- getMinute()
- getHour(bool, bool)
- getDoW()
- getDate()
- getMonth(bool)
- getYear()
* The RTClib::now() function is not accessed through the DS3231 object. Rather, it has a very specific syntax as described below in <a href="#RTClib_now_function">The Special RTClib::now() Function</a>.
Set the Date or Time
We emphasize here and elsewhere that the code writer bears responsibility to ensure that the values passed into the following functions fall within the valid range, as specified in the documentation for each function.
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
