RS1307是一个低功耗的外置时钟模块,它可以让你的项目即使在断电的情况下,也能保证在重新启动后走时正确。
所需材料
- 1x Arduino UNO
- 1x RS1307时钟模块
- 4x 跳线
接线示意图
RS1307 | Arduino | |
---|---|---|
SDA | -> | Analog A4 |
SCL | -> | Analog A5 |
VCC | -> | 5V |
GND | -> | GND |
加载库文件
- 在这里下载RTClib库,然后手动加载到Arduino IDE中。
示例代码
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc; // Create a RealTimeClock object
void setup() {
Serial.begin(9600);
Serial.println("YourDuino.com DS1307 Real Time Clock - Set / Run Utility");
#if def AVR
Wire.begin();
#else
Wire1.begin();
#end if
rtc.begin();
// Start the RTC library code
/*----( SET the date and time. Comment OUT these lines after setting )----*/
// Put these "//" in front of the line you do NOT want to use // 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
// May 21, 2015 at 6pm you would call: (use 24 hour time)
// rtc.adjust(DateTime(2015, 5, 21, 18, 0, 0));
}
//--(end setup )---
void loop(){
DateTime now = rtc.now(); // Read data from the RTC Chip
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();
delay(3000);
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE//*********( THE END )***********
输出结果
---------------------( COPY )--------------------------
2015/4/26 19:29:32
2015/4/26 19:29:35
2015/4/26 19:29:38
-----------------( END COPY )----------------------
参考文献
https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick