ESP32 - OLED Klok

In deze tutorial begeleiden we u stap voor stap bij het maken van een OLED-klok met een ESP32 door de volgende stappen te behandelen:

U heeft de vrijheid om te kiezen tussen twee RTC-modules: DS3231 en DS1307. Om een weloverwogen keuze te maken kunt u de vergelijking bekijken in DS3231 vs DS1307.

Deze tutorial biedt een volledige handleiding voor het implementeren van een OLED-klok en laat zien hoe u de ESP32 integreert met de DS3231 of DS1307 RTC-module om nauwkeurige tijdinformatie op een OLED-scherm weer te geven.

Over OLED, DS3231 en DS1307 RTC-module

Bent u nog niet bekend met OLED, DS3231 en DS1307, inclusief hun pinout, functionaliteit en programmering? Verken uitgebreide tutorials over deze onderwerpen hieronder:

Installeren van OLED- en RTC-bibliotheken

  • Klik op het Bibliotheken icoon in de linkerbalk van de Arduino IDE.
  • Zoek op “SSD1306” en vind de SSD1306 bibliotheek van Adafruit.
  • Klik op de knop Installeren om de bibliotheek te installeren.
ESP32 OLED bibliotheek
  • Er wordt gevraagd om extra bibliotheekafhankelijkheden te installeren.
  • Klik op de knop Installeer alles om alle afhankelijkheden te installeren.
ESP32 Adafruit GFX sensor bibliotheek
  • Zoek op “RTClib” en vind de RTC bibliotheek van Adafruit.
  • Klik op de knop Installeren om de RTC-bibliotheek te installeren.
ESP32 RTC bibliotheek
  • Mogelijk wordt gevraagd de afhankelijkheden voor deze bibliotheek te installeren.
  • Installeer alle afhankelijkheden door op Installeer alles te klikken.
ESP32 Adafruit BusIO bibliotheek

Tijd uitlezen van DS3231 RTC module en weergeven op OLED

Bedradingsschema

ESP32 DS3231 OLED bedrading

This image is created using Fritzing. Click to enlarge image

Als u niet weet hoe u ESP32 en andere componenten van stroom moet voorzien, vindt u instructies in de volgende tutorial: Hoe ESP32 van stroom te voorzien.

ESP32 Code - DS3231 en OLED

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-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(); }

Snel aan de slag

  • Als u ESP32 voor het eerst gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Voer de bedrading uit zoals in bovenstaande afbeelding.
  • Verbind de ESP32 met uw PC via een micro USB-kabel.
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32-bord (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Kopieer de bovenstaande code en open deze in Arduino IDE.
  • Klik op de knop Uploaden in Arduino IDE om de code naar de ESP32 te uploaden.
  • Bekijk het resultaat op het OLED-scherm.

Tijd uitlezen van DS1307 RTC module en weergeven op OLED

Bedradingsschema

ESP32 DS1307 OLED bedrading

This image is created using Fritzing. Click to enlarge image

ESP32 Code - DS1307 en OLED

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-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(); }

Snel aan de slag

  • Als u ESP32 voor het eerst gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Voer de bedrading uit zoals in bovenstaande afbeelding.
  • Verbind de ESP32 met uw PC via een micro USB-kabel.
  • Open Arduino IDE op uw PC.
  • Selecteer het juiste ESP32-bord (bijv. ESP32 Dev Module) en de juiste COM-poort.
  • Kopieer de bovenstaande code en open deze in Arduino IDE.
  • Klik op de knop Uploaden in Arduino IDE om de code naar de ESP32 te uploaden.
  • Bekijk het resultaat op het OLED-scherm.

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!