|
在本篇文章中,我们将学习如何将RTC模块DS3231与NodeMCU ESP8266 12E开发板和1602 LCD显示屏连接。我们将使用DS3231实时时钟(RTC)模块跟踪正确的时间和日期,在OLED显示屏上进行显示,并使用ESP8266作为微控制器。
也可以使用DS1307代替DS3231。 DS3231 RTC具有内置的闹钟功能以及温度传感器,其分辨率为0.25,精度为±3°C,这使该项目更加容易实现。
所需的组件 以下是制作本文所需的组件: ● NodeMCU ESP8266 12E开发板 ● DS3231 RTC模块 ● 1602 I2C LCD显示屏
DS3231 RTC模块 DS3231是一款低成本、非常精确的I2C实时时钟(RTC),具有集成的温度补偿晶体振荡器(TCXO)。该器件具有电池输入引脚,并且在器件的主电源中断时可以保持准确的计时。 
RTC可以记录秒、分钟、小时、日期、月份和年份的信息。对于少于31天的月份,将自动调整月末的日期,包括闰年的更正。时钟以24小时制或12小时制运行,并带有低电平有效AM / PM指示器。提供两个可编程的时间闹钟和一个可编程的方波输出。
精密的温度补偿电压基准和比较器电路监视VCC的状态,以检测电源故障,提供复位输出并在必要时自动切换到备用电源。另外,RST引脚低电平有效,当按钮输入时产生一个微控制器复位信号。
主要特征: ● 高精度RTC管理所有计时功能 ● 实时时钟计数秒、分钟、小时、月、周、日和年,闰年补偿有效期至2100 ● 0°C到+ 40°C的精度为±2ppm ● -40°C至+ 85°C的精度为±3.5ppm ● 数字温度传感器输出:±3°C精度 ● 低电平有效RST输出/按钮复位去抖输入 ● 两个时间闹钟 ● 可编程方波输出信号 ● 简单的串行接口连接到大多数微控制器 ● 快速(400kHz)I2C接口 ● 电池备用输入,可连续计时 ● 低功耗运行可延长电池备份运行时间 ● 工作温度范围:商业(0°C至+ 70°C)和工业(-40°C至+ 85°C)
ESP8266与DS3231的实时时钟的连接 以下是DS3231模块与NodeMCU ESP8266连接的电路图。连接非常简单。您也可以在面包上组装电路。
DS3231和1602显示屏均为I2C模块。因此,我们只需要2个引脚即可进行连接。因此,将串行数据(SDA)引脚连接到NodeMCU的D2引脚,并将串行时钟(SCL)连接到NodeMCU的D1引脚。通过NodeMCU的Vin引脚引脚为LCD和RTC模块提供5V的电压。您也可以为DS3231模块使用3.3V电源。
源代码/程序 ESP8266连接DS3231 RTC模块的源代码如下。您可以复制代码并将其直接上传到NodeMCU ESP8266-12E开发板。但是在此之前,您将需要一个库,即RTC模块的库。因此,请首先从下面的链接下载该库,然后将其添加到Arduino IDE。 ● 用于DS3231的RTClib库 - #include <Wire.h> // Library for I2C communication
- #include <SPI.h> // not used here, but needed to prevent a RTClib compile error
- #include "RTClib.h"
- #include <LiquidCrystal_I2C.h> // Library for LCD
- LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
- RTC_DS1307 RTC; // Setup an instance of DS1307 naming it RTC
- void setup ()
- {
- Serial.begin(57600); // Set serial port speed
- lcd.begin();
- Wire.begin(); // Start the I2C
- RTC.begin(); // Init RTC
- RTC.adjust(DateTime(__DATE__, __TIME__)); // Time and date is expanded to date and time on your computer at compiletime
- Serial.print('Time and date set');
- lcd.setCursor(0, 0);
- lcd.print("Real Time Clock");
- delay(3000);
- lcd.clear();
- }
- 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();
-
- lcd.setCursor(0, 0);
- lcd.print("Date: ");
- lcd.setCursor(0, 1);
- lcd.print("Time: ");
-
- lcd.setCursor(6, 0);
- lcd.print(now.year(), DEC);
- lcd.print(":");
- lcd.print(now.month(), DEC);
- lcd.print(":");
- lcd.print(now.day(), DEC);
- lcd.setCursor(6, 1);
- lcd.print(now.hour(), DEC);
- lcd.print(":");
- lcd.print(now.minute(), DEC);
- lcd.print(":");
- lcd.print(now.second(), DEC);
- delay(1000);
- lcd.clear();
- }
复制代码
ESP8266连接DS3231实时时钟的运行结果 代码上传后,RTC模块将开始工作。时间和日期将显示在1602 LCD显示屏中。不需要其他设置,也不需要任何额外的按钮或开关。
|