{"id":18591195,"url":"https://github.com/NorthernWidget/DS3231","last_synced_at":"2025-04-10T15:31:38.686Z","repository":{"id":1616404,"uuid":"12749980","full_name":"NorthernWidget/DS3231","owner":"NorthernWidget","description":"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","archived":false,"fork":false,"pushed_at":"2025-02-12T15:29:52.000Z","size":177,"stargazers_count":193,"open_issues_count":13,"forks_count":85,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-02-12T16:25:31.053Z","etag":null,"topics":["clock","i2c","rtc"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NorthernWidget.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-09-11T07:13:53.000Z","updated_at":"2025-02-12T15:29:58.000Z","dependencies_parsed_at":"2025-02-14T10:30:33.931Z","dependency_job_id":null,"html_url":"https://github.com/NorthernWidget/DS3231","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NorthernWidget%2FDS3231","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NorthernWidget%2FDS3231/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NorthernWidget%2FDS3231/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NorthernWidget%2FDS3231/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NorthernWidget","download_url":"https://codeload.github.com/NorthernWidget/DS3231/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248243452,"owners_count":21071054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clock","i2c","rtc"],"created_at":"2024-11-07T01:01:20.124Z","updated_at":"2025-04-10T15:31:38.391Z","avatar_url":"https://github.com/NorthernWidget.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# DS3231 Library\nAn Arduino library for the DS3231 real-time clock (RTC).  \n\n## Description\nThe library provides easy-to-use methods to:\n\n* set and read the current date and time;\n* set, clear, and detect two, independent, Time-of-Day alarms;\n* perform certain conversions and calculations with time data;\n* manage certain hardware inside the DS3231 RTC module.\n\nThis document explains the installation and usage of the Library with the Arduino IDE. \n\nYou do have to install the Library in your Arduino IDE environment before you can use it. Installation instructions are provided, below.\n\n## Contents\n\n* [Summary](#summary)\n* [How to Cite](#how-to-cite)\n* [About the DS3231](#about-the-ds3231)\n* [How to Install the Library](#installation)\n* [Functions Provided in the Library](#functions)\n* [Examples of Using the Library](#examples-of-use)\n* [Helpful Resources](#additional-resources-and-references)\n* [Contributing, Credits and License](#contributing)\n* [To-Do List](#to-do)\n\n\n\u003chr\u003e\n\n## Summary\n\nAfter installing the Library in your Arduino IDE, using it in a program starts with three, simple steps:\n\n\u003col start=\"1\"\u003e \n  \u003cli\u003eImport the Library into the program code:\u003c/li\u003e\n\u003c/ol\u003e\n\n\n```\n#include \u003cDS3231.h\u003e\n```\n\n\u003col start=\"2\"\u003e\n  \u003cli\u003eDeclare a DS3231 object, for example:\u003c/li\u003e\n\u003c/ol\u003e\n\n\n```\nDS3231 myRTC;\n```\n\n\u003col start=\"3\"\u003e\n  \u003cli\u003eStart the Wire library to enable I2C communications with the DS3231 hardware, typically in the setup() code block:\u003c/li\u003e\n\u003c/ol\u003e\n\n\n```\nWire.begin();\n```\n\nThen, 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):\n\n\n```\nunsigned int theDate = myRTC.getDate();\n```\n\nThe Library incorporates two other classes to assist with managing date and time data:\n\n* ```DateTime``` enables 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:\n    1. as distinct values for year, month, day, hour, minute and second, or\n    2. as a single, unsigned integer. The latter is handy for doing arithmetic with dates.\n* ```RTClib``` institutes a convenient ```RTClib::now()``` function for receiving a date/time snapshot, as a DateTime object, from the DS3231 device.\n\n## How to Cite\n\nIf you use this library in a publication, please cite it in one or both of the following two ways:\n1. Through the `CITATION.cff` file here, which should be up to date with the permanent archive available from Zenodo\n2. If you need an academic journal reference and/or you are discussing the integration of the DS3231 into a larger hardware + firmware ecosystem,\u003cbr/\u003e\n**Wickert, A. D., Sandell, C. T., Schulz, B., \u0026 Ng, G. H. C. (2019), [Open-source Arduino-compatible data loggers designed for field research](https://hess.copernicus.org/articles/23/2065/2019/), *Hydrology and Earth System Sciences*, *23*(4), 2065-2076, doi:10.5194/hess-23-2065-2019.**\u003cbr/\u003e\nThis option should not be the only one used because it does not credit the original library developer, Eric Ayars.\n\n## About the DS3231\nDS3231 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. \n\nThe 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. \n\nSeveral 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.\n\nDS3231 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.\n\nAccording to the [datasheet](https://datasheets.maximintegrated.com/en/ds/DS3231-DS3231S.pdf), the DS3231 hardware \"completely manages all timekeeping functions (including):\n\n* Seconds, \n* Minutes, \n* Hours\n    * 12-hour format with AM/PM indication, or\n    * 24-hour format,\n* Day of the Week,\n* Date of the Month, \n* Month,  and\n* Year, with Leap-Year Compensation Valid Up to 2100\"\n\nData 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.\n\nThe 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.\n\nThe oscillator can be accessed directly, independent of the date and time registers, for use as an external timer or source of interrupts.\n\nThe temperature can be read from the DS3231 using a Library function. The data sheet declares it to be accurate to within 3 degrees, Celsius. \n\n\n### Power Supply and Battery Backup\nThe 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\u003csub\u003eCC\u003c/sub\u003e, and a secondary, backup source, V\u003csub\u003eBAT\u003c/sub\u003e.\n\nSome popular modules mounting a DS3231 provide a receptacle for a coin battery, attaching it to the V\u003csub\u003eBAT\u003c/sub\u003e pin. If a sufficiently-charged battery is present, the DS3231 will switch automatically to the battery after detecting a drop in V\u003csub\u003eCC\u003c/sub\u003e voltage below a certain \"power-fail\" level.\n\nIt will switch back to V\u003csub\u003eCC\u003c/sub\u003e automatically, if and when that voltage rises back up above both the power-fail and the battery voltage level. \n\nOne 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.\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n\n## Installation\n\n### First Method\n\n![image](https://user-images.githubusercontent.com/36513474/68038497-eae68200-fceb-11e9-9812-b47ff6e06e2f.png)\n\n1. In the Arduino IDE, navigate to Sketch \u003e Include Library \u003e Manage Libraries\n1. Then the Library Manager will open and you will find a list of libraries that are already installed or ready for installation.\n1. Then search for DS3231 using the search bar.\n1. Click on the text area and then select the specific version and install it.\n\n### Second Method\n\n1. Navigate to the [Releases page](https://github.com/NorthernWidget/DS3231/releases).\n1. Download the latest release.\n1. Extract the zip file\n1. In the Arduino IDE, navigate to Sketch \u003e Include Library \u003e Add .ZIP Library\n\n### Dependencies\n\nThe 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:\n\n* Wire.h : a widely-used Arduino library for I2C communications\n* time.h : a modified C language header file packaged with avr-libc and the AVR-GCC compiler\n\nNote: 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.\n\nA 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.\n\n```\n#include \u003cWire.h\u003e\n#include \u003ctime.h\u003e\nvoid setup() {}\nvoid loop() {}\n```\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## Functions\nReaders are encouraged to visit the [Documentation folder](https://github.com/NorthernWidget/DS3231/tree/master/Documentation) for detailed information about the functions in this Library. Additional information is available in the [Examples of Use](#examples-of-use) described below, and in the code source files of this repository:\n\n* [DS3231.h](https://github.com/NorthernWidget/DS3231/blob/master/DS3231.h)\n* [DS3231.cpp](https://github.com/NorthernWidget/DS3231/blob/master/DS3231.cpp)\n\n### [Read the Date or Time](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md)\n- [RTClib::now() \u003csup\u003e\\*\u003c/sup\u003e](https://github.com/NorthernWidget/DS3231#the-special-rtclibnow-function-)\n- [getSecond()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getsecond)\n- [getMinute()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getminute)\n- [getHour(bool, bool)](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#gethour)\n- [getDoW()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getdow)\n- [getDate()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getdate)\n- [getMonth(bool)](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getmonth)\n- [getYear()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Retrieval.md#getyear)\n\n\\* The *RTClib::now()* function is not accessed through the DS3231 object. Rather, it has a very specific syntax as described below in \u003ca href=\"#RTClib_now_function\"\u003eThe Special RTClib::now() Function\u003c/a\u003e.\n\n\n### [Set the Date or Time](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md)\nWe 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.\n\nUnexpected values in the DS3231 hardware registers may follow from the insertion of an invalid parameter into any one of these functions.\n\n- [setEpoch()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setepochtime_t-epoch--0-bool-flag_localtime--false)\n- [setSecond()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setsecondbyte-second)\n- [setMinute()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setminutebyte-minute)\n- [setHour()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-sethourbyte-hour)\n- [setDoW()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setdowbyte-dow)\n- [setDate()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setdatebyte-date)\n- [setMonth()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setmonthbyte-month)\n- [setYear()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setyearbyte-year)\n- [setClockMode()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Time-Set.md#void-setclockmodebool-h12)\n\n### [Set, Clear and Check Alarms](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md)\nThe following functions set and retrieve time and date values in the DS3231 hardware alarm registers. \n\nParameters include a special 8-bit value named \"AlarmBits\". Readers may find additional information about it at the following links: [Alarm Bits Quick Reference](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#alarm-bits-quick-reference), and [Alarm Bits in Detail](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#alarm-bits-in-detail).\n\n- [getA1Time()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#geta1time)\n- [getA1Time() with Option](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#geta1time-with-option)\n- [getA2Time()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#geta2time)\n- [getA2Time() with Option](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#geta2time-with-option)\n- [setA1Time()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#seta1time)\n- [setA2Time()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#seta2time)\n\nThe remaining functions in this group set and retrieve certain flags in the DS3231 hardware that govern or report the operation of the alarms.\n\n- [turnOnAlarm()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#turnonalarm)\n- [turnOffAlarm()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#turnoffalarm)\n- [checkAlarmEnabled()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#checkalarmenabled)\n- [checkIfAlarm()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#checkifalarm)\n- [checkIfAlarm() with Option](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Alarms.md#checkifalarm-with-option)\n\n### [Manage DS3231 Hardware](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Utilities.md)\nThe functions in this group support uses for a DS3231 other than as an alarm clock.\n\n- [getTemperature()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Utilities.md#gettemperature)\n- [enableOscillator()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Utilities.md#enableoscillator)\n- [enable32kHz()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Utilities.md#enable32khz)\n- [oscillatorCheck()](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/Utilities.md#oscillatorcheck)\n\n### [DateTime Object](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/DateTime.md)\nA limited DateTime class is defined in this DS3231.h library. The link, above, provides more information about the class. \n\n[Retrieving Date and Time Data](https://github.com/NorthernWidget/DS3231/blob/master/Documentation/DateTime.md#retrieving-date-and-time-data) further documents the DateTime class methods listed below. \n\n- year()\n- month()\n- day()\n- hour()\n- minute()\n- second()\n- unixtime()\n\n\u003ch3 id=\"RTClib_now_function\"\u003eThe Special \u003ccode\u003eRTClib::now()\u003c/code\u003e Function \u003c/h3\u003e\n\n```RTClib::now()``` is the precise, complete name for a special function that returns a ```DateTime``` object from the DS3231. Always write it just so: ```RTClib::now()```.\n\nThe function returns a DateTime object. To use it in your program, declare a DateTime type of variable to receive the value. For example, \n\n```DateTime currentMoment = RTClib::now();```\n\nThe value of ```currentMoment``` can then be accessed as either:\n* an unsigned integer containing the number of seconds since a certain reference date, or\n* distinct values for Year, Month, Day, Date, Hour, Minute, or Second.\n\n[back to the list of functions](#functions)\u003cbr\u003e\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## Examples of Use\n\nThere are many examples provided in the [examples](https://github.com/NorthernWidget/DS3231/tree/master/examples) folder of this repository.  At the time of writing the examples include:\n\n* *set*: demonstrates selected time-setting functions\n* *test*: demonstrates selected time-reading functions\n* *echo*: demonstrates setting the time and date then reading it back\n* *echo_time*: similar to *echo*, demonstrates setting and reading time/date data\n* *oscillator_test*: demonstrates advanced techniques for managing and using the DS3231 device as a pulse generator\n\nFuture development plans include updating these examples and adding more of them.\n\nSee also [Working with the DS3231 libraries and interrupts](https://github.com/IowaDave/RTC-DS3231-Arduino-Interrupt), a tutorial provided by [IowaDave](https://github.com/IowaDave).\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## Additional Resources and References\n\n* [Maxim DS3231 Data Sheet](https://datasheets.maximintegrated.com/en/ds/DS3231-DS3231S.pdf)\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## Contributing\n\nIf you want to contribute to this project:\n\n- Report bugs and errors\n- Ask for enhancements\n- Create issues and pull requests\n- Tell others about this library\n- Contribute new protocols\n\nPlease read [CONTRIBUTING.md](https://github.com/NorthernWidget/DS3231/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## Credits\nThis is a splice of [Ayars'](http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html) and [Jeelabs/Ladyada's](https://github.com/adafruit/RTClib) libraries.\n\nThe authors of this library are A. Wickert \u003cawickert@umn.edu\u003e, E. Ayars, J. C. Wippler, N. W. LLC \u003cinfo@northernwidget.com\u003e and it is maintained by A. Wickert. This library is released into the public domain by Jeelabs, Ladyada, and E. Ayars.\n\nBased on previous work by:\n\n- S. T. Andersen\n- SimGas\n- Per1234\n- Glownt\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## License\n\nDS3231 is licensed under [The Unlicense](https://github.com/NorthernWidget/DS3231/blob/master/LICENSE).\n\n[back to top](#ds3231-library)\n\u003chr\u003e\n\n## To Do\nA project is underway to update the library's documentation.\n\n### Existing Repo Issues to be addressed:\n* \\#42 Alarm Documentation\n* \\#24 DateTime Comparison Operators\n* \\#20 AlarmBits Documentation\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNorthernWidget%2FDS3231","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNorthernWidget%2FDS3231","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNorthernWidget%2FDS3231/lists"}