Introduction

This real-time clock module (RTC) uses the DS3232 chip that integrates a crystal resonator and a temperature compensator, which improves the long-term accuracy of the module. In the range of -40°C to +85°C, the accuracy of DS3231 remains at ±3.5ppm (±0.3024 seconds/day).
The DS3232 integrates 236Byte SRAM storage that can store data with extremely low power consumption, and it also can be used in scenarios such as low power consumption collection of the Internet of Things.
The device incorporates a battery input, and maintains high precision time-keeping when main power to the device is interrupted. The RTC provides information of seconds, minutes, hours, week, day, month and year, and it can automatically adjust the date at the end of the month, including corrections for leap year.The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. In addition, the RST pin can be used as a push-button input for microprocessor reset.

Features

Applications

Specification

Board Overview

Num Label Description
1 VCC Power +
2 GND Power-
3 SCL I2C Data line
4 SDA I2C Clock line
5 INT Interrupt in low level or 1Hz square wave output
6 RST Reset calibration pin
7 32K 32.768KHz Pulse Output

Note: INT Pin is the sqw pin.

Tutorial

Requirements

  /*!
   *@brief get year of now time
   *@return year
   */
  uint16_t getYear();

  /*!
   *@brief get month of now time
   *@return month
   */
  uint8_t getMonth();

  /*!
   *@brief get date of now time
   *@return date
   */
  uint8_t getDate();

  /*!
   *@brief get hour of now time
   *@return hour
   */
  uint8_t getHour();

  /*!
   *@brief get minute of now time
   *@return minute
   */
  uint8_t getMinute();

  /*!
   *@brief get second of now time
   *@return second
   */
  uint8_t getSecond();

  /*!
   *@brief get day of week
   *@return day of week
   */
  String getDayOfWeek();

  /*!
   *@brief Set hour system of time
   *@param eHours_t:e24hours, e12hours. default is e24hours
   */
  void setHourSystem(eHours_t mode = e24hours);

  /*!
   *@brief Set time into rtc and take effect immediately
   *@param year, 1900~2100
   *@param month, 1~12
   *@param date, 1~31
   *@param hour, 0~23
   *@param minute, 0~59
   *@param second, 0~59
   */
  void setTime(uint16_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute, uint8_t second);

  /*!
   *@brief Set the last compiled time as the current time
   *@param comSec, Compensation time, the value obtained by subtracting the PC system time from the serial port printing time after 
   */the first upload,unit: s.
  void getCompileTime(uint8_t comSec);


  /*!
   *@brief output AM or PM of time 
   *@return AM or PM, 24hours return empty string 
   */
  String getAMorPM();

  /*!
   *@brief Get current temperature 
   *@return Current temperautre, unit: ℃ 
   */
  float getTemperatureC();

  /*!
   *@brief Judge if it is power-down 
   *@return True means rtc lost power before and need to reset time;False means rtc operates well
   */
  bool isLostPower(void);

Connection Diagram

Sample Code 1 - Read and Set time

Set the time to the specified time, the serial port will print time and temperature data once per second.
Note: The internal temperature sensor is used to compensate the clock, the accuracy is not high. If there is a high requirements of temperature accuracy, please choose other temperature sensors.

/*!
 * @file getTimeAndTemperature.ino
 * @brief Show current time and temperature of chip
 * @n Experiment phenomenon: Set original time by users themselves and get real time and temperature from the chip
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"

DFRobot_DS323X rtc;


void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    /*!
     *@brief Set time output format
     *@param eHours_t:e24hours, e12hours. default is e24hours
     */
    rtc.setHourSystem(rtc.e12hours);
    rtc.setTime(/*year,1900-2100*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .

}

void loop() {
    Serial.print(rtc.getYear(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);//second
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    Serial.print("Temperature: ");
    /*!
     *@brief Get current temperature
     *@return Current temperautre, unit: ℃ 
     */
    Serial.print(rtc.getTemperatureC());
    Serial.println(" C");
    delay(1000);

    /*!
     *@brief Judge if it is power-down
     *@return If retrun true, power down, needs to reset time; false, work well.
     */
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
}

Expected Result

Result 1

Sample Code 2 - Get NTP Time

