Arduino - OLED Klok

In deze tutorial leren we hoe u een OLED klok maakt door:

U kunt kiezen uit twee RTC-modules: DS3231 en DS1307. Zie DS3231 vs DS1307

Over OLED, DS3231 en DS1307 RTC module

Als u niet bekend bent met OLED, DS3231 en DS1307 (pinout, werking, programmeren...), leer er dan meer over in de volgende tutorials:

OLED en RTC Libraries installeren

  • Navigeer naar het Libraries icoon in de linkerzijbalk van de Arduino IDE.
  • Zoek naar “SSD1306” en vind vervolgens de SSD1306 library van Adafruit
  • Klik op de knop Install om de library te installeren.
Arduino OLED library
  • U wordt gevraagd om enkele andere library-onderdelen te installeren
  • Klik op Install All om alle afhankelijkheden te installeren.
Arduino Adafruit GFX sensor library
  • Zoek naar “RTClib” en vind vervolgens de RTC library van Adafruit. Deze library werkt zowel met DS3231 als DS1307
  • Klik op de knop Install om de RTC library te installeren.
Arduino RTC library
  • U kunt gevraagd worden om enkele andere afhankelijkheden te installeren
  • Klik op Install All om alle afhankelijkheden te installeren.
Arduino rtc dependency library

Tijd lezen van DS3231 RTC module en weergeven op OLED

Aansluitschema

Arduino DS3231 OLED Aansluitschema

This image is created using Fritzing. Click to enlarge image

Arduino Code - DS3231 en OLED

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

Snelle stappen

  • Kopieer bovenstaande code en open deze met de Arduino IDE
  • Klik op de knop Upload in de Arduino IDE om de code naar de Arduino te uploaden
  • Bekijk het resultaat op de OLED

Tijd lezen van DS1307 RTC module en weergeven op OLED

Aansluitschema

Arduino DS1307 OLED Aansluitschema

This image is created using Fritzing. Click to enlarge image

Arduino Code - DS1307 en OLED

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

Snelle stappen

  • Kopieer bovenstaande code en open deze met de Arduino IDE
  • Klik op de knop Upload in de Arduino IDE om de code naar de Arduino te uploaden
  • Bekijk het resultaat op de OLED

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!