DS3231 RTC Real Time Clock Module, you can integrate it into your projects so that they have the ability to store and keep track of date and time through the I2C communication interface.
The module is based on the RTC DS3231 from MAXIM and the EEPROM AT24C32 from ATMEL. Both integrated circuits share the same communication bus with the I2C Protocol. The RTC DS3231 is the evolution of the classic RTC DS1307. The main difference with the DS1307 is the internal temperature compensated oscillator, which makes its precision very high. The AT24C32 EEPROM memory allows 32Kbits (4K Bytes) of data to be stored permanently.
Note
The CR2032 battery is used so that the module can maintain the current date and time when it has no power, it does NOT serve as auxiliary power
SPECIFICATIONS AND FEATURES
DC Supply Voltage: 3.3V ~ 5V
Communication Type: I2C
Low energy consumption
Compatible with Arduino, Raspberry Pi and other boards.
Clock Accuracy: 2ppm
DS3132 I2C address: Read(11010001) Write(11010000)
AT24C32 EEPROM memory (4K * 8bit = 32Kbit = 4KByte)
DS3231 High Precision RTC with Internal Oscillator
It can be used in cascade with another I2C device, the address of the AT24C32 can be modified (default is 0x57)
Does not include CR2032 battery
Dimensions: 38mm x 22mm x 12mm
RTC Features
Date with seconds, minutes, hours, day number, day of the week, month and year.
Leap year compensation.
Time format configurable in 12 or 24 hours.
2 configurable alarms
Temperature compensation circuit for internal reference voltage
Test
The following connection diagram and code is to corroborate the operation of the DS3231 module, the connections will be made to have communication via I2C using an Arduino UNO:
Code
The following program will help us display the data on the serial monitor of the Arduino IDE at 9600 baud, showing the current date in format: YEAR, MONTH, DAY, WEEKDAY, HOUR, MINUTE, SECOND and additionally includes a function to program the hour and minute where you want to turn on LED 13 of the Arduino.
#include < Wire .h> #include " RTClib.h " RTC_DS3231 rtc ; String week[7] = { " Sunday ", " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday " }; String monthsNames[12] = { " January ", " February ", " March ", " April ", " May ", " June ", " July ", " August ", " September ", " October ", " November " , " December " }; bool alarm = true ; void setup(){ Serial.begin(9600);//We enable the Arduino IDE serial port at 9600 bps delay(1000);//We wait 1 second if(!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while(1); } // If power is lost, set date and time if ( rtc . lostPower ()) { // Set to compile date and time rtc . adjust ( DateTime (F( __DATE__ ), F( __TIME__ ))); // Set to specific date and time. In the example, January 21, 2023 at 03:00:00 // rtc.adjust(DateTime(2023, 1, 21, 3, 0, 0)); } pinMode ( LED_BUILTIN , OUTPUT ); } void date( DateTime date ){
Serial
. print ( date . year (), DEC ); Serial . print (' / ');
Serial
. print ( date . month (), DEC ); Serial . print (' / ');
Serial
. print ( date . day (), DEC ); Serial . print (" (" );
Serial
. print (week[ date . dayOfTheWeek ()]); Serial . print ( " )" );
Serial
. print ( date . hour (), DEC ); Serial . print (' : ');
Serial
. print ( date . minute (), DEC ); Serial . print (' : ');
Serial
. print ( date . second (), DEC ); Serial . println (); if ( date . hour () == 16 && date . minute () == 21) { if (alarm = true ) { Serial . println (" Alarm "); digitalWrite ( LED_BUILTIN , HIGH ); alarm = false ; //deactivates alarm } delay (1000); //setting alarm deactivation at 14hr and 49 min if ( date . hour () == 16 && date . minute () == 22) digitalWrite ( LED_BUILTIN , LOW ); alarm = true ; } } void loop () { DateTime now = rtc . now (); date( now ); delay (3000); }
The battery is used so that the module can maintain the current date and time when the module or the Arduino runs out of power, the battery will not keep the module on , to make it continue counting the time, you must Comment the following line of code and reload the program:
rtc . adjust ( DateTime (F( __DATE__ ), F( __TIME__ )));