Raspberry Pi - OLED

Diese Anleitung zeigt Ihnen, wie Sie Raspberry Pi mit einem OLED-Display verwenden. Im Detail werden wir lernen:

Benötigte Hardware

1×Raspberry Pi 5
1×SSD1306 I2C OLED-Display 128x64
1×SSD1306 I2C OLED-Display 128x32
1×Jumper Wires (Verbindungskabel)
1×(Empfohlen) Schraubklemmenblock-Shield für Raspberry Pi
1×(Empfohlen) Raspberry Pi Prototyping-Grundplatte & Breadboard-Kit
1×(Empfohlen) HDMI-Touchscreen-Monitor für Raspberry Pi

Oder Sie können die folgenden Kits kaufen:

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 OLED-Displays

Es gibt verschiedene Arten von OLED-Displays verfügbar. Das am häufigsten verwendete OLED mit Raspberry Pi ist das SSD1306 I2C OLED 128x64 und 128x32 Display.

OLED display

I2C OLED Display Pinout

  • GND-Pin: sollte mit der Masse des Raspberry Pi verbunden werden.
  • VCC-Pin: ist die Stromversorgung für das Display, die mit dem 5-Volt-Pin des Raspberry Pi verbunden werden sollte.
  • SCL-Pin: ist ein serieller Takt-Pin für die I2C-Schnittstelle.
  • SDA-Pin: ist ein serieller Daten-Pin für die I2C-Schnittstelle.
OLED pinout

※ Notiz:

  • Die Pins eines OLED-Moduls können je nach Hersteller und Modultyp unterschiedlich angeordnet sein. Es ist UNBEDINGT erforderlich, sich immer auf die auf dem OLED-Modul gedruckten Beschriftungen zu beziehen. Seien Sie aufmerksam!
  • Diese Anleitung verwendet ein OLED-Display, das den SSD1306 I2C-Treiber nutzt. Wir haben es mit dem OLED-Display von DIYables getestet und es funktioniert einwandfrei.

Verdrahtungsdiagramm

  • Verdrahtungsdiagramm zwischen Raspberry Pi und OLED 128x64
Raspberry Pi OLED wiring diagram

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

  • Verdrahtungsdiagramm zwischen Raspberry Pi und OLED 128x32
Raspberry Pi OLED 128x64 wiring diagram

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

Um Ihren Verdrahtungsaufbau zu vereinfachen und zu organisieren, empfehlen wir die Verwendung eines Schraubklemmenblock-Shields für Raspberry Pi. Dieses Shield gewährleistet sicherere und besser verwaltbare Verbindungen, wie unten gezeigt:

Raspberry Pi Schraubklemmenblock-Shield

Die Verdrahtungstabelle zwischen Raspberry Pi und OLED-Display:

OLED Modul Raspberry Pi
Vin 5V
GND GND
SDA GPIO2 (Pin 3)
SCL GPIO3 (Pin 5)

Raspberry Pi Code - Text auf OLED anzeigen

Schnelle Schritte

  • Stellen Sie sicher, dass Sie Raspbian oder ein anderes Raspberry Pi-kompatibles Betriebssystem auf Ihrem Pi installiert haben.
  • Stellen Sie sicher, dass Ihr Raspberry Pi mit demselben lokalen Netzwerk wie Ihr PC verbunden ist.
  • Stellen Sie sicher, dass Ihr Raspberry Pi mit dem Internet verbunden ist, falls Sie einige Bibliotheken installieren müssen.
  • Falls Sie Raspberry Pi zum ersten Mal verwenden, schauen Sie sich die Einrichtung des Raspberry Pi an.
  • Verbinden Sie Ihren PC über SSH mit dem Raspberry Pi, indem Sie den integrierten SSH-Client unter Linux und macOS oder PuTTY unter Windows verwenden. Siehe wie Sie Ihren PC über SSH mit Raspberry Pi verbinden.
  • Stellen Sie sicher, dass Sie die RPi.GPIO-Bibliothek installiert haben. Falls nicht, installieren Sie sie mit dem folgenden Befehl:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install Adafruit-SSD1306
  • Erstellen Sie eine Python-Script-Datei oled.py und fügen Sie den folgenden Code hinzu:
# Dieser Raspberry Pi Code wurde von newbiely.de entwickelt # Dieser Raspberry Pi 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/raspberry-pi/raspberry-pi-oled import time import RPi.GPIO as GPIO import Adafruit_SSD1306 # GPIO pin configuration for the OLED display OLED_RESET_PIN = None # Set to GPIO pin if your display has a reset pin OLED = Adafruit_SSD1306.SSD1306_128_64(rst=OLED_RESET_PIN) # Initialize the OLED display OLED.begin() # Clear the display OLED.clear() OLED.display() try: while True: # Clear the display OLED.clear() # Display "Hello World!" on OLED OLED.draw.text((0, 0), "Hello World!", font=None, fill=255) # Display on OLED OLED.display() # Delay for readability time.sleep(1) except KeyboardInterrupt: print("Program terminated by user.") finally: GPIO.cleanup()
  • Speichern Sie die Datei und führen Sie das Python-Script aus, indem Sie den folgenden Befehl im Terminal ausführen:
