ESP32 - SSD1309 OLED Display 128x64 | 2,42 Zoll I2C OLED Anleitung

Ein OLED (Organic Light-Emitting Diode) Display bietet selbstleuchtende Pixel, die tiefe Schwarztöne, hohen Kontrast und weite Betrachtungswinkel liefern — eine großartige Verbesserung gegenüber herkömmlichen LCDs. Der SSD1309 ist der Treiber-IC, der häufig auf 2,42-Zoll (manchmal als 2,4-Zoll bezeichnet) 128×64 I2C OLED Modulen zu finden ist.

ESP32 SSD1309 2.42-inch OLED

In dieser Schritt-für-Schritt-Anleitung lernen Sie, wie Sie das SSD1309 OLED 128×64 mit einem ESP32 Board unter Verwendung der DIYables_OLED_SSD1309 Bibliothek verbinden und programmieren. Konkret behandeln wir:

Benötigte Hardware

1×ESP32 ESP-WROOM-32 Entwicklungsmodul
1×(Alternativ) ESP32 Uno-form board
1×(Alternativ) ESP32 S3 Uno-form board
1×USB Kabel Type-C
1×SSD1309 I2C OLED Display 128x64 (2,42 Zoll)
1×Breadboard (Steckplatine)
1×Jumper Kabel
1×(Optional) DC-Stromanschluss
1×(Empfohlen) Schraubklemmen-Erweiterungsboard für ESP32
1×(Empfohlen) Breakout Expansion Board for ESP32
1×(Empfohlen) Stromverteiler für ESP32

Oder Sie können die folgenden Kits kaufen:

1×DIYables ESP32 Starter-Kit (ESP32 enthalten)
1×DIYables Sensor-Kit (30 Sensoren/Displays)
1×DIYables Sensor-Kit (18 Sensoren/Displays)
Offenlegung: Einige der in diesem Abschnitt bereitgestellten Links sind Amazon-Affiliate-Links. Wir können eine Provision für Käufe erhalten, die über diese Links getätigt werden, ohne zusätzliche Kosten für Sie. Wir schätzen Ihre Unterstützung.

Über das SSD1309 2,42-Zoll OLED Display

Der SSD1309 ist ein Single-Chip CMOS OLED Treiber-IC, der für 128×64 Dot-Matrix-Panels entwickelt wurde. Er ist register-kompatibel mit dem weit verbreiteten SSD1306, sodass viele bestehende Code-Beispiele mit minimalen Änderungen übertragen werden können. Die wichtigsten Hardware-Unterschiede sind:

  • Keine eingebaute Charge Pump — der SSD1309 benötigt eine externe VCC-Schiene, obwohl praktisch alle Breakout-Boards (einschließlich 2,42-Zoll und 2,4-Zoll Module) mit einem integrierten Boost-Converter geliefert werden, sodass dies für Sie transparent ist.
  • Höhere Spannungstoleranz — der SSD1309 akzeptiert bis zu 16 V VCC, während der SSD1306 auf etwa 4,2 V begrenzt ist.

Das 2,42-Zoll (2,4-Zoll) OLED Modul verwendet häufig den SSD1309 Treiber und verfügt über eine 128×64 Pixel Auflösung mit I2C Schnittstelle. Die Panel-Farbe (weiß, blau, gelb, grün oder zweizonal) wird durch das physikalische OLED Material bestimmt und ist nicht softwaresteuerbar.

Diese Anleitung kommuniziert mit dem Display über den I2C Bus, der nur zwei Signalleitungen (SDA und SCL) benötigt und den Bus mit anderen I2C Peripheriegeräten teilen kann.

SSD1309 OLED Pinout (I2C Modul)

Das typische 2,42-Zoll SSD1309 I2C OLED Modul hat vier Pins:

  • GND Pin: sollte mit dem Ground des ESP32 verbunden werden
  • VCC Pin: sollte mit dem 3,3V oder 5V Pin des ESP32 verbunden werden (prüfen Sie die Spezifikationen Ihres Moduls)
  • SCL Pin: ist ein I2C Clock-Pin. Verbinden Sie mit GPIO 22 (Standard SCL)
  • SDA Pin: ist ein I2C Daten-Pin. Verbinden Sie mit GPIO 21 (Standard SDA)
SSD1309 OLED Pinout

※ Notiz:

Die Pin-Reihenfolge kann zwischen Herstellern variieren. Überprüfen Sie immer die Beschriftungen auf Ihrem OLED Modul.

Schaltplan

ESP32 SSD1309 OLED 128x64 Schaltplan

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

Wenn Sie nicht wissen, wie Sie ESP32 und andere Komponenten mit Strom versorgen, finden Sie Anleitungen im folgenden Tutorial: Wie man ESP32 mit Strom versorgt.

ESP32 und SSD1309 OLED Verbindung

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

ESP32 für SSD1309 OLED programmieren

Installation der DIYables_OLED_SSD1309 Bibliothek

Die DIYables_OLED_SSD1309 Bibliothek ist speziell für SSD1309 Displays angepasst und erweitert Adafruit_GFX für erweiterte Grafikunterstützung.

  • Öffnen Sie die Arduino IDE
  • Navigieren Sie zum Libraries Symbol in der linken Seitenleiste
  • Suchen Sie nach DIYables_OLED_SSD1309
  • Klicken Sie auf Install
  • Eine Abhängigkeitsabfrage erscheint — klicken Sie auf Install All, um auch die Adafruit GFX Library zu installieren (erforderlich für Formzeichnungen und Schriftarten)
ESP32 SSD1309 OLED Bibliothek

Programmierschritte

  1. Erforderliche Bibliotheken einbinden
#include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h>
  1. Bildschirmabmessungen definieren
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64
  1. Display-Objekt deklarieren
#define OLED_RESET -1 // Reset Pin wird bei den meisten I2C Modulen nicht verwendet #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  1. OLED in setup() initialisieren
if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); // Halt }
  1. Inhalt anzeigen
display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("Hello ESP32")); display.display(); // Buffer zum Bildschirm übertragen

ESP32 Code — Hello World auf SSD1309 OLED

Der einfachste Einstieg: ein paar Textzeilen in verschiedenen Größen ausgeben.

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Hello World * Prints text in two sizes on the 2.42 inch 128x64 I2C OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); // 6x8 pixels per character display.setTextColor(SSD1309_PIXEL_ON); // turn pixels on display.setCursor(0, 0); display.println(F("Hello, World!")); display.println(); display.setTextSize(2); // 12x16 pixels per character display.println(F("DIYables")); display.setTextSize(1); display.println(); display.println(F("SSD1309 OLED 128x64")); display.display(); // push buffer to screen } void loop() { }

ESP32 Code — Text auf SSD1309 OLED anzeigen

Das folgende Beispiel demonstriert weitere Text-Features — verschiedene Größen, Zahlenformatierung und das F() Makro zur RAM-Einsparung.

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Text * Displays text in various sizes and formats on the 2.42" SSD1309 OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); // Various text sizes display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Size 1 - DIYables")); display.setTextSize(2); display.println(F("Size 2")); display.setTextSize(3); display.println(F("Sz3")); display.display(); delay(3000); // Number formatting display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 0); int value = 42; float pi = 3.14159; display.print(F("Integer: ")); display.println(value); display.print(F("Float: ")); display.println(pi, 2); // 2 decimal places display.print(F("Hex: 0x")); display.println(value, HEX); display.print(F("Binary: 0b")); display.println(value, BIN); display.display(); } void loop() { }

Nützliche Display-Funktionen Referenz

Hier ist eine Schnellreferenz der am häufigsten verwendeten Funktionen bei der Arbeit mit dem SSD1309 OLED über die DIYables Bibliothek:

  • oled.clearDisplay() — Frame-Buffer löschen (alle Pixel aus).
  • oled.display() — Buffer zum OLED übertragen, damit Änderungen sichtbar werden.
  • oled.drawPixel(x, y, color) — einzelnen Pixel setzen oder löschen.
  • oled.setTextSize(n) — Schriftart um Faktor *n* skalieren (1 = 6×8, 2 = 12×16, …, bis zu 8).
  • oled.setCursor(x, y) — Text-Cursor zu Pixel-Koordinaten *(x, y)* bewegen.
  • oled.setTextColor(SSD1309_PIXEL_ON) — nur Text-Vordergrund (Hintergrund ist transparent).
  • oled.setTextColor(SSD1309_PIXEL_OFF, SSD1309_PIXEL_ON) — Text mit expliziter Hintergrundfarbe.
  • oled.println("message") — String ausgeben und zur nächsten Zeile wechseln.
  • oled.println(number) — Integer in Dezimaldarstellung ausgeben.
  • oled.println(number, HEX) — Integer in Hexadezimaldarstellung ausgeben.
  • oled.startscrollright(start, stop) — Hardware-Scroll nach rechts zwischen Seite *start* und Seite *stop*.
  • oled.startscrollleft(start, stop) — Hardware-Scroll nach links.
  • oled.startscrolldiagright(start, stop) — Hardware-Scroll diagonal nach rechts.
  • oled.startscrolldiagleft(start, stop) — Hardware-Scroll diagonal nach links.
  • oled.stopscroll() — aktives Hardware-Scrolling stoppen.
  • oled.setContrast(value) — Display-Helligkeit anpassen (0–255).
  • oled.dim(true/false) — Display schnell auf Minimum dimmen oder vorherigen Kontrast wiederherstellen.
  • oled.invertDisplay(true/false) — Hardware-Farbinvertierung (an-Pixel ↔ aus-Pixel).

