Mengatur waktu rtc ds3231 dengan library RTCLib

Pengaturan waktu atau adjust rtc ds3231 bisa dilakukan dengan cara berikut :
1. Pengaturan langsung
a. Parameter jumlah detik sejak 1 Januari 1970 (UTC)

1
rtc.adjust(DateTime(1740199920));

angka 1740199920 adalah nilai utc pada 22 Februari 2025 11:52:00

b. Parameter tanggal wan waktu

1
rtc.adjust(DateTime(2025, 2, 22, 11, 52, 0));

parameter yang di inputkan masing-masing adalah tahun,bulan, tanggal, jam, menit dan detik.

c. Parameter string tanggal dan string waktu

1
rtc.adjust(DateTime("Feb 22 2025", "11:52:00"));

d. Paramter string ISO

1
rtc.adjust(DateTime("2025-02-22T11:52:00"));

2. Pengaturan otomatis sesuai waktu compile.

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

__DATE__ dan __TIME__ adalah macro preprocessor yang memiliki nilai string tanggal dan waktu pada saat compile, preprocessor lainnya seperti __FILE__, __LINE__, __TIMESTAMP__

3. Pengaturan dengan entry nilai
Untuk flexibilitas, pengaturan waktu rtc bisa dilakukan dengan entry melalui tombol, keypad, android, serial dll. Berikut ini adalah contoh pengaturan melalui tiga tombol:
Skema Pengaturan waktu modul rtc ds3231 meun tombol:

Koding arduino adjust rtc ds3231:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#define menuPin       8
#define plusPin       9
#define minusPin      10
 
#include <Wire.h>;
#include <LCD_I2C.h>;
#include <RTClib.h>;
 
LCD_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
DateTime now;
DateTime rtcSet;
 
enum menuMode {
  settingStandby,
  settingJam,
  settingMenit,
  settingDetik,
  settingTanggal,
  settingBulan,
  settingTahun,
  jumlahMenuMode,
};
 
char buffer[20];
byte menuIndex;
byte detikSebelumnya;
 
void setup() {
  pinMode(menuPin, INPUT_PULLUP);
  pinMode(plusPin, INPUT_PULLUP);
  pinMode(minusPin, INPUT_PULLUP);
 
  Serial.begin(9600);
  Serial.println(F("Mengatur waktu rtc ds3231 dengan library RTCLib"));
  Serial.println(F("https://www.semesin.com/project"));
  Serial.println();
 
  Wire.begin();
  Wire.beginTransmission(0x27);
  if (Wire.endTransmission())
  {
    lcd = LCD_I2C(0x3F, 16, 2);
  }
  lcd.begin();
 
  //  if (rtc.lostPower())
  //  {
  //  1a
  //  rtc.adjust(DateTime(1740199920));
  //
  //  1b
  //  rtc.adjust(DateTime(2025, 2, 22, 11, 52, 0));
  //
  //  1c
  //  rtc.adjust(DateTime("Feb 22 2025", "11:52:00"));
  //
  //  1d
  //  rtc.adjust(DateTime("2025-02-22T11:52:00"));
  //
  //  2
  //  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //}
 
  lcd.backlight();
  lcd.print("Adjust RTC");
  lcd.setCursor(0, 1);
  lcd.print("semesin.com");
  delay(3000);
 
  lcd.clear();
 
}
 
void loop() {
  now = rtc.now();
 
  if (detikSebelumnya != now.second())
  {
    detikSebelumnya = now.second();
 
    if (menuIndex == settingStandby) {
      tampilanWaktu(now);
    }
  }
 
  if (!digitalRead(menuPin)) {
    delay(50);
    if (!digitalRead(menuPin)) {
      if (menuIndex == settingStandby) {
        rtcSet = DateTime(now);
      }
 
      menuIndex++;
      menuIndex %= jumlahMenuMode;
 
      if (menuIndex == settingStandby) {
        rtc.adjust(DateTime(rtcSet));
      }
      tampilanMenu();
      while (!digitalRead(menuPin));
    }
  }
 
  if (!digitalRead(plusPin)) {
    delay(50);
    if (!digitalRead(plusPin)) {
      plusNilai();
      tampilanMenu();
      while (!digitalRead(plusPin));
    }
  }
 
  if (!digitalRead(minusPin)) {
    delay(50);
    if (!digitalRead(minusPin)) {
      minusNilai();
      tampilanMenu();
      while (!digitalRead(minusPin));
    }
  }
}
 
void tampilanMenu() {
  tampilanWaktu(rtcSet);
  switch (menuIndex) {
    case settingStandby:
      lcd.noBlink();
      break;
    case settingJam:
      lcd.setCursor(4, 1);
      lcd.blink();
      break;
    case settingMenit:
      lcd.setCursor(7, 1);
      break;
    case settingDetik:
      lcd.setCursor(10, 1);
      break;
    case settingTanggal:
      lcd.setCursor(3, 0);
      break;
    case settingBulan:
      lcd.setCursor(6, 0);
      break;
    case settingTahun:
      lcd.setCursor(9, 0);
      break;
  }
}
 
void tampilanWaktu(DateTime datetime) {
  sprintf(buffer, "%02d/%02d/%04d", datetime.day(), datetime.month(), datetime.year());
  lcd.setCursor(3, 0);
  lcd.print(buffer);
  sprintf(buffer, "%02d:%02d:%02d", datetime.hour(), datetime.minute(), datetime.second());
  lcd.setCursor(4, 1);
  lcd.print(buffer);
}
 
void plusNilai() {
  switch (menuIndex) {
    case settingStandby:
      break;
    case settingJam:
      rtcSet = rtcSet + TimeSpan(0, 1, 0, 0);
      break;
    case settingMenit:
      rtcSet = rtcSet + TimeSpan(0, 0, 1, 0);
      break;
    case settingDetik:
      rtcSet = rtcSet + TimeSpan(0, 0, 0, 1);
      break;
    case settingTanggal:
      rtcSet = rtcSet + TimeSpan(1, 0, 0, 0);
      break;
    case settingBulan:
      rtcSet = DateTime(rtcSet.year(), rtcSet.month() == 12 ? 1 : rtcSet.month() + 1, rtcSet.day(), rtcSet.hour(), rtcSet.minute(), rtcSet.second());
      break;
    case settingTahun:
      rtcSet = DateTime(rtcSet.year() + 1, rtcSet.month(), rtcSet.day(), rtcSet.hour(), rtcSet.minute(), rtcSet.second());
      break;
  }
}
 
void minusNilai() {
  switch (menuIndex) {
    case settingStandby:
      break;
    case settingJam:
      rtcSet = rtcSet - TimeSpan(0, 1, 0, 0);
      break;
    case settingMenit:
      rtcSet = rtcSet - TimeSpan(0, 0, 1, 0);
      break;
    case settingDetik:
      rtcSet = rtcSet - TimeSpan(0, 0, 0, 1);
      break;
    case settingTanggal:
      rtcSet = rtcSet - TimeSpan(1, 0, 0, 0);
      break;
    case settingBulan:
      rtcSet = DateTime(rtcSet.year(), rtcSet.month() == 1 ? 12 : rtcSet.month() - 1, rtcSet.day(), rtcSet.hour(), rtcSet.minute(), rtcSet.second());
      break;
    case settingTahun:
      rtcSet = DateTime(rtcSet.year() - 1, rtcSet.month(), rtcSet.day(), rtcSet.hour(), rtcSet.minute(), rtcSet.second());
      break;
  }
}

Library:

1. LCD_I2C-2.3.0
2. RTClib-1.13.0

Leave a Reply

Your email address will not be published. Required fields are marked *