python3 oled.py
  • Überprüfen Sie das OLED-Display.

Das Script läuft in einer Endlosschleife kontinuierlich, bis Sie Ctrl + C im Terminal drücken.

Raspberry Pi Code – Bild anzeigen

Um ein Bild auf OLED anzuzeigen, müssen wir es zuerst (unabhängig vom Format) in ein Bitmap-Array konvertieren. Dies kann mit diesem Online-Tool durchgeführt werden. Bitte beziehen Sie sich auf das untenstehende Bild für Anweisungen zur Durchführung. Ich habe bereits das Raspberry Pi-Symbol in ein Bitmap-Array konvertiert.

image to bitmap array

Sobald die Konvertierung abgeschlossen ist, nehmen Sie den Array-Code und ersetzen Sie den vorhandenen ArduinoIcon-Array-Code im folgenden Codeschnipsel.

# Dieser Raspberry Pi Code wurde von newbiely.de entwickelt # Dieser Raspberry Pi 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/raspberry-pi/raspberry-pi-oled import time import Adafruit_SSD1306 from PIL import Image # GPIO pin configuration for the OLED display OLED_RESET_PIN = None # Set to GPIO pin if your display has a reset pin DISP = Adafruit_SSD1306.SSD1306_128_64(rst=OLED_RESET_PIN) # Bitmap of DIYable-icon image bitmap_width = 128 # MUST match the bitmap image size bitmap_height = 57 # MUST match the bitmap image size bitmap_DIYables = [ # 'DIYables Icon', 128x57px 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xfe, 0x00, 0x03, 0xf1, 0xf8, 0x00, 0x3e, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc0, 0x03, 0xf1, 0xf8, 0x00, 0x7e, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf0, 0x03, 0xf0, 0xfc, 0x00, 0x7c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x03, 0xf0, 0xfc, 0x00, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfc, 0x03, 0xf0, 0x7e, 0x00, 0xf8, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x01, 0xfe, 0x03, 0xf0, 0x3f, 0x01, 0xf8, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x7f, 0x03, 0xf0, 0x3f, 0x03, 0xf0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x03, 0xf0, 0x1f, 0x83, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x83, 0xf0, 0x1f, 0x87, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x0f, 0xc7, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x07, 0xef, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x07, 0xff, 0x80, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x03, 0xff, 0x00, 0xfe, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x03, 0xff, 0x00, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x01, 0xfe, 0x00, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x00, 0xfe, 0x00, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xfc, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x7f, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0xff, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x03, 0xfe, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfc, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xe0, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0x80, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xf8, 0x00, 0x03, 0xe0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0x9f, 0xc6, 0x7f, 0x1f, 0xdf, 0xf9, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0x0f, 0x1e, 0x03, 0xe0, 0x3f, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0x07, 0x1c, 0x01, 0xc0, 0x3f, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xfe, 0x00, 0xfd, 0xf1, 0xc3, 0xc7, 0x18, 0xf8, 0x8f, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xfe, 0x00, 0xff, 0xf1, 0xc7, 0xe3, 0x18, 0xf8, 0x8f, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0x01, 0xc7, 0xe3, 0x18, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xfc, 0x01, 0xc7, 0xe3, 0x18, 0x00, 0xe0, 0x3f, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xf8, 0xf1, 0xc7, 0xe3, 0x18, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff, 0xf0, 0x00, 0xf8, 0xf1, 0xc3, 0xc7, 0x18, 0xff, 0xff, 0x1f, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xe0, 0x00, 0xf8, 0x61, 0xc0, 0x07, 0x1c, 0x01, 0xc0, 0x3f, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x00, 0xfc, 0x01, 0xc0, 0x0f, 0x1e, 0x01, 0x80, 0x3f, 0xff, 0xfc, 0x03, 0xff, 0xff, 0xff, 0x80, 0x00, 0xfe, 0x19, 0xc4, 0x3f, 0x1f, 0x87, 0xe0, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ] def draw_bitmap(image, bitmap_data, width, height): image.putdata(bitmap_data) return image try: DISP.begin() DISP.clear() DISP.display() # Create a blank image with mode '1' for 1-bit pixels (bitmap) image = Image.new('1', (bitmap_width, bitmap_height)) while True: DISP.clear() # Display bitmap to the center x = (DISP.width - bitmap_width) // 2 y = (DISP.height - bitmap_height) // 2 # Draw the bitmap on the image image = draw_bitmap(image, bitmap_DIYables, bitmap_width, bitmap_height) # Paste the image on the OLED display DISP.image(image) DISP.display() time.sleep(2) except KeyboardInterrupt: print("Program terminated by user.") finally: DISP.clear() DISP.display()

※ Notiz:

  • Die Größe des Bildes sollte die Größe des Bildschirms nicht überschreiten.
  • Wenn Sie den Code für ein OLED 128x32 verwenden möchten, müssen Sie die Bildgröße ändern und die Breiten- und Höhenparameter in der oled.drawBitmap();-Funktion anpassen.

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