Arduino UNO R4 – OLED 128×32

Dieses Tutorial zeigt dir, wie du einen Arduino UNO R4 mit einem OLED-Display 128×32 I2C verwendest. Du wirst lernen:

Arduino UNO R4 OLED-I2C-Display

Über OLED-Display

I2C-OLED-Display-Pinbelegung

  • GND-Pin: sollte mit dem Masseanschluss des Arduino UNO R4 verbunden werden
  • VCC-Pin: ist die Stromversorgung für das Display, an die wir den 5-Volt-Anschluss des Arduino UNO R4 anschließen
  • SCL-Pin: ist ein serieller Taktpin für die I2C-Schnittstelle
  • SDA-Pin: ist ein serieller Datenpin für die I2C-Schnittstelle
OLED-Pinbelegung

※ Notiz:

Die Anordnung der Pins eines OLED-Moduls kann je nach Hersteller und Modell des Moduls variieren. Überprüfen Sie stets die Beschriftungen am OLED-Modul und befolgen Sie sie. Seien Sie aufmerksam!

Diese Anleitung gilt für ein OLED-Display, das den SSD1306-I2C-Treiber verwendet. Wir haben es mit einem OLED-Display von DIYables getestet. Es funktioniert einwandfrei, ohne Probleme.

Verdrahtungsdiagramm

Arduino UNO R4 OLED 128x32 Verdrahtungsdiagramm

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

Siehe Der beste Weg, den Arduino Uno R4 und andere Komponenten mit Strom zu versorgen.

Wenn Sie einen anderen Typ des Arduino UNO R4 verwenden, stimmen die Pinbelegungen nicht mit dem Uno überein. Schauen Sie sich die Tabelle unten an, um Informationen zu anderen Arduino UNO R4-Modellen zu erhalten.

128x32 OLED Module Arduino UNO R4
Vin 5V
GND GND
SDA A4
SCL A5

So verwenden Sie OLED mit dem Arduino UNO R4

SSD1306 OLED-Bibliothek installieren

  • Gehen Sie zum Bibliotheken-Symbol auf der linken Seite der Arduino IDE.
  • Geben Sie "SSD1306" in das Suchfeld ein und suchen Sie nach der SSD1306-Bibliothek von Adafruit.
  • Drücken Sie die Install-Schaltfläche, um die Bibliothek hinzuzufügen.
Arduino UNO R4 OLED-Bibliothek
  • Sie müssen einige zusätzliche Bibliotheken installieren.
  • Klicken Sie auf die Schaltfläche Alle installieren, um alle erforderlichen Bibliotheken zu installieren.
Arduino UNO R4 Adafruit GFX-Sensorbibliothek

Wie man für OLED programmiert

  • Eine Bibliothek einbinden.
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
  • Stellen Sie die Bildschirmgröße auf OLED 123×32 ein.
#define SCREEN_WIDTH 128 // Definiert die Breite des OLED-Displays in Pixeln #define SCREEN_HEIGHT 32 // Definiert die Höhe des OLED-Displays in Pixeln
  • Erstellen Sie ein SSD1306-OLED-Element.
// Initialisiere ein Adafruit SSD1306-Display-Objekt für die I2C-Kommunikation Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • In der setup()-Funktion richten Sie das OLED-Display ein.
// Starte OLED-Display mit spezifischer I2C-Adresse (0x3C) für 128×32 Auflösung if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); // Fehlermeldung ausgeben, falls die Initialisierung fehlschlägt while (true); // Endlosschleife, um die weitere Ausführung zu stoppen }
  • Dann kannst du Text, Bilder anzeigen und Linien zeichnen.

Arduino UNO R4-Code – Text auf dem OLED-Display anzeigen