This example is to obtain the current time from the NTP server and prints it every second.
Note: This example needs to be connected to the Internet, so the ESP32 series main control board is required.
If there is no compilation environment, please configure ESP32 compilation environment.
After configuring the ESP32 compilation environment, you need to select the FireBeetle-ESP32 development board and port.

/*!
 * @file getTimefromNTP.ino
 * @brief Get time from ntpServer and show current time ,only work on ESP32
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
#include <WiFi.h>
#include "time.h"

DFRobot_DS323X rtc;

const char* ssid       = "WIFI_ID";//wlan information
const char* password   = "WIFI_PASSWORD";

const char* ntpServer = "ntp.ntsc.ac.cn";//local ntp server
const long  gmtOffset_sec = 8*3600;
const int   daylightOffset_sec = 0;


void getTimeFromNTP()
{
    struct tm timeinfo;
    if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
    }
    rtc.setTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);//Set Set initial time .

}

void setup()
{
    Serial.begin(9600);
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    //connect to WiFi
    Serial.printf("Connecting to %s ", ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println(" CONNECTED");

    //init and get the time
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    getTimeFromNTP();
    //disconnect WiFi as it's no longer needed
    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
}

void loop()
{
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.println(' ');
    delay(1000);
}

Expected Result

Result 2

Sample Code 3 - Alarm Trigger Query

This example uses the rtc.isAlarmTrig(); function in the loop function to enquire which alarm clock is triggered.

/*!
 * @file setAlarmInQuery.ino
 * @brief Set alarm clock and trigger it in query
 * @n Experiment phenomenon: set the alarm clock to trigger at a sepcified time
 * @n           Trigger when the set time of clock is the same with the RTC time
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"

DFRobot_DS323X rtc;

void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is fine");
        delay(1000);
    }
    //rtc.setHourSystem();//Set time mode, default in the 24 hours mode, e24hours, eAM, ePM.
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,                 //repeat in every second
     *@n                                  eSecondsMatch,                //repeat in every minute
     *@n                                  eSecondsMinutesMatch,         //repeat in every hour
     *@n                                  eSecondsMinutesHoursMatch,    //repeat in every day
     *@n                                  eSecondsMinutesHoursDateMatch,//repeat in every month
     *@n                                  eSecondsMinutesHoursDayMatch, //repeat in every week  //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/10);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,                 //repeat in every minute
     *@n                                  eMinutesMatch,                //repeat in every hour
     *@n                                  eMinutesHoursMatch,           //repeat in every day
     *@n                                  eMinutesHoursDateMatch,       //repeat in every month
     *@n                                  eMinutesHoursDayMatch,        //repeat in every week  //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    rtc.setAlarm2(rtc.eEveryMinute,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0);//Alarm2
    if (rtc.isLostPower())
        rtc.setTime(/*year,1901-2099*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,\
                    /*second,0-59*/55);//Set Set initial time .
}
void loop() {
    /*!
     *@brief Judge if the alarm clock is triggered
     *@return eNoTrigger          // No alarm is triggered
     *@n      eAlarm1Trigger      // Alarm1 is triggered
     *@n      eAlarm2Trigger      // Alarm2 is triggered
     *@n      eAllTrigger         // All alarms are triggered
     */
    if (rtc.isAlarmTrig() == rtc.eAlarm1Trigger){ // If the alarm bit is set
        Serial.println("Alarm1 clock is triggered.");
        /*!
         *@brief Clear trigger flag
         */
        rtc.clearAlarm();
    }else if (rtc.isAlarmTrig() == rtc.eAlarm2Trigger){
        Serial.println("Alarm2 clock is triggered.");
        rtc.clearAlarm();
    }else if (rtc.isAlarmTrig() == rtc.eAllTrigger){
        Serial.println("Both Alarm clocks are triggered.");
        rtc.clearAlarm();
    }
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    delay(1000);
}

Expected Result

Result 3

Sample Code 4 - Alarm Trigger Interrupt

This example uses the INT pin of the clock module to determine whether the alarm is triggered Note: If the set clock time system and the set alarm time system are different, the alarm will not be triggered. This sample needs to connect the INT pin of the sensor to the corresponding interrupt pin of the main control board (the UNO interrupt pin D2 is selected in the sample)

