Arduino - OLED-Uhr

In diesem Tutorial lernen wir, wie man eine OLED-Uhr herstellt, indem man:

Sie können eines von zwei RTC-Modulen wählen: DS3231 und DS1307. Siehe DS3231 vs DS1307

Über OLED, DS3231 und DS1307 RTC-Modul

Wenn Sie nichts über OLED, DS3231 und DS1307 (Pinbelegung, wie sie funktionieren und wie man programmiert ...) wissen, informieren Sie sich in den folgenden Tutorials darüber:

OLED- und RTC-Bibliotheken installieren

  • Navigieren Sie zum Bibliotheken-Symbol in der linken Leiste der Arduino IDE.
  • Suchen Sie „SSD1306“, dann finden Sie die SSD1306-Bibliothek von Adafruit.
  • Klicken Sie auf die Installieren-Schaltfläche, um die Bibliothek zu installieren.
Arduino OLED-Bibliothek
  • Sie werden aufgefordert, einige weitere Bibliotheksabhängigkeiten zu installieren.
  • Klicken Sie auf die Alle installieren Schaltfläche, um alle Bibliotheksabhängigkeiten zu installieren.
Arduino Adafruit GFX-Sensor-Bibliothek
  • Suchen Sie nach “RTClib”, dann finden Sie die RTC-Bibliothek von Adafruit. Diese Bibliothek funktioniert sowohl mit dem DS3231 als auch mit dem DS1307
  • Klicken Sie auf die Schaltfläche Installieren, um die RTC-Bibliothek zu installieren.
Arduino Echtzeituhr-Bibliothek
  • Sie werden möglicherweise aufgefordert, weitere Bibliotheksabhängigkeiten zu installieren.
  • Klicken Sie auf die Schaltfläche Alle installieren, um alle Bibliotheksabhängigkeiten zu installieren.
Arduino RTC-Abhängigkeitsbibliothek

Zeit vom DS3231 RTC-Modul auslesen und auf dem OLED anzeigen

Verdrahtungsdiagramm

Arduino DS3231 OLED-Verdrahtungsdiagramm

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Arduino-Code - DS3231 und OLED

/* * Dieser Arduino Code wurde von newbiely.de entwickelt * Dieser Arduino Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino/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_DS3231 rtc; 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(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // 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() { DateTime now = rtc.now(); time = ""; time += now.hour(); time += ':'; time += now.minute(); time += ':'; time += now.second(); oledDisplayCenter(time); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Schnelle Schritte

  • Kopieren Sie den obigen Code und öffnen Sie ihn mit der Arduino-IDE
  • Klicken Sie auf die Schaltfläche Hochladen in der Arduino-IDE, um den Code auf Arduino hochzuladen
  • Sehen Sie das Ergebnis auf dem OLED-Display

Zeit vom DS1307 RTC-Modul auslesen und auf dem OLED-Display anzeigen.

Verdrahtungsdiagramm

Arduino DS1307 OLED Verdrahtungsdiagramm

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Arduino-Code - DS1307 und OLED

/* * Dieser Arduino Code wurde von newbiely.de entwickelt * Dieser Arduino Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino/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 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(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // 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() { DateTime now = rtc.now(); time = ""; time += now.hour(); time += ':'; time += now.minute(); time += ':'; time += now.second(); oledDisplayCenter(time); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Schnelle Schritte

  • Kopieren Sie den obigen Code und öffnen Sie ihn mit der Arduino IDE.
  • Klicken Sie auf die Schaltfläche Hochladen in der Arduino IDE, um den Code auf den Arduino hochzuladen.
  • Sehen Sie das Ergebnis auf dem OLED-Display.

Video Tutorial

Wir erwägen die Erstellung von Video-Tutorials. Wenn Sie Video-Tutorials für wichtig halten, abonnieren Sie bitte unseren YouTube-Kanal , um uns zu motivieren, die Videos zu erstellen.

※ UNSERE NACHRICHTEN

  • Sie können gerne den Link zu diesem Tutorial teilen. Bitte verwenden Sie jedoch unsere Inhalte nicht auf anderen Websites. Wir haben viel Mühe und Zeit in die Erstellung der Inhalte investiert, bitte respektieren Sie unsere Arbeit!