Example Code for Arduino-Countdown

Last revision 2025/12/08

Run this routine to implement the countdown function.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0998 Wiring diagram

Sensor Side Pin Name MCU Side Pin Name
Fermion: SD3031 RTC Module VCC UNO R3 3.3V
Fermion: SD3031 RTC Module GND UNO R3 GND
Fermion: SD3031 RTC Module SDA UNO R3 SDA
Fermion: SD3031 RTC Module SCL UNO R3 SCL
Fermion: SD3031 RTC Module INT UNO R3 D2

Sample Code

/*!
 * @file countDown.ino
 * @brief Run this routine to implement the countdown function
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license      The MIT License (MIT)
 * @author       [TangJie]([email protected])
 * @version      V1.0.0
 * @date         2022-07-27
 * @url         https://github.com/DFRobot/DFRobot_SD3031
 */
#include "DFRobot_SD3031.h"

DFRobot_SD3031 rtc;
volatile  int8_t alarmFlag = 0;
void setup()
{
    Serial.begin(115200);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != 0){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    #if defined(ESP32)||defined(ARDUINO_SAM_ZERO)
      attachInterrupt(digitalPinToInterrupt(D7)/*Query the interrupt number of the D6 pin*/,interrupt,FALLING);
    #elif defined(ESP8266)
      attachInterrupt(digitalPinToInterrupt(D5)/*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(digitalPinToInterrupt(2), interrupt, CHANGE);
    #endif
    rtc.countDown(10);
    Serial.println("start");
}

void loop()
{
    if(alarmFlag == 1){
        rtc.countDown(10);
        alarmFlag = 0;
        Serial.println("Alarm clock is triggered.");   
    } 
}
void interrupt(void)
{
    alarmFlag = 1;
}

Result

The serial port outputs time information.

DFR0998 Countdown Result

Was this article helpful?

TOP