/*!
 * @file setAlarmInterrupt.ino
 * @brief Set alarm, and use interrput pin to trigger it
 * @n Experiment phenomenon: set the alarm clock to trigger at a specified time 
 * @n                        connect int pin with DIGITALPIN2
 * @n                        print information on serial port after the alarm clock is triggered.
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"

volatile  int8_t alarmFlag = 0;

DFRobot_DS323X rtc;

void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is correct. ");
        delay(1000);
    }
    /*!
     *@brief Set the value of pin sqw
     *@param mode eSquareWave_OFF  = 0x1C // Not output square wave, enter interrupt mode
     *@n          eSquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eSquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eSquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eSquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(rtc.eSquareWave_OFF);

    /*!
     *@brief enable Alarm1 interrupt
     */
    rtc.enableAlarm1Int();

    /*!
     *@brief disable Alarm1 interrupt
     */
    //rtc.disableAlarm1Int();

    /*!
     *@brief enable Alarm2 interrupt
     */
    rtc.enableAlarm2Int();

    /*!
     *@brief disable Alarm2 interrupt
     */
    //rtc.disableAlarm2Int();

    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,                 //repeat in every second
     *@n                                  eSecondsMatch,                //repeat in every minute
     *@n                                  eSecondsMinutesMatch,         //repeat in every hour
     *@n                                  eSecondsMinutesHoursMatch,    //repeat in every day
     *@n                                  eSecondsMinutesHoursDateMatch,//repeat in every month
     *@n                                  eSecondsMinutesHoursDayMatch, //repeat in every week  //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-31*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/10);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,                 //repeat in every minute
     *@n                                  eMinutesMatch,                //repeat in every hour
     *@n                                  eMinutesHoursMatch,           //repeat in every day
     *@n                                  eMinutesHoursDateMatch,       //repeat in every month
     *@n                                  eMinutesHoursDayMatch,        //repeat in every week  //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    rtc.setAlarm2(rtc.eEveryMinute,/*date,0-31*/1,/*hour,0-23*/0,/*minute,0-59*/0);//Alarm2
    /*!
     *@brief Judge if it is power-down 
     *@return if return true, power-down, time needs to reset; false, work well
     */
    if (rtc.isLostPower())
        rtc.setTime(/*year,1900-2099*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .
    #if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
    attachInterrupt(digitalPinToInterrupt(D6)/*Query the interrupt number of the D6 pin*/,interrupt,FALLING);
    #else
    /*    The Correspondence Table of AVR Series Arduino Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------
    * |                                        |  DigitalPin  | 2  | 3  |                   |
    * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  |                   |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
    * |               Mega2560                 |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
    * |    Leonardo, other 32u4-based          |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
    * |--------------------------------------------------------------------------------------
    */
    /*                      The Correspondence Table of micro:bit Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------------------------------------------------------------
    * |             micro:bit                       | DigitalPin |P0-P20 can be used as an external interrupt                                     |
    * |  (When using as an external interrupt,      |---------------------------------------------------------------------------------------------|
    * |no need to set it to input mode with pinMode)|Interrupt No|Interrupt number is a pin digital value, such as P0 interrupt number 0, P1 is 1 |
    * |-------------------------------------------------------------------------------------------------------------------------------------------|
    */
    attachInterrupt(/*Interrupt No*/0,interrupt,FALLING);//Open the external interrupt 0, connect INT1/2 to the digital pin of the main control: 
    //UNO(2), Mega2560(2), Leonardo(3), microbit(P0).
    #endif
}
void loop() {
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    if(alarmFlag == 1){
        alarmFlag = 0;
        Serial.println("Alarm clock is triggered.");
        delay(1000);
        rtc.clearAlarm();
    }
    else
        delay(1000);
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
}

void interrupt(){
  alarmFlag = 1;
}

Expected Result

Result 4

Sample Code 5 - Low Power and Wakeup

In this example, the main control board enters low power consumption after setting the clock and the alarm. When the alarm is triggered, the main control board exits low power consumption, and it enters low power consumption again after printing 10 times. This sample needs to connect the INT pin of the sensor to the corresponding interrupt pin of the main control board (the UNO interrupt pin D2 is selected in the sample).