Text vertikal und horizontal auf dem SSD1309 OLED zentrieren

Siehe Wie man Text vertikal/horizontal auf OLED zentriert

ESP32 Code — Formen auf SSD1309 OLED zeichnen

Da die DIYables_OLED_SSD1309 Bibliothek Adafruit_GFX erweitert, erhalten Sie einen vollständigen Satz von Form-Zeichnungsprimitive: Pixel, Linien, Rechtecke, gefüllte Rechtecke, Kreise, gefüllte Kreise, Dreiecke, gefüllte Dreiecke und abgerundete Rechtecke. Das folgende Sketch durchläuft alle mit animierten Demos.

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Draw Shapes * Demonstrates drawing pixels, lines, rectangles, circles, and triangles on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Draw pixels display.clearDisplay(); for (int i = 0; i < 20; i++) { display.drawPixel(random(SCREEN_WIDTH), random(SCREEN_HEIGHT), SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw lines display.clearDisplay(); for (int y = 0; y < SCREEN_HEIGHT; y += 8) { display.drawLine(0, 0, SCREEN_WIDTH - 1, y, SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw rectangles display.clearDisplay(); display.drawRect(10, 10, 40, 30, SSD1309_PIXEL_ON); // outline display.fillRect(60, 10, 40, 30, SSD1309_PIXEL_ON); // filled display.drawRoundRect(10, 45, 40, 15, 5, SSD1309_PIXEL_ON); // rounded corners display.display(); delay(2000); // Draw circles display.clearDisplay(); display.drawCircle(32, 32, 20, SSD1309_PIXEL_ON); // outline display.fillCircle(96, 32, 20, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); // Draw triangles display.clearDisplay(); display.drawTriangle(20, 10, 10, 50, 30, 50, SSD1309_PIXEL_ON); // outline display.fillTriangle(90, 10, 70, 50, 110, 50, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); }

ESP32 Code — Hardware-Scrolling auf SSD1309 OLED

Der SSD1309 hat eine eingebaute Scrolling-Engine, die den Display-Inhalt ohne CPU-Last verschiebt. Die DIYables Bibliothek stellt vier Scroll-Richtungen zur Verfügung: rechts, links, diagonal-rechts und diagonal-links. Jede nimmt einen Start-Seiten- und Stop-Seiten-Parameter (Seiten sind 8-Pixel-hohe horizontale Streifen, nummeriert 0–7 auf einem 64-Pixel-hohen Display).

※ Notiz:

Rufen Sie immer display() auf, um Ihren Inhalt zum OLED zu übertragen bevor Sie ein Scrolling starten. Vermeiden Sie das Zeichnen neuer Inhalte während aktives Scrolling läuft — rufen Sie zuerst stopscroll() auf.

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Scroll Text * Demonstrates all four hardware scroll directions on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(10, 24); display.println(F("DIYables")); display.display(); delay(2000); } void loop() { // Scroll right across all pages display.startscrollright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Scroll left display.startscrollleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll right display.startscrolldiagright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll left display.startscrolldiagleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); }

ESP32 Code — Bitmap-Bild auf SSD1309 OLED anzeigen

Um ein Bitmap auf dem SSD1309 OLED anzuzeigen, müssen Sie Ihr Bild zuerst in ein C Byte-Array konvertieren. Verwenden Sie das kostenlose image2cpp Online-Tool für diese Konvertierung:

  1. Laden Sie Ihre Bilddatei hoch (PNG, JPG, BMP, etc.).
  2. Setzen Sie die Canvas-Größe auf 128×64 (oder kleiner).
  3. Wählen Sie Arduino code als Ausgabeformat.
  4. Kopieren Sie das generierte Array in Ihr Sketch.
Bild zu Bitmap Array

Das folgende Beispiel wechselt zwischen einem 16×16 Herz-Icon und einem vollbreiten DIYables Logo:

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Bitmap Image * Shows a 16x16 heart icon and 128x64 DIYables logo on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // 16x16 heart icon bitmap const unsigned char heart_icon[] PROGMEM = { 0x00, 0x00, 0x0c, 0x30, 0x1e, 0x78, 0x3f, 0xfc, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x00, 0x00 }; // 128x64 DIYables logo bitmap const unsigned char diyables_logo[] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xc1, 0xff, 0xe0, 0x7f, 0xf0, 0x1f, 0xfc, 0x03, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xff, 0xf7, 0xff, 0xf9, 0xff, 0xfc, 0x7f, 0xff, 0x0f, 0x80, 0x3f, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xfe, 0xff, 0xff, 0x9f, 0xc0, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0x80, 0xff, 0x00, 0x7f, 0xc0, 0x3f, 0xf0, 0x0f, 0xfc, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x1f, 0xe0, 0x07, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x01, 0xfc, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x03, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x07, 0xff, 0xff, 0xc0, 0x07, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x0f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x03, 0xff, 0xff, 0x80, 0x0f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x01, 0xff, 0xff, 0x80, 0x1f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x01, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x7f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x7f, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x00, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x00, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xfc, 0x01, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf8, 0x01, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf8, 0x03, 0xff, 0xf8, 0x00, 0x3e, 0x00, 0x1f, 0x00, 0x0f, 0xc0, 0x03, 0xf0, 0x00, 0x00, 0xff, 0xf0, 0x03, 0xff, 0xfc, 0x00, 0x7e, 0x00, 0x1f, 0x80, 0x1f, 0xe0, 0x07, 0xf8, 0x00, 0x00, 0xff, 0xf0, 0x07, 0xff, 0x7f, 0x80, 0xff, 0x00, 0x7f, 0xc0, 0x3f, 0xf0, 0x0f, 0xfc, 0x00, 0x00, 0xff, 0xf0, 0x07, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xfe, 0xff, 0xff, 0x9f, 0xc0, 0x00, 0xff, 0xe0, 0x0f, 0xfc, 0x1f, 0xff, 0xf7, 0xff, 0xf9, 0xff, 0xfc, 0x7f, 0xff, 0x0f, 0x80, 0x00, 0xff, 0xe0, 0x0f, 0xf8, 0x07, 0xff, 0xc1, 0xff, 0xe0, 0x7f, 0xf0, 0x1f, 0xfc, 0x03, 0x00, 0x00, 0xff, 0xe0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xfe, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xc0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x3e, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7c, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Display small heart icon centered display.clearDisplay(); display.drawBitmap((SCREEN_WIDTH - 16) / 2, (SCREEN_HEIGHT - 16) / 2, heart_icon, 16, 16, SSD1309_PIXEL_ON); display.display(); delay(3000); // Display full-screen DIYables logo display.clearDisplay(); display.drawBitmap(0, 0, diyables_logo, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1309_PIXEL_ON); display.display(); delay(3000); // Invert display display.invertDisplay(true); delay(2000); display.invertDisplay(false); delay(1000); }

※ Notiz:

  • Die Bitmap-Abmessungen dürfen die Bildschirmauflösung nicht überschreiten (128×64 für das 2,42-Zoll Modul).

ESP32 Code — Kontrast und Dimmen auf SSD1309 OLED

Der SSD1309 unterstützt 256 Kontraststufen (0–255). Die DIYables Bibliothek bietet setContrast() für feine Steuerung und dim() für schnelles Umschalten zwischen minimaler Helligkeit und dem zuvor konfigurierten Level.

/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Contrast & Dim * Sweeps contrast from 0→255→0 then toggles dim mode on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } // Draw a test pattern so the contrast changes are visible display.clearDisplay(); display.fillRect(0, 0, 64, 32, SSD1309_PIXEL_ON); display.fillRect(64, 32, 64, 32, SSD1309_PIXEL_ON); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_INVERSE); display.setCursor(16, 28); display.println(F("Contrast Demo")); display.display(); delay(2000); } void loop() { // Ramp up for (int c = 0; c <= 255; c += 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Ramp down for (int c = 255; c >= 0; c -= 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Quick dim toggle display.dim(true); // minimum brightness delay(2000); display.dim(false); // restore delay(2000); }

ESP32 Code — Benutzerdefinierte externe Schriftarten auf SSD1309 OLED

Die Adafruit GFX Bibliothek wird mit Dutzenden skalierbarer FreeFont Schriftarten geliefert (Serif, Sans, Mono — jeweils in Regular, Bold, Italic und vier Größen). Sie können jede davon auf dem SSD1309 Display aktivieren, indem Sie den entsprechenden Header einbinden und setFont() aufrufen.

※ Notiz:

  • Wenn eine externe Schriftart aktiv ist, bezieht sich die Cursor Y-Koordinate auf die Text-Grundlinie, nicht auf die obere linke Ecke. Dies unterscheidet sich von der eingebauten 5×7 Schriftart.
  • Externe Schriftarten werden im Flash (PROGMEM) gespeichert. Der ESP32 hat ausreichend Flash-Speicher, sodass Sie gerne mehrere Schriftdateien in Ihrem Projekt verwenden können.
/* * Dieser ESP32 Code wurde von newbiely.de entwickelt * Dieser ESP32 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/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – External Fonts * Cycles through three FreeFont typefaces on the 2.42" SSD1309 OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #include <Fonts/FreeSerif9pt7b.h> #include <Fonts/FreeSansBold12pt7b.h> #include <Fonts/FreeMono9pt7b.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // ── Built-in 5×7 font ── display.clearDisplay(); display.setFont(NULL); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Built-in 5x7 font")); display.println(); display.setTextSize(2); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeSerif 9pt ── display.clearDisplay(); display.setFont(&FreeSerif9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); // Y = baseline display.println(F("FreeSerif 9pt")); display.setCursor(0, 38); display.println(F("DIYables OLED")); display.setCursor(0, 58); display.println(F("Hello World!")); display.display(); delay(3000); // ── FreeSansBold 12pt ── display.clearDisplay(); display.setFont(&FreeSansBold12pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("SansBold")); display.setCursor(0, 52); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeMono 9pt ── display.clearDisplay(); display.setFont(&FreeMono9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); display.println(F("FreeMono 9pt")); display.setCursor(0, 34); display.println(F("0123456789")); display.setCursor(0, 54); display.println(F("!@#$%^&*()")); display.display(); delay(3000); }

SSD1309 OLED Fehlerbehebung mit ESP32

Wenn nach dem Hochladen Ihres Sketches nichts auf dem 2,42-Zoll SSD1309 OLED erscheint, arbeiten Sie diese Prüfungen durch:

  • Verkabelung überprüfen — bestätigen Sie, dass SDA, SCL, VCC und GND mit den korrekten ESP32 Pins verbunden sind.
  • Treiber-Chip bestätigen — diese Bibliothek ist für den SSD1309 entwickelt. Wenn Ihr Modul einen anderen Controller verwendet (z.B. SH1106), wird es nicht korrekt antworten.
  • I2C Adresse prüfen — die meisten SSD1309 Module verwenden standardmäßig 0x3C, aber einige verwenden 0x3D. Führen Sie das folgende I2C Scanner Sketch aus, um die tatsächliche Adresse zu erkennen:
// I2C Adress-Scanner — gibt jedes erkannte Gerät auf dem Serial Monitor aus #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices = 0; Serial.println("Scanning..."); 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); }

Erwartete Serial Monitor Ausgabe, wenn der SSD1309 erkannt wird:

COM6
Send
Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Sicherstellen, dass display() aufgerufen wird — der SSD1309 verwendet einen Frame-Buffer. Zeichnungsfunktionen modifizieren nur den Buffer im RAM; nichts erscheint auf dem Bildschirm, bis Sie oled.display() aufrufen.
  • Stromversorgung prüfen — das 2,42-Zoll Modul verbraucht mehr Strom als kleinere OLEDs. Stellen Sie sicher, dass Ihre Stromquelle ausreichend Strom liefern kann (typischerweise 20–40 mA bei voller Helligkeit). Bei USB-Stromversorgung ist dies normalerweise kein Problem mit ESP32.
  • Benutzerdefinierte I2C Pins (optional) — Wenn Sie nicht-Standard I2C Pins verwenden müssen, initialisieren Sie Wire mit Wire.begin(SDA_PIN, SCL_PIN) bevor Sie display.begin() aufrufen.

Zusammenfassung

Diese Anleitung hat alle wesentlichen Punkte für die Verwendung des 2,42-Zoll SSD1309 OLED Displays (128×64) mit ESP32 abgedeckt, einschließlich:

  • Hardware-Verbindungen und I2C Verkabelung
  • Text- und Zahlenanzeige mit verschiedenen Größen
  • Form-Zeichnungen (Rechtecke, Kreise, Dreiecke)
  • Bitmap-Bild-Darstellung
  • Hardware-Scrolling in vier Richtungen
  • Kontrast- und Helligkeitssteuerung
  • Benutzerdefinierte externe Schriftarten

Die DIYables_OLED_SSD1309 Bibliothek vereinfacht die SSD1309 Programmierung durch Bereitstellung von High-Level-Funktionen für Text, Grafiken und Display-Steuerung, während sie Adafruit GFX für erweiterte Zeichnungsmöglichkeiten nutzt.

Für weitere ESP32 Projekte und Anleitungen besuchen Sie esp32io.com

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