DS3231 Clock Module
The DS3231M is a low-cost, high-precision I2C real-time clock (RTC). The device includes a battery input to maintain accurate timing when mains power is disconnected. The integrated microelectromechanical system (MEMS) improves the long-term accuracy of the device and reduces the number of components in the production line.The DS3231M is packaged in the same device package as the popular DS3231 RTC.The RTC holds seconds, minutes, hours, days of the week, date, month, and year information. Months with fewer than 31 days will automatically adjust the date at the end of the month, including leap year correction. Clock format can be 24-hour or 12-hour format with AM/PM indication. Two settable calendar alarms and a 1Hz output are provided. Address and data are transmitted serially over the I 2C bidirectional bus. Precision, temperature-compensated voltage reference and comparator circuits are used to monitor VCC status, detect power supply failures, provide reset outputs, and automatically switch to backup power if necessary. In addition, the RST monitor pin can be used as a key input to generate a microprocessor reset.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.10.62a36a4bHYzQJk&id=522553195737
Baidu Netdisk Download Link:
https://pan.baidu.com/s/1HjUmpkC0kCk7tNnVgYd9Zg
Password:dmlk
Specifications
Operating Voltage:2.3-5.5V
Operating Current:200-300uA
Time Accuracy: ±0.432 seconds/day
Control Method: IIC
Number of Pins:4 Pin (2.54mm pitch)
Description:with battery, with power failure detection and battery switching function
Hardware Connection
- VCC connects to the 5V of the development board.
- GND connects to the GND of the development board.
- SDA (Serial Data Line) connects to A4 (or SDA port) of the development board.
- SCL (Serial Clock Line) connects to A5 (or SCL port) of the development board.
Usage Method
Install Library
You need to ensure that the RTClib
ibrary is installed to operate the RTC module. If it's not installed, open the Arduino IDE, go to Tools -> Manage Libraries, search for RTClib
, and then install it.
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 12, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
// 检查Wire库是否启动而且DS3231 RTC是否在运行
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// 检查DS3231是否正在运行
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// 如果RTC失去了电力,设置日期和时间
// 以下日期/时间是编译这个草稿的时间
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
// 打印日期及时间
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// 如果需要,也可以获取温度
float temperature = rtc.getTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
delay(1000); // 等待1000毫秒(1秒)
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62