To come in
All computer secrets for beginners and professionals
  • Towards the launch of new Russian over-the-horizon radars. Main tasks solved by radars
  • Meaning of the obturator foramen in medical terms
  • Working with new words
  • Sapphire lessons. PC sapphire. Wide range of tools
  • How to work with decoupage cards
  • How to top up your Internet account at Smile
  • We synchronize the time on the DS3231 module with the computer. Connection diagram for the DS3231 real-time clock and a simple program

    We synchronize the time on the DS3231 module with the computer.  Connection diagram for the DS3231 real-time clock and a simple program

    ". Let's get acquainted with the DS3231 real-time clock module. The article contains video instructions, program listings, purpose and methods of connecting modules from the DS family to Arduino.

    DS3231 Real Time Clock Module

    What is DS3231 Real Time Clock Module?

    Real time clock module- this is an electronic circuit designed to record chronometric data (current time, date, day of the week, etc.), and is a system consisting of an autonomous power source and a recording device.

    DS3231 module It is essentially an ordinary watch. Arduino boards already have a built-in time sensor Millis, however, it only works when power is applied to the board. If you turn off and then turn on the Arduino, the Millis time will be reset to zero. And the DS3231 has a battery on board, which, even when the Arduino board is disconnected, continues to “power” the module, allowing it to measure time.

    The module can be used as a clock or alarm clock based on Arduino boards. Or as an alert for various systems, for example in a Smart Home.

    DS3231 Specifications:

    • the module calculates hours, minutes, seconds, dates, months, years (leap years are taken into account until 2100);
    • To connect to various devices, the clock is connected via an I2C interface.

    32K— Output designed to supply external power >12V.

    S.Q.W.— Programmable Square-Wave signal output.

    SCL– Through this pin, data is exchanged with the clock via the I2C interface.

    S.D.A.– Data from the watch is transmitted through this pin.

    VCC– Power supply for real time clock, 5 volts required. If there is no voltage supplied to this pin, the watch goes into sleep mode.

    GND- Earth.

    Connection diagram for the DS3231 real-time clock and a simple program

    SDA and SCL pins on different Arduino boards:

    S.D.A. SCL
    UNO A4 A5
    Mini A4 A5
    Nano A4 A5
    Mega2560 20 21
    Leonardo 2 3

    Let's connect the real-time clock module to the Arduino UNO. SDA - pin A4, SCL - pin A5.

    The following program is suitable for the model to work (you can simply copy the program into the Arduino IDE):

    #include

    void setup() (
    delay(300);
    Serial.begin(9600);
    time.begin();
    }
    void loop()



    }
    }

    In this sketch, the time is simply counting down.

    First of all, in sktech, connect the library iarduino_RTC.h.

    There, indicate the exact name of your module to work with it correctly.

    As a result, we get the time output from the DS3231 module to the port monitor. Hours, minutes, seconds are displayed.

    In the next sketch we will add a function settime, which allows you to set the initial countdown time.

    #include
    iarduino_RTC time(RTC_DS3231);
    void setup() (
    delay(300);
    Serial.begin(9600);
    time.begin();
    time.settime(0,0,18,24,04,17,1); // 0 sec, 0 min, 18 hours, April 24, 2017, Monday
    }
    void loop()
    if(millis()%1000==0)( // if 1 second has passed
    Serial.println(time.gettime("d-m-Y, H:i:s, D")); // display time
    delay(1); // pause for 1 ms so as not to display the time several times in 1 ms
    }
    }

    In the example, time starts counting from 0 sec, 0 min, 18 o'clock, April 24, 2017, Monday.

    Lesson posts:

    1. First lesson: .
    2. Second lesson: .
    3. Third lesson: .
    4. Fourth lesson: .
    5. Fifth lesson: .
    6. Lesson six: .
    7. Seventh lesson: .
    8. Eighth lesson: .
    9. Ninth lesson:

    Description

    Real-time clock module with independent power supply. Arduino/Genuino controllers do not have a built-in real time clock. To work with time, there is the millis() function. However, for projects where time and date are required, the capabilities of this function are not enough and a real-time clock comes to the rescue.

    The DS3231 module is a low-cost, accurate, I2C real-time clock with TCXO temperature compensation. The device contains a connector for a CR2032 battery and maintains accurate timekeeping when the device's power is interrupted. The clock supports information about seconds, minutes, hours, day, month and year. The end of month date is automatically adjusted for months containing fewer than 31 days, including adjustment for leap year. They operate in one of the modes: 24- or 12-hour format (with AM/PM indicator). They have two programmable alarms.

    Main characteristics of the watch:

      Real time clock with counting seconds, minutes, hours, day, month and year (with date adjustments including leap years until 2100)

      Travel error: ±2 minutes per year

      Temperature sensor with an error of ±3°С

      Two alarm clocks

    Specifications

      Operating temperature: -40°С - +85°С

      Supply voltage: 2.3 - 5.5 V

      Battery supply voltage: 2.3 - 5.5 V

      Maximum current consumption: 650 nA

    Physical Dimensions

      Module (L x W x H): 45 x 23 x 15 mm

    Pros of use

      High watch accuracy

      There are two alarm clocks with interrupt function

      Wide operating temperature range

    Disadvantages of use

      More expensive than some analogues

    Library for working with the module

    Examples of connection and use

    Example 1: The example illustrates connecting a clock module to the controller, setting the time and date on the clock, outputting the time, date and day of the week to the Serial port monitor, as well as receiving the temperature from the current module. (Examples were tested on the Smart UNO controller)

    Connection diagram:

    Sketch to download:

    //Connecting libraries#include #include "DS3231.h" DS3231 RTC; //Create object DS3231 char weekDay [ 4 ] = ( "Sun" , "Mon" , "Tue" , ​​"Wed" , "Thu" , "Fri" , "Sat" ) ; //Creating an array of days of the week //Creating a variable of type DateTime to set the date in the format: //year, month, day, hour, minute, second and day of the week (from 0 - Sunday to 6 - Saturday) DateTime dt(2016, 8, 22, 16, 10, 0, 1); void setup () ( Serial . begin ( 9600 ) ; //initializing the Serial port Wire.begin(); //initialize the Wire library RTC.begin(); //initialize the clock RTC.adjust (dt) ; //Set the date-time as it is specified in the dt variable) void loop () ( DateTime now = RTC.now () ; //getting the current date and time //output the received year Serial.print(now.year(), DEC); Serial.print("/"); //output the month Serial .print (now.month () , DEC) ; Serial.print("/"); //day Serial .print (now.date () , DEC) ; Serial.print(" "); //hour Serial .print (now.hour () , DEC) ; Serial.print(":"); //minutes Serial .print (now.minute () , DEC) ; Serial.print(":"); //seconds Serial .print (now.second () , DEC) ; Serial.println(); //day of the week Serial .print (weekDay[ now.dayOfWeek () ] ) ; Serial.println(); delay(1000); //delay for 1 second RTC.convertTemperature(); //conversion of the current temperature in registers Serial.print(RTC.getTemperature()); //read registers and output the resulting temperature Serial.println("C"); delay(1000); //delay for 1 second }

    Today we will continue our search for the perfect real time clock (RTC) chip. We will make watches based on . The display will be more convenient for development - an LCD display, which will display all the information at once except the settings. In this form, the clock is convenient to use as a desktop option.

    So, let's look at the DS3231 chip itself. The DS3231 is a real-time clock with extremely precise movement (the manufacturers chose the word) thanks to a built-in quartz resonator with temperature compensation. The data transfer interface is I 2 C. This microcircuit also has an input for the backup battery voltage; when the main power is turned off, the microcircuit automatically switches to operation from the backup battery, the accuracy of operation from the backup battery is not affected. It’s very pleasing, isn’t it? The DS3231 supports counting seconds, minutes, hours, days of the month (date), days of the week, months, and years (including leap year for months). Supports work in 12 and 24 hour format. There are 2 alarm clocks with the ability to configure them and monitor their status. Adjustment of temperature compensation accuracy. And also two outputs - at 32 kHz (the output is 32.768 kHz) and a programmable output from 1 Hz to 8.192 kHz. There is also a reset pin - RST. The real-time clock chip is available in an SO-16 package. The case is quite large, but if you consider that there is already quartz inside, and it is also temperature compensated, then it seems to me that everything is fine with the dimensions. DS3231 has a twin in the form of DS3232, which, however, has 2 more legs. All this is very reminiscent of NXP products - PCA2129 and PCF2129 clock chips. Similar temperature-compensated built-in quartz resonator, both are the same twins only with different numbers of n.c. pins and similar functions relative to the DS3231 in addition to timekeeping.

    RTC DS3231 are available for sale in the form of modules with the necessary wiring, as well as complete with an EEPROM chip, which most often is not needed, it only adds weight:

    In addition to the necessary parts, there is also an LED on the module board, the function of which is to indicate the connection of power to the terminals. They probably just delivered it for beauty.

    What is important to know when working with such a real-time clock chip is how to extract data from it or write it there. The clock has an I 2 C interface. In order to write data (and this is also necessary in order to read the data), you need to pass the start condition (these commands are carried out using hardware or software I 2 C for the microcontroller), then pass the address of the chip with the bit record, then pass the address of the register to which we will access and then transfer a byte of data to this register, if you then transfer another byte of data, it will be written to the next register, and so on. When finished, you need to pass a stopping condition. Graphic representation of the above in the figure:

    Data recording is required for initial setup as well as for setting the current time. Next, we need to constantly receive data about the current time and date. To do this, it is necessary to read from the registers storing this information. Reading consists of two procedures - setting a pointer to the desired register and reading it. To set a pointer to the desired register, you need to pass the start condition, then pass the address of the chip with the write bit and a byte with the register address. Next is either a stop condition and then a start condition, or just a restart. Now the second procedure is directly reading from registers. The start is transmitted, then you need to send the address of the microcircuit with a read bit and then read the registers in the required number, and upon completion, transmit the stop condition. If information from the register has been read, the pointer automatically moves to the next register without unnecessary actions on the part of the microcontroller (device master). The figure illustrates everything said above regarding reading registers using the I 2 C interface:

    Chip address:

    • for recording - 0b11010000
    • for reading - 0b11010001

    The C code will look like this:

    // functions with clock ============================================================ ===================================================== ======== // initializing initial settings void RTC_init(void)( i2c_start_cond(); // starting i2c i2c_send_byte(RTC_adr_write); // transferring the device address, recording mode i2c_send_byte(0x0E); // transferring the memory address i2c_send_byte(0b00100000); // start temperature conversion and output at 1 Hz i2c_send_byte(0b00001000); // enable 32 kHz output i2c_stop_cond(); // stop i2c ) // get time and date void RTC_read_time(void)( i2c_start_cond() ; // start i2c i2c_send_byte(RTC_adr_write); // transfer the device address, write mode i2c_send_byte(0x00); // transfer the memory address i2c_stop_cond(); // stop i2c i2c_start_cond(); // start i2c i2c_send_byte(RTC_adr_read); / / transmitting device address, reading mode sec = bcd(i2c_get_byte(0)); // reading seconds, ACK min = bcd(i2c_get_byte(0)); // reading minutes, ACK hour = bcd(i2c_get_byte(0)); / / read clock, ACK wday = bcd(i2c_get_byte(0)); // reading the day of the week, ACK day = bcd(i2c_get_byte(0)); // reading the number, ACK month = bcd(i2c_get_byte(0)); // reading month, ACK year = bcd(i2c_get_byte(1)); // reading year, NACK i2c_stop_cond(); // stop i2c ) // set the time void RTC_write_time(unsigned char hour1, unsigned char min1, unsigned char sec1)( i2c_start_cond(); // start i2c i2c_send_byte(RTC_adr_write); // transfer the device address, recording mode i2c_send_byte(0x00) ; // transfer of memory address i2c_send_byte(bin(sec1)); // 0x00 seconds (is it advisable to also specify seconds?) i2c_send_byte(bin(min1)); // 0x01 minutes i2c_send_byte(bin(hour1)); // 0x02 clock i2c_stop_cond(); // stop i2c ) // set the date void RTC_write_date(unsigned char wday, unsigned char day, unsigned char month, unsigned char year)( i2c_start_cond(); // start i2c i2c_send_byte(RTC_adr_write); // transfer device addresses, recording mode i2c_send_byte(0x03); // transfer of memory address i2c_send_byte(bin(wday)); // 0x03 day of the week (Sunday - 1, Mon 2, Tue 3, Wed 4, Thu 5, Fri 6, Sat 7 ) i2c_send_byte(bin(day)); // 0x04 day month i2c_send_byte(bin(month)); // 0x05 month i2c_send_byte(bin(year)); // 0x06 year i2c_stop_cond(); // stop i2c ) // read the temperature void RTC_read_temper(void)( i2c_start_cond(); // start i2c i2c_send_byte(RTC_adr_write); // transfer the device address, write mode i2c_send_byte(0x11); // transfer the memory address i2c_stop_cond(); // stop i2c i2c_start_cond(); // start i2c i2c_send_byte(RTC_adr_read); // transmit device address, read mode t1 = i2c_get_byte(0); // read MSB temperature t2 = i2c_get_byte(1); // read LSB temperature i2c_stop_cond (); // stop i2c t2=(t2/128); // shift by 6 - precision 0.25 (2 bits) // shift by 7 - precision 0.5 (1 bit) t2=t2*5; )

    This is all the source code used to work with the microcircuit; adjusting the clock speed was not affected, since the clock had not lost a second in several days.

    Yes - a great feature DS3231 is that the same chip performs the functions of a thermometer (otherwise how else to carry out temperature compensation) and the ability to read the current temperature. The maximum temperature resolution is 0.25 degrees Celsius. Also, the temperature update period is quite long - about 1 minute. Yes, we have no need to update it quickly.

    The diagram of the entire clock structure looks like this:

    The microcontroller was chosen by Atmega8 for its widespread availability and low price. This microcontroller can be used both in a DIP-28 package and in an SMD version in a TQFP-32 package. Resistor R3 is necessary to prevent spontaneous restart of the microcontroller in the event of random noise on the PC6 pin. Resistor R3 pulls the power plus to this pin, reliably creating a potential across it. A liquid crystal display (LCD) is used for display. I used the 2004A display - 4 lines of 20 characters are more for beauty, so you can use a more familiar display - 2 lines of 16 characters. The LCD display is connected to the microcontroller using a four-bit system. Variable resistor R2 is necessary to adjust the contrast of the characters on the display. By rotating the slider of this resistor we achieve the clearest readings on the screen for us. The backlight of the LCD display is organized through pins “A” and “K” on the display board. The backlight is turned on through a current-limiting resistor - R1. The higher the value, the dimmer the display will be backlit. However, this resistor should not be neglected to avoid damage to the backlight. Buttons S1 - S4 control the clock settings. The LED indicates that the alarm has gone off. The LED can be replaced with some kind of sound circuit. Resistors R5 - R8 are pull-up and are necessary for the formation of rectangular pulses at the terminals of the clock chip. This is also necessary for the correct operation of the I2C protocol. To power the circuit, a linear stabilizer chip L7805 is used; it can be replaced with a domestic analogue of the five-volt linear stabilizer KR142EN5A, or you can use another voltage stabilizer chip in accordance with its connection in the circuit (for example, LM317 or switching stabilizers LM2576, LM2596, MC34063, and so on). Next, 5 volts are stabilized by another microcircuit - AMS1117 in a version that gives an output of 3.3 volts. The clock chip, according to the datasheet, is powered by a voltage of 3.3 volts. However, the maximum voltage is 5.5 volts. Therefore, this stabilizer can be used or not, at your discretion. The AMS1117 voltage stabilizer can also be replaced with the ADJ version (AMS1117ADJ) - that is, an adjustable version, you will need to set the required voltage with this choiceusing two resistors connected to the microcircuit in accordance with its datasheet.

    The circuit was assembled and debugged using a development board for the ATmega8 microcontroller:

    Purpose of the buttons:

    • S1 - turns off the alarm signal, or exits to the main menu from any settings menu
    • S2- microcontroller reset
    • S3 - changes the time or date in the settings menu
    • S4 - enter the settings menu and scroll through the menu

    The 32 kHz pin can be used to control the crystal frequency. We connect a frequency meter or oscilloscope to this pin and control the frequency:

    As can be seen from the screenshot of the oscillogram, the frequency approximately corresponds to 32.768 kHz (approximately due to limitations in the resolution of frequency measurements, and it is difficult to determine so accurately “by eye”).

    The result was a watch with the following characteristics:

    • time indication
    • date display
    • day of week indication
    • alarm clock activity indication
    • 1 alarm clock with signal output from a microcontroller
    • indication of ambient temperature (only positive temperature is implemented in software; negative temperature, I think, is of no use to us)
    • alarm settings
    • time settings
    • date settings
    • LCD display with backlight
    • saving settings and continuing the clock when the main power is turned off

    Let's summarize. The DS3231 real-time clock chip is an excellent solution. The accuracy is comparable to some DS1307 or higher, but the PCA/PCF2129 can still compete with it. Among the real-time clock chips I have reviewed, this instance currently ranks first in terms of functionality and accuracy.

    To program the Atmega8 microcontroller, you need to know the configuration of the fuse bits (screenshot taken in the program):

    The article is accompanied by firmware for the Atmega8 microcontroller, a circuit design in the program, as well as a video of the clock working (at the very beginning the alarm will go off - the LED will light up).

    List of radioelements

    Designation Type Denomination Quantity NoteShopMy notepad
    IC1 MK AVR 8-bit

    ATmega8

    1 To notepad
    IC2 Real Time Clock (RTC)

    DS3231

    1 To notepad
    VR1 Linear regulator

    L7805AB

    1 To notepad
    VR2 Linear regulator

    AMS1117-3.3

    1 To notepad
    VD1 Rectifier diode

    1N4148

    1 To notepad
    C1 470 µF1 To notepad
    C2, C3, C5, C7 Capacitor100 nF4 To notepad
    C4 Electrolytic capacitor220 µF1 To notepad
    C6, C8 Electrolytic capacitor10 µF2 To notepad
    R1 Resistor

    22 Ohm

    1 To notepad
    R2 Trimmer resistor10 kOhm1 3296W-1-103LF

    The DS3231 chip is a high-precision RTC real-time clock that has a built-in temperature-compensated quartz oscillator, resulting in time drift of only ±2 minutes per year. Additionally, an alarm function is implemented, and there is also an interrupt output. The clock can be purchased as a ready-made Arduino module with strapping elements and a battery compartment.

    I ordered the module here. The diagram is shown in the picture below:


    The microcircuit uses the widely used . Supports standard (100 kHz) and high (400 kHz) data rates. The microcircuit address (7 bits) on the I2C bus is 1101000. Additionally, the module has I2C memory (24C32), not shown in the diagram.

    Power Modes

    The supply voltage of the microcircuit can be in the range of 2.3...5.5V, there are two power lines, for an external source (Vcc line), as well as for the battery (Vbat). The external source voltage is constantly monitored, and when it drops below the threshold Vpf=2.5V, it switches to the battery line. The following table shows the conditions for switching between power lines:

    The watch's accuracy is maintained by monitoring the ambient temperature. The microcircuit starts an internal procedure for adjusting the frequency of the clock generator; the amount of adjustment is determined using a special graph of frequency versus temperature. The procedure starts after power is applied and then runs every 64 seconds.

    In order to conserve charge, when the battery is connected (voltage is applied to the Vbat line), the clock generator does not start until the voltage on the Vcc line exceeds the threshold value Vpf, or the correct address of the microcircuit is transmitted via the I2C interface. The clock generator startup time is less than one second. Approximately 2 seconds after power is applied (Vcc), or the address is received via the I2C interface, the frequency correction procedure starts. Once the clock generator has started, it continues to operate as long as Vcc or Vbat is present. When turned on for the first time, the date and time registers are reset and have the following values: 01/01/00 – 01 – 00/00/00 (day/month/year/ – day of the week – hour/minutes/seconds).

    The current consumption when powered by a 3.63V battery is 3 µA, in the absence of data transmission via the I2C interface. The maximum current consumption can reach 300 µA when using an external 5.5V power supply and high I2C data transfer speed.

    External reset function

    The RST line can be used for external reset and also has a low voltage alarm function. The line is pulled high through an internal resistor; no external pull-up is required. To use the external reset function, a button can be connected between the RST line and the common wire; the microcircuit has contact bounce protection. The alarm function is activated when the supply voltage Vcc drops below the threshold value Vpf, while the RST line is set to a low logic level.

    Description of DS3231 registers

    The table below shows a list of real-time clock registers:

    AddressD7D6D5D4D3D2D1D0FunctionLimits
    0x000 10 SecondsSecondsSeconds00-59
    0x010 10 minutesminutesminutes00-59
    0x020 12/24 AM/PM10 hoursHourWatch1-12 + AM/PM or 00-23
    10 hours
    0x030 0 0 0 0 DayDay of the week1-7
    0x040 0 10thNumberdate01-31
    0x05Century0 0 10 monthMonthMonths/century01-12 + Century
    0x0610 yearsYearYears00-99
    0x07A1M110 SecondsSecondsSeconds, 1st alarm00-59
    0x08A1M210 minutesminutesMinutes, 1st alarm00-59
    0x09A1M312/24 AM/PM10 hoursHourClock, 1st alarm1-12 + AM/PM or 00-23
    10 hours
    0x0AA1M4DY/DT10thDayDay of the week, 1st alarm1-7
    NumberDate, 1st alarm01-31
    0x0BA2M210 minutesminutesMinutes, 2nd alarm00-59
    0x0CA2M312/24 AM/PM10 hoursHourClock, 2nd alarm1-12 + AM/PM or 00-23
    10 hours
    0x0DA2M4DY/DT10thDayDay of the week, 2nd alarm1-7
    NumberDate, 2nd alarm01-31
    0x0EEOSCBBSQWCONVRS2RS1INTCNA2IEA1IESettings register (Control)
    0x0FO.S.F.0 0 0 EN32kHzBSYA2FA1FStatus Register
    0x10SIGNDATADATADATADATADATADATADATAAging Offset Register
    0x11SIGNDATADATADATADATADATADATADATATemperature register, high byte
    0x12DATADATA0 0 0 0 0 0 Temperature register, low byte

    Time information is stored in binary decimal format, that is, each digit of a decimal number (from 0 to 9) is represented as a group of 4 bits. In the case of one byte, the low nibble counts ones, the high nibble counts tens, etc. Time is counted in registers with addresses 0x00-0x06; for counting hours, you can select the 12 or 24 hour mode. Setting the 6th bit of the clock register (address 0x02) sets the 12-hour mode, in which the 5th bit indicates the time of day, value 1 corresponds to afternoon (PM), value 0 corresponds to afternoon (AM). The zero value of the 6th bit corresponds to the 24-hour mode, here the 5th bit is involved in counting the hours (values ​​20-23).

    The day of the week register is incremented at midnight, counting from 1 to 7, the month register (address 0x05) contains the Century bit (7th bit), which switches when the years counting register (address 0x06) overflows, from 99 to 00.

    The DS3231 chip implements two alarm clocks, the 1st alarm clock is configured using registers with addresses 0x07-0x0A, the 2nd alarm clock is configured using registers 0x0B-0x0D. The A1Mx and A2Mx bits can be used to configure various modes for alarms; setting the bit excludes the corresponding register from the comparison operation. The tables below show the bit combinations for different alarm modes:

    Bit combinations not specified in the tables lead to incorrect operation of alarms. If the DY/DT bit is cleared, then the date match (day of the month) is monitored for the alarm clock; when the DY/DT bit is set, the match of the day of the week is checked.

    Most functions are configured in the Control register. The EOSC bit controls the start of the clock generator, resetting the bit starts the clock generator. Setting the bit stops the generator, for battery mode (Vbat) only. When powered from an external source (Vcc), the oscillator is always running regardless of the state of the EOSC bit. When enabled, the default bit value is 0.

    Setting the BBSQW bit allows the INT/SQW output (3rd pin) to operate in battery power mode, in the absence of external power. When the bit is set to zero, the INT/SQW output goes into state 3 (deactivated) if the external source voltage Vcc falls below the threshold value Vpf. After power is applied, the default bit value is 0.

    The CONV bit is responsible for forced temperature measurement; setting the bit starts the conversion process, during which the clock generator frequency is also adjusted; the measurement result is located in registers with addresses 0x11, 0x12. Starting is possible only if the previous conversion has completed; before starting, you need to check the busy flag BSY. Forced temperature conversion does not affect the internal 64 second frequency adjustment cycle. Setting the CONV bit does not affect the BSY flag for 2 ms. The CONV and BSY bits are cleared automatically after conversion is complete.

    Bits RS2, RS1 set the frequency of rectangular pulses (square wave) at the INT/SQW output. By default, when enabled, the bits are set to 1. The table below shows the possible combinations of bits:

    The INTCN bit controls the INT/SQW output. If the bit is reset, rectangular pulses (square waves) appear at the output, the frequency of which is set by the RS2, RS1 bits. When the INTCN bit is set, the output is used to generate alarm interrupts. By default, the bit value is 1. The output type is INT/SQW - open drain, therefore it is necessary to pull it up through a resistor to a high logic level, the active level is low.

    Setting bits A1IE, A2IE enables interruptions on the 1st and 2nd alarm signals, respectively. Reset bits, disables interrupts. The default value is 0.

    The Status register contains event flags and controls the 32 kHz output. The OSF flag reflects the state of the clock generator, a value of 1 means that the clock generator is stopped, this event can occur in the following cases:

    • For the first time after power is applied
    • Battery or external voltage is insufficient to operate the clock generator
    • The generator is turned off by setting the EOSC bit in battery mode
    • External factors affecting the crystal oscillator (noise, leakage, etc.)

    Once set, the bit value does not change; the bit must be reset manually.

    Setting the EN32kHz bit allows the generation of rectangular pulses (square waves) at the 32kHz output (1st pin), the pulse frequency is fixed and equal to 32.768 kHz. Resetting the bit disables this function and moves the output to the 3rd state (high input impedance). By default, the bit value is 1; after power is applied, pulses appear at the output. The output type is 32kHz open drain, so it requires a pull-up to a high logic level.

    The BSY busy flag is set during the temperature conversion and clock adjustment process. The flag is reset when the conversion is complete.

    Alarm clock flags A1F, A2F are set when the values ​​of the time counting registers and the alarm clock registers match. If alarm interrupts A1IE, A2IE are enabled, and an interrupt output is assigned (INTCN bit is set), then an interrupt signal appears at the INT/SQW output (transition from high to low logic level). The flags must be reset manually by writing the value 0.

    The Aging Offset register is designed to adjust the clock generator frequency. The register value is added to the oscillator frequency during the internal adjustment procedure, if a temperature change is detected, and also when temperature conversion is triggered by the CONV bit. The offset value is signed, that is, positive values ​​(1-127) reduce the frequency, negative values ​​(128-255) increase it. For the same offset, the frequency change will be different depending on the temperature. At +25°C, the frequency change will be 0.1 ppm/LSB.

    The current temperature value is stored in registers with addresses 0x11 and 0x12, the high and low byte, respectively, the temperature value in the registers is periodically updated. Left alignment is set, the resolution is 10 bits or 0.25°C/LSB, that is, the high byte contains the integer part of the temperature, and the 6th, 7th bits in the low registers make up the fractional part. In the high byte, the 7th bit indicates the sign of the temperature, for example, the value 00011011 01 corresponds to a temperature of +27.25 °C, the value 11111110 10 corresponds to a temperature of -2.5 °C.

    When reading time registers, it is recommended to use an additional buffer, that is, read several registers at once, and not separately, since between individual reading operations, the time registers can change their value. This rule is also recommended to be followed when writing new data to account registers. Writing a new value to the seconds register pauses the clock for 1 second, the remaining registers must be rewritten during this time.

    Connecting DS3231 to a microcontroller

    I connected the clock to a PIC16F628A microcontroller and used . The connection diagram is shown below:


    After power is applied, dashes (– – – – – –) are displayed on the indicators, then the clock is initialized, the time value appears on the indicators with a delay of 1 second, which is required to start the clock generator. The indicators display the hours, minutes and seconds, separated by a decimal point, and the time format is 24-hour. Using the SB1 “Indication” button, you can change the display format, where the indicators will display the temperature, as well as the value of hours and minutes, separated by a decimal point, which blinks at a frequency of 2 Hz. The temperature is displayed without a fractional part; the program reads only the high byte of temperature storage at address 0x11.

    The time value is read from the clock via an interrupt on the SQW/INT line, which is controlled by the 1st alarm signal; during the clock initialization, the alarm clock is set to an every second signal. The HL1 LED serves as an indicator and flashes at the interrupt signal every second. The HL2 LED lights up if there is an error in data transmission via the I2C interface.

    Additionally, I added to the program the ability to set the clock using the SB2 “Settings”, SB3 “Installation” buttons. The setup mode is entered by pressing the SB2 button; the indicators display 00 hours and dashes instead of minutes and seconds (00 – – – –). Using the SB3 button, you set the hour value (increment with each press), then pressing the SB2 button switches to editing the minutes; instead of a dash, 00 minutes will be displayed. Button SB3 also sets the required value and so on. After editing the seconds and pressing the SB2 button, the time in the clock is rewritten, and the updated time is displayed on the indicators.

    Partial program code is given below (the full version can be downloaded at the end of the article):

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; #include LIST p=16F628A __CONFIG H"3F10" ;Microcontroller configuration errorlevel -302 ;do not display messages with error 302 in listing Sec equ 0020h ;auxiliary account registers Sec1 equ 0021h ; Sec2 equ 0022h ; scetbit equ 0024h;auxiliary register for counting the number of bits perem equ 0025h;auxiliary register for byte reception/transmission via spi, i2c temp equ 0026h;temperature register perem_1 equ 0027h;auxiliary register for binary-decimal converter. result equ 0028h ;binary-decimal converter auxiliary register dat_ind equ 0029h ;data register for transmission via the spi protocol adr_ind equ 002Ah ;address register for transmission via the spi protocol second equ 002Bh ;seconds storage register for setting the time minut equ 002Ch ;minutes storage register for setting the time hour equ 002Dh ;hour storage register for time settings adr_i2c equ 002Eh ;registers of the i2c interface data transfer subroutine tmp_i2c equ 002Fh slave_adr equ 0030h data_i2c equ 0031h flag equ 007Fh ;flag register #DEFINE int PORTB,0 ;interrupt line INT/SQW DS3231 #DEFINE sda PORTB,1 ;line SDA for connections DS3231 #DEFINE scl PORTB,2 ;SCL line for connecting DS3231 #DEFINE sda_io TRISB,1 ;direction of the SDA line #DEFINE scl_io TRISB,2 ;direction of the SCL line #DEFINE datai PORTB,5 ;data input line of the MAX7219 driver #DEFINE cs PORTB ,6 ;driver selection line MAX7219 #DEFINE clk PORTB,7 ;driver clock line MAX7219 #DEFINE led PORTB,4 ;i2c error LED #DEFINE led_sec PORTB,3 ;clock progress indicator LED 1Hz #DEFINE regim PORTA,2 ;Indication button - changing the display mode #DEFINE nast PORTA,3 ;Setting button - entering the time setting mode #DEFINE ust PORTA,4 ;Setting button - setting the clock value;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; org 0000h ;start program execution from address 0000h goto Start ;go to the Start label ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Main program Start movlw b"00000000" ;setting the values ​​of the output latches of port A movwf PORTA ; movlw b"01000000" ;set the values ​​of the output latches of port B movwf PORTB ; movlw b"00000111" ;turn off comparators movwf CMCON ; bsf STATUS,RP0 ;select the 1st bank movlw b"00000111" ;set up the input/output lines of port B movwf TRISB ;RB0-RB2 - for input, the rest for output movlw b"11111111" ;set up the input/output lines of port A movwf TRISA ;all lines to input bcf STATUS,RP0 ;select bank 0 clrf flag ;reset flag register call init_lcd ;call driver initialization subroutine (MAX7219) call viv_not ;output dash symbols " ------ " ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;; movlw b"11010000" ;device address (DS3231) movwf slave_adr ;Write 4 bytes to the receive/transmit registers via i2c;here the 1st alarm is configured, beeping every second movlw data_i2c ;set the first receive/transmit register via i2c movwf FSR ; movlw b"10000000" ;data for the seconds register of the 1st alarm movwf INDF ; incf FSR,F ; movlw b"10000000" ;data for the minutes register of the 1st alarm movwf INDF ; incf FSR,F ; movlw b"10000000" ;data for the clock register of the 1st alarm clock movwf INDF ; incf FSR,F ; movlw b"10000000" ;data for the date/day of week register of the 1st alarm movwf INDF ; movlw. 4 ;transfer 4 bytes via i2c movwf tmp_i2c ; movlw 0x07 ;setting the address of the seconds register of the 1st alarm clock movwf adr_i2c ; call write_i2c ;calling the writing subroutine via the i2c interface call err_prov ;checking for I2C write/read errors movlw .1 ;transferring the 1st byte via i2c movwf tmp_i2c ; movlw 0x0E ;setting the address of the Control register movwf adr_i2c ; movlw data_i2c ;setting the first transmit/receive register via i2c movwf FSR ; movlw b"00000101" ;start the clock generator, prohibit the operation of the INT/SQW pin for movwf INDF ;battery power mode, pulse frequency at the INT/SQW output is 1Hz, ;the INT/SQW output is used to generate alarm clock interrupts, ;enable alarm clock interrupts 1st alarm call write_i2c ;calling the recording subroutine via the i2c interface call err_prov ;checking for I2C write/read errors met_2 movlw .1 ;transferring the 1st byte via i2c movwf tmp_i2c ; movlw 0x0F ;setting the address of the Status register movwf adr_i2c ; movlw data_i2c ;setting the first transmit/receive register via i2c movwf FSR ; movlw b"00000000" ;reset the OSF bit, prohibit the generation of pulses at the EN32kHz output, movwf INDF ;reset the alarm interrupt flags A2F, A1F call write_i2c ;call the recording subroutine via the i2c interface call err_prov ;check for I2C write/read errors met_1 btfsc int ; polling the alarm interrupt line goto met_3; bsf led_sec ;turn on the clock progress indicator LED goto met_4 ; met_3 bcf led_sec ;turns off the clock progress indicator LED btfsc nast ;polls the clock setting button goto met_5 ; call nast_time ;call the subroutine for setting the time goto met_2 ; met_5 btfsc regim ;poll of the indication mode button goto met_1 ; met_6 call paus_knp ; btfss regim ; goto met_6 ; btfss flag,2 ;change the value of the indication mode flag goto met_7 ; bcf flag,2 ;reset indication flag, clock display mode goto met_1 ; met_7 bsf flag,2 ;setting the indication flag, temperature and clock display mode goto met_1 ; met_4 movlw .1 ;transmitting the 1st byte via i2c movwf tmp_i2c ; movlw 0x11 ;setting the address of the high temperature register movwf adr_i2c ; call read_i2c ;calling the reading subroutine via I2C call err_prov ;checking for I2C write/read errors movf INDF,W ;copying the temperature value into the temp register movwf temp rd_time movlw .3 ;transferring 3 bytes via i2c movwf tmp_i2c ; movlw 0x00 ;setting the seconds register address movwf adr_i2c ; call read_i2c ;calling the reading subroutine via I2C call err_prov ;checking for I2C write/read errors btfsc flag,2 ;poll of the indication mode flag goto met_8 ; call vivod ;calling the subroutine for displaying the clock value on the digital display goto met_2 ; met_8 call vivod_temp ;calling the subroutine for displaying the temperature and clock on the digital display goto met_2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    #include

    CONFIG H"3F10" ;Microcontroller configuration

    errorlevel -302 ;do not display 302 error messages in the listing

    Sec equ 0020h ;auxiliary account registers

    Sec1 equ 0021h ;

    Sec2 equ 0022h ;

    scetbit equ 0024h ;counting auxiliary register number of bits

    perem equ 0025h ;auxiliary byte reception/transmission register via spi, i2c

    temp equ 0026h ;temperature register

    perem_1 equ 0027h ;BCD auxiliary register

    result equ 0028h ;binary-decimal converter auxiliary register

    dat_ind equ 0029h ;data register for transmission via spi protocol

    adr_ind equ 002Ah ;address register for transmission via spi protocol

    second equ 002Bh ;seconds storage register for setting the time

    minut equ 002Ch ;minute storage register for setting the time

    hour equ 002Dh ;hour storage register for setting the time

    adr_i2c equ 002Eh ;registers of the i2c interface data transfer subroutine

    tmp_i2c equ 002Fh

    slave_adr equ 0030h

    data_i2c equ 0031h

    flag equ 007Fh ;flag register

    #DEFINE int PORTB,0 ;interrupt line INT/SQW DS3231

    #DEFINE sda PORTB,1 ;SDA line for connecting DS3231

    #DEFINE scl PORTB,2 ;SCL line for connecting DS3231

    #DEFINE datai PORTB,5 ;data input line of MAX7219 driver

    #DEFINE cs PORTB,6 ;driver selection line MAX7219

    #DEFINE clk PORTB,7 ;clock line of MAX7219 driver

    #DEFINE led PORTB,4 ;i2c error LED

    #DEFINE led_sec PORTB,3 ;LED clock progress indicator 1Hz

    #DEFINE regim PORTA,2 ;Indication button - changes the display mode

    #DEFINE nast PORTA,3 ;Settings button - enters time setting mode

    #DEFINE ust PORTA,4 ;Set button - set the clock value

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    org 0000h ;start program execution from address 0000h

    goto Start ;go to the Start label

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;Main program

    Start movlw b"00000000" ;set the values ​​of the output latches of port A

    movlw b"01000000" ;set the values ​​of the output latches of port B

    movlw b"00000111" ;turn off comparators

    bsf STATUS,RP0 ;select 1st bank

    movlw b"00000111" ;configuring the input/output lines of port B

    movwf TRISB ;RB0-RB2 - to the input, the rest to the output

    movlw b"11111111" ;setting up the input/output lines of port A

    movwf TRISA ;all lines to input

    bcf STATUS,RP0 ;select bank 0

    clrf flag ;reset flag register

    call init_lcd ;call the driver initialization routine (MAX7219)

    call viv_not ;output dash symbols " ------ " to indicators

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    movlw b"11010000" ;device address (DS3231)

    ;Write 4 bytes to the receive/transmit registers via i2c

    movlw data_i2c ;setting the first receive/transmit register via i2c

    movlw b"10000000" ;data for the seconds register of the 1st alarm

    movlw b"10000000" ;data for the minutes register of the 1st alarm

    movlw b"10000000" ;data for the 1st alarm clock register

    movlw b"10000000" ;data for the date/day of week register of the 1st alarm

    movlw .4 ;transfer 4 bytes via i2c

    movlw 0x07 ;setting the address of the seconds register of the 1st alarm clock

    In many Arduino projects, it is required to monitor and record the time of occurrence of certain events. The real-time clock module, equipped with an additional battery, allows you to store the current date without depending on the availability of power on the device itself. In this article we will talk about the most common RTC modules DS1307, DS1302, DS3231 that can be used with Arduino board.

    The clock module is a small board containing, as a rule, one of the DS1307, DS1302, DS3231 microcircuits. In addition, on the board you can practically find a battery installation mechanism. Such boards are often used to track time, date, day of the week and other chronometric parameters. The modules operate on autonomous power - batteries, accumulators, and continue to count even if the power to the Arduino is turned off. The most common watch models are DS1302, DS1307, DS3231. They are based on an RTC (real time clock) module connected to Arduino.

    Clocks count in units that are convenient for the average person - minutes, hours, days of the week and others, in contrast to conventional counters and clock generators that read “ticks”. Arduino has a special millis() function that can also read different time intervals. But the main disadvantage of this function is that it resets to zero when the timer is turned on. With its help you can only read time; it is impossible to set the date or day of the week. Real-time clock modules are used to solve this problem.

    The electronic circuit includes a microcircuit, a power supply, a quartz resonator and resistors. The quartz resonator operates at a frequency of 32768 Hz, which is convenient for a conventional binary counter. The DS3231 circuit has built-in quartz and thermal stabilization, which allows for highly accurate values.

    Comparison of popular RTC modules DS1302, DS1307, DS3231

    In this table we have provided a list of the most popular modules and their main characteristics.

    Name Frequency Accuracy Supported protocols
    DS1307 1 Hz, 4.096 kHz, 8.192 kHz, 32.768 kHz Depends on the quartz - usually the value reaches 2.5 seconds per day, it is impossible to achieve accuracy higher than 1 second per day. Also, accuracy depends on temperature. I2C
    DS1302 32.768 kHz 5 seconds a day I2C, SPI
    DS3231 Two outputs - the first at 32.768 kHz, the second programmable from 1 Hz to 8.192 kHz ±2 ppm at temperatures from 0C to 40C.

    ±3.5 ppm at temperatures from -40C to 85C.

    Temperature measurement accuracy – ±3С

    I2C

    Module DS1307

    DS1307 is a module that is used for timing. It is assembled on the basis of the DS1307ZN chip, power is supplied from a lithium battery to ensure autonomous operation for a long period of time. The battery on the board is mounted on the reverse side. The module has an AT24C32 chip - this is a 32 KB non-volatile EEPROM memory. Both microcircuits are connected by an I2C bus. DS1307 has low power consumption and contains a clock and calendar for the year 2100.

    The module has the following parameters:

    • Power supply – 5V;
    • Operating temperature range from -40C to 85C;
    • 56 bytes of memory;
    • Lithium battery LIR2032;
    • Implements 12 and 24 hour modes;
    • I2C interface support.

    The module is justified in cases where data is read quite rarely, at intervals of a week or more. This allows you to save on power, since uninterrupted use will require more voltage, even with a battery. The presence of memory allows you to register various parameters (for example, temperature measurement) and read the received information from the module.

    Interaction with other devices and exchange of information with them is carried out using the I2C interface from the SCL and SDA pins. The circuit contains resistors that allow you to provide the required signal level. The board also has a special place for mounting the DS18B20 temperature sensor. The contacts are distributed into 2 groups, pitch 2.54 mm. The first group of contacts contains the following pins:

    • DS – output for sensor DS18B20;
    • SCL – clock line;
    • SDA – data line;
    • VCC – 5V;

    The second group of contacts contains:

    • SQ – 1 MHz;
    • BAT – input for lithium battery.

    To connect to the Arduino board, you need the board itself (in this case we are considering Arduino Uno), an RTC DS1307 real-time clock module, wires and a USB cable.

    To connect the controller to Arduino, 4 pins are used - VCC, ground, SCL, SDA.. VCC from the clock is connected to 5V on the Arduino, ground from the clock is connected to ground from the Arduino, SDA - A4, SCL - A5.

    To start working with the clock module, you need to install the DS1307RTC, TimeLib and Wire libraries. You can also use RTCLib for work.

    Checking the RTC module

    When you run the first code, the program will read data from the module once per second. First, you can see how the program behaves if you remove the battery from the module and replace it with another while the Arduino board is not connected to the computer. You need to wait a few seconds and remove the battery, eventually the watch will reboot. Then you need to select an example from the Examples→RTClib→ds1307 menu. It is important to set the transmission speed correctly to 57600 bps.

    When you open the Serial Monitor window, the following lines should appear:

    The time will show 0:0:0. This is because the watch loses power and will stop counting time. For this reason, the battery must not be removed while the module is operating.

    To set the time on the module, you need to find the line in the sketch

    RTC.adjust(DateTime(__DATE__, __TIME__));

    This line will contain data from the computer that is used to flash the real-time clock module. For correct operation, you must first check that the date and time on the computer are correct, and only then start flashing the clock module. After setup, the monitor will display the following data:

    The setup is done correctly and there is no need to additionally reconfigure the real-time clock.

    Time reading. Once the module is configured, time requests can be sent. This is done using the now() function, which returns a DateTime object that contains time and date information. There are a number of libraries that are used to read time. For example, RTC.year() and RTC.hour() - they separately obtain information about the year and hour. When working with them, a problem may arise: for example, a request to display the time will be made at 1:19:59. Before showing the time 1:20:00, the clock will display the time 1:19:00, meaning, in essence, one minute will be lost. Therefore, it is advisable to use these libraries in cases where reading occurs infrequently - once every few days. There are other functions for calling time, but if you need to reduce or avoid errors, it is better to use now() and extract the necessary readings from it.

    Example project with i2C clock module and display

    The project is a regular clock; the exact time will be displayed on the indicator, and the colon between the numbers will blink at intervals of once every second. To implement the project, you will need an Arduino Uno board, a digital indicator, a real-time clock (in this case, the above-described ds1307 module), a shield for connection (in this case, Troyka Shield is used), a battery for the clock and wires.

    The project uses a simple four-digit indicator on the TM1637 chip. The device has a two-wire interface and provides 8 levels of monitor brightness. Used only to display time in hours:minutes format. The indicator is easy to use and easy to connect. It is beneficial to use for projects where minute or hourly data verification is not required. To obtain more complete information about time and date, LCD monitors are used.

    The clock module is connected to the SCL/SDA pins, which belong to the I2C bus. You also need to connect ground and power. It is connected to Arduino in the same way as described above: SDA – A4, SCL – A5, ground from the module to ground from Arduino, VCC -5V.

    The indicator is connected simply - its CLK and DIO pins are connected to any digital pins on the board.

    Sketch. To write code, use the setup function, which allows you to initialize the clock and indicator and record the compilation time. Printing the time to the screen will be done using loop.

    #include #include "TM1637.h" #include "DS1307.h" //you need to include all the necessary libraries for working with the clock and display. char compileTime = __TIME__; //compilation time. #define DISPLAY_CLK_PIN 10 #define DISPLAY_DIO_PIN 11 //numbers from the Arduino outputs to which the screen is connected; void setup() ( display.set(); display.init(); //connect and configure the screen. clock.begin(); //turn on the clock. byte hour = getInt(compileTime, 0); byte minute = getInt( compileTime, 2); byte second = getInt(compileTime, 4); //getting time. clock.fillByHMS(hour, minute, second); //preparing for writing to the time module. clock.setTime(); //recording in progress received information into internal memory, start reading time. ) void loop() ( int8_t timeDisp; //display on each of the four bits. clock.getTime();//request to get time. timeDisp = clock.hour / 10; timeDisp = clock.hour % 10; timeDisp = clock.minute / 10; timeDisp = clock.minute % 10; //various operations to get tens, units of hours, minutes and so on. display.display(timeDisp); //display time to the indicator display.point(clock.second % 2 ? POINT_ON: POINT_OFF);//turn on and off the colon after a second. ) char getInt(const char* string, int startIndex) ( return int(string - "0") * 10 + int(string) - "0"; //actions to correctly write time into a two-digit integer. Otherwise, just a couple of symbols will be displayed on the screen. )

    After this, the sketch needs to be loaded and the time will be shown on the monitor.

    The program could be slightly modernized. When the power is turned off, the sketch written above will cause the display to show the time that was set during compilation after turning it on. In the setup function, each time the time that has passed from 00:00:00 to the start of compilation will be calculated. This hash will be compared to what is stored in the EEPROM, which is retained when power is removed.

    To write and read time to or from non-volatile memory, you need to add the EEPROMWriteInt and EEPROMReadInt functions. They are needed to check whether the hash matches/mismatches the hash recorded in the EEPROM.

    The project can be improved. If you use an LCD monitor, you can make a project that displays the date and time on the screen. The connection of all elements is shown in the figure.

    As a result, the code will need to specify a new library (for liquid crystal screens this is LiquidCrystal), and add lines to the loop() function to obtain the date.

    The operating algorithm is as follows:

    • Connecting all components;
    • Check - the time and date on the monitor screen should change every second. If the time on the screen is incorrect, you need to add the RTC.write (tmElements_t tm) function to the sketch. Problems with incorrect times are due to the fact that the clock module resets the date and time to 00:00:00 01/01/2000 when turned off.
    • The write function allows you to get the date and time from the computer, after which the correct parameters will be indicated on the screen.

    Conclusion

    Clock modules are used in many projects. They are needed for data recording systems, when creating timers and control devices that operate according to a given schedule, in household appliances. With widely available and cheap modules, you can create projects such as an alarm clock or a sensor data logger, recording information to an SD card or showing the time on a display screen. In this article, we looked at typical usage scenarios and connection options for the most popular types of modules.