Aokiji_Master
Forum Veteran
done
Last edited:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
RTC_DS3231 rtc;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
Wire.begin();
rtc.begin();
if (! rtc.isrunning()) {
display.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
display.display();
delay(2000);
}
}
void loop() {
DateTime now = rtc.now();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.clearDisplay();
display.println("Current Time:");
display.setTextSize(3);
display.print(now.hour());
display.print(":");
if (now.minute() < 10) {
display.print("0");
}
display.println(now.minute());
display.setTextSize(2);
display.println("Date:");
display.setTextSize(3);
display.print(now.month());
display.print("/");
display.print(now.day());
display.print("/");
display.println(now.year());
display.display();
delay(1000);
}
setup()
function, we initialize the OLED display and RTC module. If the RTC module is not running, we set the time to the compile time of the sketch.loop()
function, we get the current time from the RTC module using rtc.now()
. We then display the time and date on the OLED display using the display.print()
and display.println()
functions.