Arduino Nano - OLED Klok

Deze handleiding legt uit hoe u een OLED-klok maakt met behulp van de Arduino Nano, een RTC-module en een OLED-display. De tutorial bevat instructies voor zowel het DS3231 als het DS1307 RTC-module. In detail:

U kunt kiezen uit twee RTC-modules: DS3231 en DS1307. Voor meer informatie verwijzen wij u naar DS3231 vs DS1307.

Over OLED, DS3231 en DS1307 RTC-module

Als u niet bekend bent met OLED, DS3231 en DS1307 (pinout, werking, programmeren, enz.), kunnen de volgende tutorials u helpen:

Installeer OLED en RTC Libraries

  • Klik op het icoon Libraries in de linkerzijbalk van de Arduino IDE.
  • Zoek naar “SSD1306” en vind de SSD1306 bibliotheek van Adafruit.
  • Druk op de knop Installeren om de installatie te voltooien.
Arduino Nano OLED bibliotheek
  • U krijgt een melding om enkele andere benodigde libraries te installeren.
  • Klik op de knop Alles Installeren om ze allemaal te installeren.
Arduino Nano Adafruit GFX sensor bibliotheek
  • Zoek naar “RTClib” en vind de RTC bibliotheek gemaakt door Adafruit. Deze bibliotheek is compatibel met zowel DS3231 als DS1307.
  • Druk op de knop Installeren om de RTC bibliotheek te installeren.
Arduino Nano RTC bibliotheek

Tijd uitlezen van DS3231 RTC-module en tonen op OLED

Bedradingsschema

Arduino Nano DS3231 OLED bedrading

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - DS3231 en OLED

/* * Deze Arduino Nano code is ontwikkeld door newbiely.nl * Deze Arduino Nano code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-nano/arduino-nano-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_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(); oled_display_center(time); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Snelle Stappen

  • Kopieer de code en open deze in de Arduino IDE.
  • Druk op de knop Uploaden in de Arduino IDE om de code naar de Arduino Nano te sturen.
  • Plaats de sensor in heet en koud water of houd deze in uw hand.
  • Bekijk de resultaten op het OLED-scherm.

Tijd uitlezen van DS1307 RTC-module en tonen op OLED

Bedradingsschema

Arduino Nano DS1307 OLED bedrading

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - DS1307 en OLED

/* * Deze Arduino Nano code is ontwikkeld door newbiely.nl * Deze Arduino Nano code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-nano/arduino-nano-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_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(); oled_display_center(time); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Snelle Stappen

  • Kopieer de code en open deze in de Arduino IDE.
  • Druk op de knop Uploaden in de Arduino IDE om de code naar de Arduino Nano te sturen.
  • Plaats de sensor in heet en koud water, of houd deze in uw hand.
  • 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!