ESP32断电后时间就归零,重启还要重新联网对时,很多项目等不起——定时浇花、闹钟插座、数据打时间戳,都想要一块断电也走时的芯片。DS3231就是干这个的:内置温补晶振,官方标称温度范围内误差每年约2分钟以内,比DS1307的民用晶振准得多,还自带纽扣电池座和一路温度传感器。这篇把它接上ESP32,用RTClib读时间、设时间。

接线与上电前的检查
DS3231也是I2C设备:SCL接GPIO22、SDA接GPIO21、VCC接3.3V、GND共地。模块背面的纽扣电池座装一颗CR2032,主电源断掉以后时钟照走。这里有个新手必看的坑:很多便宜模块板载了充电电路,原设计给LIR2032可充电电池用的;如果你装的是不可充电的CR2032,要把板上的充电电阻取下来或者挑断,不然长期通电会给一次性电池充电,有鼓包风险。

装库
库管理器搜RTClib装Adafruit官方版即可,它同时支持DS1307、DS3231和PCF8523。装不上先确认没有装到同名旧版。

接线图与代码
四根线连好就是下面这个样子,模块上SCL、SDA、VCC、GND四个焊盘一一对应:

下面的示例先在rtc.adjust里写死一次编译时间给模块对时,然后每秒从模块读回时间和片上温度打印到串口:
/*********
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete instructions at https://RandomNerdTutorials.com/esp32-ds3231-real-time-clock-arduino/
*********/
// Based on the RTCLib Library examples: https://github.com/adafruit/RTClib
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
// Get the current time from the RTC
DateTime now = rtc.now();
// Getting each time field in individual variables
// And adding a leading zero when needed;
String yearStr = String(now.year(), DEC);
String monthStr = (now.month() < 10 ? "0" : "") + String(now.month(), DEC);
String dayStr = (now.day() < 10 ? "0" : "") + String(now.day(), DEC);
String hourStr = (now.hour() < 10 ? "0" : "") + String(now.hour(), DEC);
String minuteStr = (now.minute() < 10 ? "0" : "") + String(now.minute(), DEC);
String secondStr = (now.second() < 10 ? "0" : "") + String(now.second(), DEC);
String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];
// Complete time string
String formattedTime = dayOfWeek + ", " + yearStr + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minuteStr + ":" + secondStr;
// Print the complete formatted time
Serial.println(formattedTime);
// Getting temperature
Serial.print(rtc.getTemperature());
Serial.println("ºC");
Serial.println();
delay(3000);
}
对时逻辑要注意:rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))用的是编译那一刻的时间,烧录后模块会停在编译时刻附近,误差就是编译到上电的间隔,所以别把烧录好的板子放半小时再上电。对时成功后把adjust那一行注释掉再重新烧录,避免每次重启都重置时间。
三个常见问题
读出来全是2165年或者01/01/2000:I2C没通,先扫地址确认0x68能被扫到,再查SDA和SCL有没有接反。时间走了几分钟就停:纽扣电池没电或者电池座接触不良,主电一断就归零。温度读数偏高两三度:这是芯片自热加封装热阻的正常现象,它标的是芯片结温附近的环境温度,拿来当环境温度计要打个折。
如果项目里同时挂了OLED等I2C设备,DS3231地址0x68不冲突,直接并联到同一组SCL/SDA就行。











温度偏高两三度原来是芯片自热,长知识
一年两分钟对定时浇花够用了,比NTP省心
0x68和OLED并联没冲突,实测能一起挂
充电电阻那个坑第一次听说,还好我的是LIR2032
对时后要注释掉adjust这点太重要了,我重启就重置折腾好久