/*!
 * @file lowPowerAndWakeUp.ino
 * @brief Set alarm, and use interrput pin to trigger MCU wake up
 * @n Experiment phenomenon: Set the alarm clock to trigger at a specified time.
 * @n                        Connect SQW pin with DIGITALPIN2.
 * @n                        Print information on serial port after the alarm clock is triggered.
 * @n                        This demo only works on avr
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
#include <avr/sleep.h>

volatile  int8_t alarmFlag = 0;

DFRobot_DS323X rtc;
int t = 0;

void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is correct. ");
        delay(1000);
    }
    /*!
     *@brief Set the value of pin sqw
     *@param mode eSquareWave_OFF  = 0x1C // Not output square wave, enter interrupt mode
     *@n          eSquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eSquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eSquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eSquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(rtc.eSquareWave_OFF);
    rtc.enableAlarm1Int();
    //rtc.disableAlarm1Int();
    //rtc.enableAlarm2Int();
    rtc.disableAlarm2Int();
    //rtc.setHourSystem();    //set hour system of time, e24hours,e12hours, default is the 24 hours mode.
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,
     *@n                                  eSecondsMatch,
     *@n                                  eSecondsMinutesMatch,
     *@n                                  eSecondsMinutesHoursMatch,
     *@n                                  eSecondsMinutesHoursDateMatch,
     *@n                                  eSecondsMinutesHoursDayMatch, //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/15);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,
     *@n                                  eMinutesMatch,
     *@n                                  eMinutesHoursMatch,
     *@n                                  eMinutesHoursDateMatch,
     *@n                                  eMinutesHoursDayMatch,        //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    //rtc.setAlarm2(rtc.eEveryMinute,/*date,0-30*/1,/*hour,1-12 in 12hours,0-23 in 24hours*/0,/*minute,0-59*/0);//Alarm2

    rtc.setTime(/*year,1900-2100*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .
    #if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
    attachInterrupt(digitalPinToInterrupt(D6)/*Query the interrupt number of the D6 pin*/,interrupt,FALLING);
    #else
    /*    The Correspondence Table of AVR Series Arduino Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------
    * |                                        |  DigitalPin  | 2  | 3  |                   |
    * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  |                   |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
    * |               Mega2560                 |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
    * |    Leonardo, other 32u4-based          |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
    * |--------------------------------------------------------------------------------------
    */
    /*                      The Correspondence Table of micro:bit Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------------------------------------------------------------
    * |             micro:bit                       | DigitalPin |P0-P20 can be used as an external interrupt                                     |
    * |  (When using as an external interrupt,      |---------------------------------------------------------------------------------------------|
    * |no need to set it to input mode with pinMode)|Interrupt No|Interrupt number is a pin digital value, such as P0 interrupt number 0, P1 is 1 |
    * |-------------------------------------------------------------------------------------------------------------------------------------------|
    */
    attachInterrupt(/*Interrupt No*/0,interrupt,FALLING);//Enable the external interrupt 0, connect INT1/2 to the digital pin of the main control: 
    //UNO(2), Mega2560(2), Leonardo(3), microbit(P0).
    #endif

    /*!
     *@brief Set avr sleep mode
     */
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    sleep_cpu();
    sleep_enable();

}
void loop() {
    if(alarmFlag == 1){
        /*!
         *@brief Judge if the alarm clock is triggered
         *@return eNoTrigger          // No alarm is triggered
         *@n      eAlarm1Trigger      // Alarm1 is triggered
         *@n      eAlarm2Trigger      // Alarm2 is triggered
         *@n      eAllTrigger         // All alarms are triggered
         */
        if (rtc.isAlarmTrig() == rtc.eAlarm1Trigger){ // If the alarm bit is set
            Serial.println("Alarm1 clock is triggered.");
            /*!
             *@brief Clear trigger flag
             */
            rtc.clearAlarm();
        }else if (rtc.isAlarmTrig() == rtc.eAlarm2Trigger){
            Serial.println("Alarm2 clock is triggered.");
            rtc.clearAlarm();
        }else if (rtc.isAlarmTrig() == rtc.eAllTrigger){
            Serial.println("Both Alarm clocks are triggered.");
            rtc.clearAlarm();
        }
        alarmFlag = 0;
        while (t < 10){
            Serial.print(rtc.getYear(), DEC);
            Serial.print('/');
            Serial.print(rtc.getMonth(), DEC);
            Serial.print('/');
            Serial.print(rtc.getDate(), DEC);
            Serial.print(" (");
            Serial.print(rtc.getDayOfWeek());
            Serial.print(") ");
            Serial.print(rtc.getHour(), DEC);
            Serial.print(':');
            Serial.print(rtc.getMinute(), DEC);
            Serial.print(':');
            Serial.print(rtc.getSecond(), DEC);
            Serial.print(' ');
            /*if rtc works in 24hours mode,this function doesn't print anything*/
            Serial.print(rtc.getAMorPM());
            Serial.println();
            delay(1000);
            t = t + 1;
        }
        t = 0;
    }
    else
        delay(1000);
    /*!
     *@brief Judge if it is power-down 
     *@return if return true, power-down, time needs to reset; false, work well
     */
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    sleep_enable();
    //energy.PowerDown();
}


