/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-oled-clock
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C
RTC_DS1307 rtc;
String date;
String time;
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(1000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
//oled.setCursor(0, 10); // position to display
// SETUP RTC MODULE
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (true);
}
// automatically sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
time.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
// Clear the OLED display
oled.fillScreen(BLACK);
DateTime now = rtc.now();
date = "";
date += now.day();
date += "/";
date += now.month();
date += "/";
date += now.year();
time = "";
time += now.hour();
time += ':';
time += now.minute();
time += ':';
time += now.second();
// Print the date and time to the OLED display
oled.getTextBounds(date, 0, 0, &x1, &y1, &width, &height);
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT + height) / 2);
oled.println(date);
oled.getTextBounds(time, 0, 0, &x1, &y1, &width, &height);
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - 3*height) / 2);
oled.print(time);
oled.display();
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW