Arduino library to check the date and temperature of the integrated DS3231 via I2C

Arduino library to check the date and temperature of the integrated DS3231 via I2C

Arduino library to check the date and temperature of the integrated DS3231 via I2C

UPDATED: Also visit the new library to manage date and time with the DS3231 RTC module and Arduino with improvements such as seasonal time.

The operation of IC de real time clocks (RTC) most popular that are controlled using the bus I2C It is usually very similar. Besides, the Wire library de Arduino greatly simplifies communications with devices Two-wire Serial Interface (TWI), I2C, specific.

Table of Contents

    Broadly speaking, the process consists of

    1. Initiate communications as slave or master (set by default) with the function Wire.begin(address). If "address" is omitted, communications begin with the μC the teacher of bus I2C.

    2. Activate communication I2C with the device through the memory address where it is located, using the command Wire.beginTransmission(address).

    3. Write an order in the bus I2C to tell the device the operation you want it to perform, using Wire.write(command), in which "order" is the operation code.

    4. Disable communications to release the bus I2C with function Wire.endTransmission().

    5. Request the device to send a certain amount of data that corresponds to the operation that has been requested (in this case, the date and time) with the function Wire.requestFrom(address,amount).

    6. Wait for the data requested with the function to be available for reading Wire.available(), which returns the number of data that has already been received and can be read.

    7. Read the data sent by the device (the real time clock, in this case) using the function Wire.read() as many times as bytes indicated Wire.available() that are available.

    8. Normally the data is sent in very compact formats so it is very likely that it will be necessary to interpret the data received in a way that corresponds to the representation of the data made in the program that uses the device.

    Regarding the DS3231 (and compatible ones in the same series, such as the DS3232) and the interpretation of the data, according to the specifications of the integrated, for example, the values ​​of the different digits that represent the time are represented in binary coded decimal (BCD) which will be more convenient to express as a decimal value (a byte) to use in Arduino

    On the same line, the temperature is expressed as a byte in two's complement for the integer part and two bits for the step, with a resolution of a quarter of a degree, of the decimal part. These and other aspects of data representation on the clock have been discussed exhaustively in the library code below. DS3231

    To check the temperature with this library, just use the method read_temperature() Object DS3231 instantiated at the beginning. To read the date and time, they are first loaded and then requested in one of the formats (compact, human...) available for different uses, documented in the header document of the code library below.

    The following is example code to show how to use the library. As mentioned above, the temperature is simply read with the function read_temperature() of the class object DS3231 but, to ignore errors in the reading, two constants are used that store, respectively, the maximum and minimum temperature of the device according to the data sheet and which are read with the corresponding functions.

    The temperature reading is carried out in two stages: first the value is loaded, so that different uses of the date or time will be consistent (they will not show a higher value in unfavorable cases) and secondly it is used according to the format that is needed. The example program (which is not very practical, although it explains all the possibilities) shows all the available formats

    • The function date_time_value() which returns a pointer to an array tank (bytes) containing the seven numerical values ​​that represent the date and time on a clock DS3231 converted to decimal (they are in BCD on the device)

    • Using the function weekday_number() A value is obtained that corresponds to the number of the day of the week starting on Sunday. To display it as text, an array is used and one is subtracted to start at index zero, Sunday.

    • To consult the date in a "local" (Spanish) format, use the function human_date(), which returns a pointer to a string in which the date is represented in DD/MM/YYYY format, where DD is the day represented with 2 digits, MM is the month with 2 digits and YYYY is the year with 4 digits.

    • The function human_hour() returns the time in the format hh:mm:ss, with hh being the hour (in 24 format) represented with 2 digits, mm being the minutes with 2 digits and ss being the seconds with 2 digits.

    • To easily use the date and time on log files the function has been programmed compact_date_time(), which delivers the value of the date and time in the format YYMMDDhhmmss with AA being the year represented with the last 2 digits, MM the month with 2 digits, DD the day with 2 digits, hh the hour (in 24 format) with 2 digits, mm the minutes with 2 digits and ss the seconds with 2 digits. This format, even though it is text, takes up little space and allows for very simple alphabetical ordering.

    • The function date_time_MySQL() serves to present the date and time in the format used by the database manager MySQL (or the new and freer MariaDB) YYYY-MM-DD hh:mm:ss, where YYYY is the year represented with 4 digits, MM is the month with 2 digits, DD is the day with 2 digits, hh is the hour (in 24 format) with 2 digits, mm is the minutes with 2 digits and seconds with 2 digits.

    Although there are many formats with which to represent the date and time, the one you need may not be there, but surely based on one of the existing ones and using it as an example, it will be easy to add a new method according to other specifications. Please, if you add new functions, share the code (release it!) and explain to us how it works, so we can make the library better little by little. Thank you!

    The output of the above example program could be something like what is shown in the following image: a list of 7 values ​​(seconds, minutes, hour, day of the week, day of the month, month and year) the date and the time expressed in a "human" way (according to the Spanish style) the time as a whole number in the four-digit clock format, the date and time in database format MySQL, date and time in compact format (for logs) and the internal temperature of the DS3231.

    Arduino console output library real time clock (RTC) and temperature DS3231 via I2C bus

    Post Comment

    You May Have Missed