Introduction
This precise RTC adopts DS3231M chip, which integrates MEMS resonator and temperature compensator. Compared to quartz crystal clock, the real time clock reduces the risk of mechanical failure. Therefore, it can be used in highly vibrating environment.
Within -45°C to +85°C, the accuracy of the DS3231M is ±5ppm (±0.432 s/D). The RTC based on MEMS has already passed both shock tests and vibration tests that certified by ACE-Q100. It can withstand 2900g mechanical shock (JESD22-B104C Condition-H) and 20g variable frequency vibration (JESD22-B103B Condition-1)
The DS3231M can be powered by a CR1220 coin cell battery and can still maintain accurate timing even the mains are disconnected. RTC provides information about seconds, minutes, hours, days, months, and years. It can automatically adjust the end date of the month and the leap year correction. The clock format can be 24 hours or 12 hours with AM/PM indication. It provides two programmable calendar alarms and one 1Hz square wave output. In addition, the RST check pin can be used as a key input for microprocessor reset.
Application
- Event Reminders
- MCU External Wakeup
- Alarm Clock
Features
- High anti-vibration
- Internal MEMS resonator and temperature compensator
- High precision: ±5ppm (±0.432 s/D) at -45°C to +85°C
- Clock calendar
- 2 programmable calendar alarms
- Low power consumption
Specification
- Operating Voltage: 3.3V~5.5V
- Operating Current: 130uA
- Low Power Current: 2uA
- Timing Range: 1970~2100 year
- Operating Temperature: -45℃~ +85℃
- Timing Precision: ±5ppm (±0.432 s/D)
- Dimension: 22.5x21mm / 0.886x0.827inch
Board Overview
Num | Label | Description |
---|---|---|
1 | VCC | + |
2 | GND | - |
3 | SCL | IIC clock line |
4 | SDA | IIC data line |
5 | INT | Interrupt in low level or 1Hz square wave output |
6 | RST | Reset calibration pin |
7 | 32K | 32.768KHz pulse output |
Tutorial
Compatibility
MCU | Work Well | Untested |
---|---|---|
Arduino Uno | √ | |
Mega2560 | √ | |
Leonardo | √ | |
ESP32 | √ | |
micro:bit | √ |
Requirements
Hardware
- DFRduino UNO R3 (or similar) x 1
- DS3231M MEMS Precise RTC x 1
- Jumper wires
Software
- Arduino IDE
- Download and install the DS3231M Library and examples (About how to install the library?)
API Function List
DFRobot_DS3231M(TwoWire *pWire = &Wire){_pWire = pWire;}; ~DFRobot_DS3231M(); /*! *@brief Init chip *@return True means IIC communication succeeds, false means it fails. */ bool begin(void); /*! *@brief Get current time data */ void getNowTime(); /*! *@brief get year *@return year */ uint16_t year(); /*! *@brief get month *@return month */ uint8_t month(); /*! *@brief get date *@return date */ uint8_t day(); /*! *@brief get hour *@return hour */ uint8_t hour(); /*! *@brief get minute *@return minute */ uint8_t minute(); /*! *@brief get second *@return second */ uint8_t second(); /*! *@brief get day of week *@return day of week */ const char* getDayOfTheWeek(); /*! *@brief Set year *@param Year */ void setYear(uint8_t year); /*! *@brief Set month *@param Month */ void setMonth(uint8_t month); /*! *@brief Set Date *@param Date */ void setDate(uint8_t date); /*! *@brief Set the hours and 12hours or 24hours *@param hour:1-12 in 12hours,0-23 in 24hours *@param mode:e24hours, eAM, ePM */ void setHour(uint8_t hour, ehours mode); /*! *@brief Set minute *@param Minute */ void setMinute(uint8_t minute); /*! *@brief Set second *@param Second */ void setSecond(uint8_t second); /*! *@brief Set the last compiled time as the current time *@param date Input compiling date *@param time Input compiling time */ void dateTime(const __FlashStringHelper* date, const __FlashStringHelper* time); /*! *@brief Adjust current time */ void adjust(); /*! *@brief output AM or PM of time */ const char* getAMorPM(); /*! *@brief Get current temperature *@return Current temperautre, unit: ℃ */ float getTemperatureC(); /*! *@brief Judge if it is power-down *@return If retrun true, power down, time needs to reset; false, work well. */ bool lostPower(void); /*! *@brief Read the value of pin sqw *@return eDS3231M_OFF = 0x01 // Off *@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave *@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave *@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave *@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave */ eDs3231MSqwPinMode_t readSqwPinMode(); /*! *@brief Set the vaule of pin sqw *@param mode eDS3231M_OFF = 0x01 // Off *@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave *@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave *@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave *@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave */ void writeSqwPinMode(eDs3231MSqwPinMode_t 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 eEveryMinute, *@n eMinutesMatch, *@n eMinutesHoursMatch, *@n eMinutesHoursDateMatch, *@n eMinutesHoursDayMatch, //Alarm2 *@n eUnknownAlarm *@n }eAlarmTypes; *@param days Alarm clock Day (day) *@param hours Alarm clock Hour (hour) *@param mode: e24hours, eAM, ePM *@param minutes Alarm clock (minute) *@param seconds Alarm clock (second) */ void setAlarm(const uint8_t alarmType,int16_t days,int8_t hours,ehours mode, int8_t minutes,int8_t seconds, const bool state = true); /*! *@brief Judge if the alarm clock is triggered *@return true, triggered; false, not trigger */ bool isAlarm(); /*! *@brief Clear trigger flag */ void clearAlarm(); /*! *@brief enable or disable the interrupt of alarm */ void enAbleAlarm1Int(); void disAbleAlarm1Int(); void enAbleAlarm2Int(); void disAbleAlarm2Int(); /*! *@brief enable the 32k output */ void enAble32k(); /*! *@brief disable the 32k output */ void disAble32k();
Connection Diagram
Sample Code
1. Get and set time
Set the time to the specified time, and the serial port prints time and temperature data every second.
NOTE:
- The accuracy of the internal temeprature compensator is for normal temperature detection. If your application requires high accuracy temperature measurement, you may want to add an extra temperature sensor with higher accuracy.
/*!
* @file getTimeAndTemperature.ino
* @brief Show current time
* @n Experiment phenomenon: read data every 1 seconds and print it on serial port.
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng](yufeng.luo@dfrobot.com)
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
DFRobot_DS3231M 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 eDS3231M_OFF = 0x01 // Not output square wave, enter interrupt mode
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
rtc.writeSqwPinMode(eDS3231M_SquareWave_1Hz);
/*!
*@brief Read the value of pin sqw
*@return mode eDS3231M_OFF = 0x01 // Off
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
//rtc.readSqwPinMode();
/*!
*@brief Set the last compiled time as the current time
*/
//rtc.dateTime();//If users use this function, please don't set time by other way
rtc.setYear(19);//Set year, default in the 21st century, input negative number for years in the 20th century.
rtc.setMonth(10);
rtc.setDate(23);
/*!
*@brief Set the hours and 12hours or 24hours
*@param hour:1-12 in 12hours,0-23 in 24hours
*@param mode:e24hours, eAM, ePM
*/
rtc.setHour(0,e24hours);
rtc.setMinute(59);
rtc.setSecond(40);
rtc.adjust();
/*!
*@brief enable the 32k output (default is enable)
*/
//rtc.disAble32k();
/*!
*@brief disable the 32k output
*/
//rtc.enAble32k();
}
void loop() {
/*!
*@brief Get current time data
*@return Current time data
*/
rtc.getNowTime();
Serial.print(rtc.year(), DEC);//year
Serial.print('/');
Serial.print(rtc.month(), DEC);//month
Serial.print('/');
Serial.print(rtc.day(), DEC);//date
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());//day of week
Serial.print(") ");
Serial.print(rtc.hour(), DEC);//hour
Serial.print(':');
Serial.print(rtc.minute(), DEC);//minute
Serial.print(':');
Serial.print(rtc.second(), 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.lostPower()) {
Serial.println("RTC lost power, plrase reset the time!");
}
}
Expected Results
2. Alarm Trigger
The example checks alarm trigger by the function rtc.isAlarm();
in loop.
NOTE:
- If the hour systems of the clock and alarm clock are not the same, the alarm clock will not be triggered. For example: the clock is 24-hour system, and the alarm clock is 12-hour system.
/*!
* @file setAlarmInQuery.ino
* @brief Set alarm clock
* @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 (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng](yufeng.luo@dfrobot.com)
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
DFRobot_DS3231M 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 eDS3231M_OFF = 0x01 // Not output square wave, enter interrupt mode
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
rtc.writeSqwPinMode(eDS3231M_OFF);
/*!
*@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 eEveryMinute,
*@n eMinutesMatch,
*@n eMinutesHoursMatch,
*@n eMinutesHoursDateMatch,
*@n eMinutesHoursDayMatch, //Alarm2
*@n eUnknownAlarm
*@n }eAlarmTypes;
*@param days Alarm clock Day (day)
*@param hours Alarm clock Hour (hour)
*@param mode: e24hours, eAM, ePM
*@param minutes Alarm clock (minute)
*@param seconds Alarm clock (second)
*/
rtc.setAlarm(eSecondsMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
rtc.setAlarm(eMinutesHoursDateMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
rtc.setYear(19);//Set year, default in the 21st century,
rtc.setMonth(8);
rtc.setDate(26);
/*!
*@brief Set the hours and 12hours or 24hours
*@param hour:1-12 in 12hours,0-23 in 24hours
*@param mode:e24hours, eAM, ePM
*/
rtc.setHour(11,ePM);//1-12 in 12hours,0-23 in 24hours
rtc.setMinute(59);
rtc.setSecond(50);
rtc.adjust();
}
void loop() {
/*!
*@brief Get current time data
*@return Current time data
*/
rtc.getNowTime();
/*!
*@brief Judge if the alarm clock is triggered
*@return true, triggered; false, not triggered
*/
if (rtc.isAlarm()){ // If the alarm bit is set
Serial.println("Alarm clock is triggered.");
/*!
*@brief Clear trigger flag
*/
rtc.clearAlarm();
}
Serial.print(rtc.year(), DEC);
Serial.print('/');
Serial.print(rtc.month(), DEC);
Serial.print('/');
Serial.print(rtc.day(), DEC);
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());
Serial.print(") ");
Serial.print(rtc.hour(), DEC);
Serial.print(':');
Serial.print(rtc.minute(), DEC);
Serial.print(':');
Serial.print(rtc.second(), DEC);
Serial.print(' ');
/*if rtc works in 24hours mode,this function doesn't print anything*/
Serial.print(rtc.getAMorPM());
Serial.println();
if (rtc.lostPower()) {
Serial.println("RTC lost power, please reset the time!");
}
delay(1000);
}
Expected Results
3. Alarm Trigger Interrupt
The example judges the alarm trigger by the pin INT of RTC module.
NOTE:
- In this sample, the pin INT of the sensor should be connected to the corresponding interrupt pin of the mainboard(Here selected the interrput pin D2 of UNO board).
AVR Series Interrupt Pin and Interrupt Number
328 Mainboards: Uno, Nano, Mini... | Mega2560 | 32u4 Mainboards: Leonardo... | |
---|---|---|---|
Interrupt Pin | D2, D3 | D2, D3, D21, D20, D19, D18 | D3, D2, D0, D1, D7 |
Interrupt Number | 0, 1 | 0, 1, 2, 3, 4, 5 | 0, 1, 2, 3 ,4 |
/*!
* @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 SQW pin with DIGITALPIN2
* @n print information on serial port after the alarm clock is triggered.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng](yufeng.luo@dfrobot.com)
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
volatile int8_t alarmFlag = 0;
DFRobot_DS3231M 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 eDS3231M_OFF = 0x01 // Not output square wave, enter interrupt mode
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
rtc.writeSqwPinMode(eDS3231M_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,
*@n eSecondsMatch,
*@n eSecondsMinutesMatch,
*@n eSecondsMinutesHoursMatch,
*@n eSecondsMinutesHoursDateMatch,
*@n eSecondsMinutesHoursDayMatch, //Alarm1
*@n
*@n eEveryMinute,
*@n eMinutesMatch,
*@n eMinutesHoursMatch,
*@n eMinutesHoursDateMatch,
*@n eMinutesHoursDayMatch, //Alarm2
*@n eUnknownAlarm
*@n }eAlarmTypes;
*@param days Alarm clock (day)
*@param hours Alarm clock (hour)
*@param mode: e24hours, eAM, ePM
*@param minutes Alarm clock (minute)
*@param seconds Alarm clock (second)
*/
//Alarm1
rtc.setAlarm(eSecondsMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,/*minute,0-59*/12,/*second,0-59*/35);
//Alarm2
rtc.setAlarm(eMinutesHoursDayMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,
/*minute,0-59*/13,/*second,0-59, this argument doesn't work in Alarm2*/42);
/*!
*@brief Judge if it is power-down
*@return if return true, power-down, time needs to reset; false, work well
*/
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
/*!
*@brief Adjust the current time
*/
rtc.setYear(19);//Set year, default in the 21st century.
rtc.setMonth(9);
rtc.setDate(30);
/*!
*@brief Set the hours and 12hours or 24hours
*@param hour:1-12 in 12hours,0-23 in 24hours
*@param mode:e24hours, eAM, ePM
*/
rtc.setHour(15,e24hours);
rtc.setMinute(12);
rtc.setSecond(30);
rtc.adjust();
}
attachInterrupt(0, interrupt, FALLING);
}
void loop() {
/*!
*@brief Judge if the alarm clock is triggered
*@return true, triggered; false, not triggered
*/
rtc.getNowTime();
Serial.print(rtc.year(), DEC);
Serial.print('/');
Serial.print(rtc.month(), DEC);
Serial.print('/');
Serial.print(rtc.day(), DEC);
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());
Serial.print(") ");
Serial.print(rtc.hour(), DEC);
Serial.print(':');
Serial.print(rtc.minute(), DEC);
Serial.print(':');
Serial.print(rtc.second(), 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.lostPower()) {
Serial.println("RTC lost power, please reset the time!");
}
}
void interrupt(){
alarmFlag = 1;
}
Expected Results
4. Low power consumption and awake
In the example, it enters low power consumption once clock and alarm set, then exits from the low power consumption mode when alarm triggered, and enters low power consumption again after 10 times prints.
NOTE:
- Note: Please connect the pin INT of sensor to the interrupt pin of maincontrol board(here selected D2 of UNO).
/*!
* @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 (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng](yufeng.luo@dfrobot.com)
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
#include <avr/sleep.h>
volatile int8_t alarmFlag = 0;
DFRobot_DS3231M 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 eDS3231M_OFF = 0x01 // Not output square wave, enter interrupt mode
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
rtc.writeSqwPinMode(eDS3231M_OFF);
rtc.enAbleAlarm1Int();
//rtc.disAbleAlarm1Int();
rtc.enAbleAlarm2Int();
//rtc.disAbleAlarm2Int();
/*!
*@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 eEveryMinute,
*@n eMinutesMatch,
*@n eMinutesHoursMatch,
*@n eMinutesHoursDateMatch,
*@n eMinutesHoursDayMatch, //Alarm2
*@n eUnknownAlarm
*@n }eAlarmTypes;
*@param days Alarm clock (day)
*@param hours Alarm clock (hour)
*@param mode: e24hours, eAM, ePM
*@param minutes Alarm clock (minute)
*@param seconds Alarm clock (second)
*/
rtc.setAlarm(eSecondsMatch,/*date,0-30*/29,/*hour,1-12 in 12hours,0-23 in 24hours*/9,e24hours,/*minute,0-59*/10,/*second,0-59*/40);
rtc.setYear(19);//Set year, default in the 21st century.
rtc.setMonth(9);
rtc.setDate(29);
/*!
*@brief Set the hours and 12hours or 24hours
*@param hour:1-12 in 12hours,0-23 in 24hours
*@param mode:e24hours, eAM, ePM
*/
rtc.setHour(9,e24hours);
rtc.setMinute(10);
rtc.setSecond(30);
rtc.adjust();
attachInterrupt(0, interrupt, FALLING);
/*!
*@brief Set avr sleep mode
*/
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_cpu();
sleep_enable();
}
void loop() {
/*!
*@brief Judge if the alarm clock is triggered
*@return true, triggered; false, not triggered
*/
if(alarmFlag == 1){
Serial.println("Alarm is up");
rtc.clearAlarm();
alarmFlag = 0;
while (t < 10){
rtc.getNowTime();
Serial.print(rtc.year(), DEC);
Serial.print('/');
Serial.print(rtc.month(), DEC);
Serial.print('/');
Serial.print(rtc.day(), DEC);
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());
Serial.print(") ");
Serial.print(rtc.hour(), DEC);
Serial.print(':');
Serial.print(rtc.minute(), DEC);
Serial.print(':');
Serial.print(rtc.second(), 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.lostPower()) {
Serial.println("RTC lost power, please reset the time!");
}
sleep_enable();
//energy.PowerDown();
}
void interrupt(){
alarmFlag = 1;
sleep_disable();
}
Expected Results
5. Get TNP time
The example is to get the current time from the NTP server and print it every second.
NOTE: This example requires network connection, so the ESP32 series main control board is necessary here.
If there is no enviroment, please configure the ESP32 compilation environment first.
After the ESP32 compilation environment is configured, please select the port of FireBeetle-ESP32 development board.
/*!
* @file getTimefromNTP.ino
* @brief Get time from ntpServer and show current time
* @n Experiment phenomenon: read data every second and print it on serial port.
* @n This demo only works on mpython
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng](yufeng.luo@dfrobot.com)
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
#include <WiFi.h>
#include "time.h"
DFRobot_DS3231M rtc;
const char* ssid = "dfrobotYanfa";//wlan information
const char* password = "hidfrobot";
const char* ntpServer = "ntp.ntsc.ac.cn";//local ntp server
const long gmtOffset_sec = 8*3600; //GMT+08:00
const int daylightOffset_sec = 0;
void getTimeFromNTP()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
rtc.setYear(timeinfo.tm_year - 100);
rtc.setMonth(timeinfo.tm_mon + 1);
rtc.setDate(timeinfo.tm_mday);
rtc.setHour(timeinfo.tm_hour, e24hours);
rtc.setMinute(timeinfo.tm_min);
rtc.setSecond(timeinfo.tm_sec);
rtc.adjust();
}
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()
{
rtc.getNowTime();
Serial.print(rtc.year(), DEC);//year
Serial.print('/');
Serial.print(rtc.month(), DEC);//month
Serial.print('/');
Serial.print(rtc.day(), DEC);//date
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());//day of week
Serial.print(") ");
Serial.print(rtc.hour(), DEC);//hour
Serial.print(':');
Serial.print(rtc.minute(), DEC);//minute
Serial.print(':');
Serial.print(rtc.second(), DEC);//second
Serial.println();
delay(1000);
}