/* * Dieser Arduino UNO R4 Code wurde von newbiely.de entwickelt * Dieser Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 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 oled.println("Hello World!"); // text to display oled.display(); // show on OLED } void loop() { }

Hier sind einige Funktionen, die Sie verwenden können, um Text auf dem OLED anzuzeigen:

  • oled.clearDisplay(): schaltet alle Pixel aus.
  • oled.drawPixel(x, y, color): zeichnet ein Pixel an den Koordinaten x, y.
  • oled.setTextSize(n): ändert die Textgröße, von 1 bis 8 auswählbar.
  • oled.setCursor(x, y): setzt den Startpunkt für Text fest.
  • oled.setTextColor(WHITE): setzt die Textfarbe auf Weiß.
  • oled.setTextColor(BLACK, WHITE): setzt die Textfarbe schwarz und den Hintergrund weiß.
  • oled.println("message"): zeigt Text an.
  • oled.println(number): zeigt eine Zahl an.
  • oled.println(number, HEX): zeigt eine Zahl im hexadezimalen Format an.
  • oled.display(): aktualisiert das Display mit den Änderungen.
  • oled.startscrollright(start, stop): verschiebt Text von links nach rechts.
  • oled.startscrollleft(start, stop): verschiebt Text von rechts nach links.
  • oled.startscrolldiagright(start, stop): verschiebt Text diagonal von unten links nach oben rechts.
  • oled.startscrolldiagleft(start, stop): verschiebt Text diagonal von unten rechts nach oben links.
  • oled.stopscroll(): stoppt das Scrollen von Text.

Arduino UNO R4 Code - Zeichnen auf dem OLED-Display

/* * Dieser Arduino UNO R4 Code wurde von newbiely.de entwickelt * Dieser Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.setCursor(0, 0); } void loop() { // draw rectangle oled.clearDisplay(); oled.drawRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // fill rectangle oled.clearDisplay(); oled.fillRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // draw the round rectangle oled.clearDisplay(); oled.drawRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // fill the round rectangle oled.clearDisplay(); oled.fillRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // draw circle oled.clearDisplay(); oled.drawCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // fill circle oled.clearDisplay(); oled.fillCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // draw triangle oled.clearDisplay(); oled.drawTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); // fill triangle oled.clearDisplay(); oled.fillTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); }

Arduino UNO R4 Code – Bild anzeigen

Um ein Bild auf einem OLED-Bildschirm anzuzeigen, wandeln Sie zuerst das Bild (beliebiges Format) in ein Bitmap-Array um. Sie können dieses Online-Tool verwenden, um es zu konvertieren. Sehen Sie sich das unten gezeigte Bild an, um zu sehen, wie man ein Bild in ein Bitmap-Array umwandelt. Ich habe das Arduino-Symbol in ein Bitmap-Array umgewandelt.

Bild in Bitmap-Array

Kopiere den neuen Array-Code und aktualisiere ihn im Arduino-Icon-Array im untenstehenden Code.

Das untenstehende Video zeigt, wie man es mit einem OLED-Display 128x64 und Arduino Uno sowie Arduino-Icon macht.

Wir können es ähnlich machen, um es mit dem Arduino Uno R4 und dem OLED 128x32 zum Laufen zu bringen. Der unten stehende Code zeigt das DIYables-Symbol auf dem OLED 128x32 an.

/* * Dieser Arduino UNO R4 Code wurde von newbiely.de entwickelt * Dieser Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // bitmap of DIYable-icon image int bitmap_width = 72; // MUST match to bitmap image size int bitmap_height = 32; // MUST match to bitmap image size const unsigned char bitmap_DIYables [] PROGMEM = { // 'DIYables Icon', 72x32 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xf8, 0x07, 0x38, 0x07, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xfe, 0x07, 0x1c, 0x0e, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x1c, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x07, 0x87, 0x0e, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x0f, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x07, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xf0, 0xf0, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xe0, 0xfc, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x01, 0xe0, 0xfe, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x03, 0x87, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x0f, 0x87, 0x01, 0xc0, 0xff, 0xcf, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x8f, 0xfc, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0x0e, 0x0c, 0x0c, 0xc3, 0x07, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0xec, 0xec, 0x99, 0x7f, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0x04, 0xe4, 0x81, 0x0f, 0xff, 0xcf, 0xff, 0xfc, 0x0e, 0x32, 0xe4, 0x9f, 0xc7, 0xff, 0x8f, 0xff, 0xf8, 0x0e, 0x32, 0x4c, 0x9b, 0x67, 0xff, 0x0f, 0xff, 0xf0, 0x0e, 0x04, 0x0c, 0xc3, 0x0f, 0xfe, 0x0f, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xfc, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing } void loop() { oled.clearDisplay(); // display bitmap to the center int x = (SCREEN_WIDTH - bitmap_width) / 2; int y = (SCREEN_HEIGHT - bitmap_height) / 2; oled.drawBitmap(x, y, bitmap_DIYables, bitmap_width, bitmap_height, WHITE); oled.display(); delay(2000); }

※ Notiz:

  • Die Bildgröße darf die Bildschirmgröße nicht überschreiten.
  • Um den gegebenen Code für ein OLED 128x32 zu verwenden, müssen Sie das Bild skalieren und die Breite und Höhe in der Funktion oled.drawBitmap(); anpassen.

Wie man Text/Zahl horizontal und vertikal auf einem OLED-Display zentriert

/* * Dieser Arduino UNO R4 Code wurde von newbiely.de entwickelt * Dieser Arduino UNO R4 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-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 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(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display } void loop() { // display string String text = "DIYables"; oledDisplayCenter(text); delay(2000); // display number int number = 21; String str = String(number); oledDisplayCenter(str); delay(2000); } 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(); }

OLED-Fehlerbehebung

Wenn der OLED-Bildschirm nichts anzeigt, befolgen Sie bitte diese Schritte:

  • Stellen Sie sicher, dass die Verkabelung ordnungsgemäß erfolgt.
  • Bestätigen Sie, dass Ihr I2C-OLED mit einem SSD1306-Treiber ausgestattet ist.
  • Überprüfen Sie die I2C-Adresse Ihres OLED mit dem folgenden I2C-Adressscanner-Code auf dem Arduino UNO R4.
// I2C address scanner program #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found"); else Serial.println("done"); delay(5000); // wait 5 seconds for next scan }

Die Ausgabe am seriellen Monitor:

COM6
Send
Scanning... I2C device found at address 0x3C ! done Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 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!