#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//RTC
#include <RTClib.h>
RTC_DS3231 rtc;
DateTime nows;
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while(1); //for (;;); // Don't proceed, loop forever
}
// Clear the buffer
//atur ukuran tulisan
display.setTextSize(1); // ukuran normal 1:1
display.setTextColor(WHITE); // tulisan warna putih
display.clearDisplay();
display.print("test RTC");
display.display();
delay(1000);
Serial.println(F("\r\nCek RTC"));
display.setCursor(0, 20);
display.print("CEK RTC");
display.display();
delay(1000);
if (!rtc.begin()) {
Serial.println(F("RTC Error"));
display.setCursor(0, 30);
display.print("RTC ERROR!!!");
display.display();
while (1);
}
else {
Serial.println(F("\r\nRTC OK"));
display.setCursor(0, 30);
display.print("RTC OK!");
display.display();
}
delay(1000);
display.clearDisplay();
}
void loop() {
display.clearDisplay();
nows = rtc.now();
int tahun = nows.year();
byte bulan = nows.month();
byte hari = nows.day();
byte jam = nows.hour();
byte menit = nows.minute();
byte detik = nows.second();
char tanggal[20]="yyyy/mm/dd hh:mm:ss";
sprintf(tanggal,"%d/%d/%d %02d:%02d:%02d", tahun, bulan, hari, jam, menit, detik);
Serial.println(tanggal);
display.setCursor(0,0);
display.print(tanggal);
display.display();
delay(1000);
}