DS3231实时时钟接ESP32:断电走时不断片

2026-12-06 13:16:11 14阅读 所属项目:ESP32 开发板

给ESP32做项目要记录时间时,很多人第一反应是连WiFi上NTP对时。可一旦部署在没有网络的车库、大棚或者配电箱里,断电重启后时间就归零了。这时候一颗几十块钱不到的DS3231实时时钟模块就是标准答案:自带电池、自带温补晶振,断电照走,来电即用。这篇把接线、设时、读数一次跑通。

模块能耐:温补晶振加双闹钟

DS3231时钟模块正面

DS3231用的是32kHz温补晶振(TCXO),时钟精度不受温度漂移影响,这点比老前辈DS1307强不少。模块通过I2C通信,默认地址0x68;除了走时,片上还能存两个独立闹钟,可以按具体时刻或者日期触发,闹钟触发时INT引脚会拉低给主控一个中断信号。另外它还能输出1Hz等频率的方波。模块背面还挂了一颗24C32 EEPROM,同样走I2C,可以存断电不丢的自定义数据。

电池方面有个坑:模块配的可充电LIR2032电池,充电电路是出厂焊好的。如果你想换成不可充电的CR2032,必须先把标注R4的那颗电阻拆掉断开充电回路,否则给一次性电池充电有鼓包风险。

接线:还是I2C那两根线

ESP32与DS3231接线示意

DS3231模块引出SCL、SDA、VCC、GND四个常用脚。接ESP32时SCL接GPIO22、SDA接GPIO21,VCC接3.3V即可,模块工作电压范围宽,3.3V和5V都能跑。装好电池后即使ESP32断电,时钟也在持续走。

装库与设时

库管理器里搜“RTClib”安装Adafruit官方库。第一次上电或者断电后重新来电,rtc.lostPower()会返回true,此时用编译时间给RTC校准一次:

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

这行用的是编译那一刻的时间,有一点点偏差但日常够用;要精确就用显式写法,比如rtc.adjust(DateTime(2026, 12, 6, 13, 0, 0))。校准过一次后记得把这行注释掉,否则每次重启都会把时间拨回编译时刻。

完整代码:读时间顺带读温度

DS3231内部那颗温补晶振顺便还能报温度,一起读出来:

/*********
  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));
  }
}

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);
}

看结果

DS3231与ESP32面包板实测接线

串口波特率115200,每三秒刷一行完整日期时间加星期,再跟一行芯片温度。做数据采集项目时把这套时间戳直接写进日志文件,断电也不怕乱序。

下一步:用上双闹钟

RTClib对两个闹钟都有现成API,可以设成每天固定时刻触发,触发时INT脚拉低接到ESP32任意GPIO做外部中断——正好配合深度睡眠,让设备平时睡死省电,到点被闹钟唤醒干活,一套低成本定时采集方案就成型了。


作者:工位上的小周
链接:https://www.aiddithome.com/p/10cf88bede1fce.html
本作版权归作者所有,未经允许请勿转载。若存在违规行为,您可以点击 “举报”。


登录 后发表评论
4条评论
阿伟不emo
1楼 · 3小时前

LIR2032换CR2032要拆R4这个提醒很关键,差点直接换

参数党小马
2楼 · 3小时前

lostPower那个坑我也踩过,每次重启时间都被拨回编译时刻

第三次终于亮了
3楼 · 3小时前

时钟模块配深度睡眠做定时采集,这个组合真香

刷视频进来围观
4楼 · 3小时前

温度比室温略高这个细节观察挺细,我第一次看还以为坏了