void interrupt(){
    alarmFlag = 1;
    sleep_disable();
}

Expected Result

Result 5

Sample Code 6 - Clock Square Wave Output

Enable the 32K pin and let the sqw pin output 1Hz square wave.

/*!
 * @file setSqwAnd32k.ino
 * @brief Set the mode of pin sqw and 32k output
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"

DFRobot_DS323X rtc;


void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    /*!
    *@brief Set the vaule of pin sqw
    *@param mode eSquareWave_OFF  = 0x1C // Not output square wave, enter interrupt mode
    *@n          eSquareWave_1Hz  = 0x00 // 1Hz square wave
    *@n          eSquareWave_1kHz = 0x08 // 1kHz square wave
    *@n          eSquareWave_4kHz = 0x10 // 4kHz square wave
    *@n          eSquareWave_8kHz = 0x18 // 8kHz square wave
    */
    rtc.writeSqwPinMode(rtc.eSquareWave_1kHz);
    /*!
    *@brief Read the value of pin sqw
    *@return eOFF             = 0x1C // Not output square wave, enter interrupt mode
    *@n      eSquareWave_1Hz  = 0x00 // 1Hz square wave
    *@n      eSquareWave_1kHz = 0x08 // 1kHz square wave
    *@n      eSquareWave_4kHz = 0x10 // 4kHz square wave
    *@n      eSquareWave_8kHz = 0x18 // 8kHz square wave
    */
    Serial.println(rtc.readSqwPinMode());
    /*!
     *@brief enable the 32k output 
     */
    rtc.enable32k();
    /*!
     *@brief disable the 32k output 
     */
    //rtc.disable32k();

}

void loop() {
}

Sample Code 7 - Read/Write SRAM

Write data into SRAM and print it out.

/*!
 * @file readWriteSRAM.ino
 * @brief read and write data in SRAM, only work on DS3232 
 * @n Experiment phenomenon: There are 236 bytes of SRAM available for reading and writing 
 * @n                        The address of SRAM is 0x14~0xFF
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"

DFRobot_DS323X rtc;
uint8_t buffer[] = {0x10,0x11,0x24,0x36,0x45,0x65,0x17,0x30,0x78,0xA4,0x89};
int i = 0;

void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    /*!
     *@brief clear the SRAM
     *@param addr 0x14~0xFF
     *@return true means write is successful, false means write is failed
     */
    for (uint8_t addr = 0x14; addr < 0x1F; addr++){
        while (rtc.clearSRAM(addr) != true){}
    }
    /*!
     *@brief write data into the SRAM
     *@param addr 0x14~0xFF
     *@param data uint8_t HEX
     *@return true means write is successful, false means write is failed
     */
    for (uint8_t addr = 0x14; addr < 0x1F; addr++){
        while (rtc.writeSRAM(addr,buffer[i]) != true){}
        i++;
    }
    delay(1000);
}

void loop() {
    /*!
     *@brief read  the SRAM
     *@param addr 0x14~0xFF
     *@return data store in the SRAM
     */
    for (uint8_t addr = 0x14; addr < 0x1F; addr++){
        Serial.print(rtc.readSRAM(addr),HEX);
        Serial.print(" ");
        delay(200);
    }
    delay(1000);
    Serial.println();
}

Expected Result

Result 7

FAQ

Please click the topic link on DFRobot Forum.

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFshopping_car1.png Get Fermion: DS3232 Precise RTC (Breakout) from DFRobot Store or DFRobot Distributor.

Turn to the Top