Tiny RTC I2C modules คือโมดูลนาฬิกาเวลาจริง (Real-Time Clock: RTC) ที่ใช้การสื่อสารแบบ I2C (Inter-Integrated Circuit) เพื่อเชื่อมต่อกับไมโครคอนโทรลเลอร์ เช่น Arduino นิยมใช้เพราะสามารถนับเวลาต่อได้ แม้ว่าบอร์ดจะถูกตัดไฟฟ้าไปแล้ว โดยโมดูลจะใช้ถ่านเป็นตัวนับเวลาต่อ
1.ต่ออุปกรณ์
.png?alt=media&token=eb8c9797-7e66-4aaf-b0a0-3c6e719fbf64)
Tiny RTC > Arduino UNO
VCC>5VGND>GNDSCL>A5SDA>A4
2.ลงโปรแกรม
Copy โค้ดด้านล่าง เพื่อตั้งเวลาครั้งแรก
การอัปโหลดครั้งแรก สามารถตั้งเวลาโดยการแก้ second = 0; // Seconds minute = 30; // Minutes hour = 10; // Hours (24-hour format) dayOfWeek = 5; // Day of week (1=Sunday, 2=Monday, ..., 7=Saturday) dayOfMonth = 1; // Day of month month = 8; // Month year = 24; // Year (20xx)
1#include <Wire.h>
2
3#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
4
5// Declare variables for storing time data
6byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
7
8// Convert normal decimal numbers to binary coded decimal
9byte decToBcd(byte val) {
10 return ((val / 10 * 16) + (val % 10));
11}
12
13// Convert binary coded decimal to normal decimal numbers
14byte bcdToDec(byte val) {
15 return ((val / 16 * 10) + (val % 16));
16}
17
18// Function to set the current time
19void setDateDs1307() {
20 // Set your current time here
21 second = 0; // Seconds
22 minute = 30; // Minutes
23 hour = 10; // Hours (24-hour format)
24 dayOfWeek = 5; // Day of week (1=Sunday, 2=Monday, ..., 7=Saturday)
25 dayOfMonth = 1; // Day of month
26 month = 8; // Month
27 year = 24; // Year (20xx)
28
29 Wire.beginTransmission(DS1307_I2C_ADDRESS);
30 Wire.write(0); // Reset register pointer
31 Wire.write(decToBcd(second) & 0x7F); // 0 to bit 7 starts the clock
32 Wire.write(decToBcd(minute));
33 Wire.write(decToBcd(hour));
34 Wire.write(decToBcd(dayOfWeek));
35 Wire.write(decToBcd(dayOfMonth));
36 Wire.write(decToBcd(month));
37 Wire.write(decToBcd(year));
38 Wire.endTransmission();
39}
40
41// Function to get the date and time from the DS1307
42void getDateDs1307() {
43 // Reset the register pointer
44 Wire.beginTransmission(DS1307_I2C_ADDRESS);
45 Wire.write(0);
46 Wire.endTransmission();
47
48 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
49
50 // Read the data
51 second = bcdToDec(Wire.read() & 0x7F);
52 minute = bcdToDec(Wire.read());
53 hour = bcdToDec(Wire.read() & 0x3F);
54 dayOfWeek = bcdToDec(Wire.read());
55 dayOfMonth = bcdToDec(Wire.read());
56 month = bcdToDec(Wire.read());
57 year = bcdToDec(Wire.read());
58
59 // Print the time and date
60 Serial.print(hour, DEC);
61 Serial.print(":");
62 Serial.print(minute, DEC);
63 Serial.print(":");
64 Serial.print(second, DEC);
65 Serial.print(" ");
66 Serial.print(month, DEC);
67 Serial.print("/");
68 Serial.print(dayOfMonth, DEC);
69 Serial.print("/");
70 Serial.print(year, DEC);
71 Serial.print(" ");
72 Serial.println();
73}
74
75void setup() {
76 Wire.begin();
77 Serial.begin(9600);
78
79 setDateDs1307(); // Set the current time; comment this out after setting
80}
81
82void loop() {
83 delay(2000);
84 getDateDs1307(); // Get the time data from the RTC and print it
85}
861#include <Wire.h>
2
3#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
4
5// Declare variables for storing time data
6byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
7
8// Convert normal decimal numbers to binary coded decimal
9byte decToBcd(byte val) {
10 return ((val / 10 * 16) + (val % 10));
11}
12
13// Convert binary coded decimal to normal decimal numbers
14byte bcdToDec(byte val) {
15 return ((val / 16 * 10) + (val % 16));
16}
17
18// Function to set the current time
19void setDateDs1307() {
20 // Set your current time here
21 second = 0; // Seconds
22 minute = 30; // Minutes
23 hour = 10; // Hours (24-hour format)
24 dayOfWeek = 5; // Day of week (1=Sunday, 2=Monday, ..., 7=Saturday)
25 dayOfMonth = 1; // Day of month
26 month = 8; // Month
27 year = 24; // Year (20xx)
28
29 Wire.beginTransmission(DS1307_I2C_ADDRESS);
30 Wire.write(0); // Reset register pointer
31 Wire.write(decToBcd(second) & 0x7F); // 0 to bit 7 starts the clock
32 Wire.write(decToBcd(minute));
33 Wire.write(decToBcd(hour));
34 Wire.write(decToBcd(dayOfWeek));
35 Wire.write(decToBcd(dayOfMonth));
36 Wire.write(decToBcd(month));
37 Wire.write(decToBcd(year));
38 Wire.endTransmission();
39}
40
41// Function to get the date and time from the DS1307
42void getDateDs1307() {
43 // Reset the register pointer
44 Wire.beginTransmission(DS1307_I2C_ADDRESS);
45 Wire.write(0);
46 Wire.endTransmission();
47
48 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
49
50 // Read the data
51 second = bcdToDec(Wire.read() & 0x7F);
52 minute = bcdToDec(Wire.read());
53 hour = bcdToDec(Wire.read() & 0x3F);
54 dayOfWeek = bcdToDec(Wire.read());
55 dayOfMonth = bcdToDec(Wire.read());
56 month = bcdToDec(Wire.read());
57 year = bcdToDec(Wire.read());
58
59 // Print the time and date
60 Serial.print(hour, DEC);
61 Serial.print(":");
62 Serial.print(minute, DEC);
63 Serial.print(":");
64 Serial.print(second, DEC);
65 Serial.print(" ");
66 Serial.print(month, DEC);
67 Serial.print("/");
68 Serial.print(dayOfMonth, DEC);
69 Serial.print("/");
70 Serial.print(year, DEC);
71 Serial.print(" ");
72 Serial.println();
73}
74
75void setup() {
76 Wire.begin();
77 Serial.begin(9600);
78
79 setDateDs1307(); // Set the current time; comment this out after setting
80}
81
82void loop() {
83 delay(2000);
84 getDateDs1307(); // Get the time data from the RTC and print it
85}
86จากนั้นก็ให้ใช้โค้ดนี้ เพื่อแสดงเวลาปัจจุบัน
1#include <Wire.h>
2
3#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
4
5// Declare variables for storing time data
6byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
7
8// Convert normal decimal numbers to binary coded decimal
9byte decToBcd(byte val) {
10 return ((val / 10 * 16) + (val % 10));
11}
12
13// Convert binary coded decimal to normal decimal numbers
14byte bcdToDec(byte val) {
15 return ((val / 16 * 10) + (val % 16));
16}
17
18// Function to get the date and time from the DS1307
19void getDateDs1307() {
20 // Reset the register pointer
21 Wire.beginTransmission(DS1307_I2C_ADDRESS);
22 Wire.write(0);
23 Wire.endTransmission();
24
25 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
26
27 // Read the data
28 second = bcdToDec(Wire.read() & 0x7F);
29 minute = bcdToDec(Wire.read());
30 hour = bcdToDec(Wire.read() & 0x3F);
31 dayOfWeek = bcdToDec(Wire.read());
32 dayOfMonth = bcdToDec(Wire.read());
33 month = bcdToDec(Wire.read());
34 year = bcdToDec(Wire.read());
35
36 // Print the time and date
37 Serial.print(hour, DEC);
38 Serial.print(":");
39 Serial.print(minute, DEC);
40 Serial.print(":");
41 Serial.print(second, DEC);
42 Serial.print(" ");
43 Serial.print(month, DEC);
44 Serial.print("/");
45 Serial.print(dayOfMonth, DEC);
46 Serial.print("/");
47 Serial.print(year, DEC);
48 Serial.print(" ");
49 Serial.println();
50}
51
52void setup() {
53 Wire.begin();
54 Serial.begin(9600);
55}
56
57void loop() {
58 delay(2000);
59 getDateDs1307(); // Get the time data from the RTC and print it
60}
611#include <Wire.h>
2
3#define DS1307_I2C_ADDRESS 0x68 // the I2C address of Tiny RTC
4
5// Declare variables for storing time data
6byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
7
8// Convert normal decimal numbers to binary coded decimal
9byte decToBcd(byte val) {
10 return ((val / 10 * 16) + (val % 10));
11}
12
13// Convert binary coded decimal to normal decimal numbers
14byte bcdToDec(byte val) {
15 return ((val / 16 * 10) + (val % 16));
16}
17
18// Function to get the date and time from the DS1307
19void getDateDs1307() {
20 // Reset the register pointer
21 Wire.beginTransmission(DS1307_I2C_ADDRESS);
22 Wire.write(0);
23 Wire.endTransmission();
24
25 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
26
27 // Read the data
28 second = bcdToDec(Wire.read() & 0x7F);
29 minute = bcdToDec(Wire.read());
30 hour = bcdToDec(Wire.read() & 0x3F);
31 dayOfWeek = bcdToDec(Wire.read());
32 dayOfMonth = bcdToDec(Wire.read());
33 month = bcdToDec(Wire.read());
34 year = bcdToDec(Wire.read());
35
36 // Print the time and date
37 Serial.print(hour, DEC);
38 Serial.print(":");
39 Serial.print(minute, DEC);
40 Serial.print(":");
41 Serial.print(second, DEC);
42 Serial.print(" ");
43 Serial.print(month, DEC);
44 Serial.print("/");
45 Serial.print(dayOfMonth, DEC);
46 Serial.print("/");
47 Serial.print(year, DEC);
48 Serial.print(" ");
49 Serial.println();
50}
51
52void setup() {
53 Wire.begin();
54 Serial.begin(9600);
55}
56
57void loop() {
58 delay(2000);
59 getDateDs1307(); // Get the time data from the RTC and print it
60}
61
3.ผลลัพธ์
เมื่อเปิด Serial Monitor จะแสดงวันที่และเวลา ที่ดึงค่ามาจาก RTC
.png?alt=media&token=2ea8c1a7-51f5-4e87-94b1-9e57bc1f1f96)
