Bemerkung

Hallo und willkommen in der SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasten-Gemeinschaft auf Facebook! Tauchen Sie tiefer ein in die Welt von Raspberry Pi, Arduino und ESP32 mit anderen Enthusiasten.

Warum beitreten?

  • Expertenunterstützung: Lösen Sie Nachverkaufsprobleme und technische Herausforderungen mit Hilfe unserer Gemeinschaft und unseres Teams.

  • Lernen & Teilen: Tauschen Sie Tipps und Anleitungen aus, um Ihre Fähigkeiten zu verbessern.

  • Exklusive Vorschauen: Erhalten Sie frühzeitigen Zugang zu neuen Produktankündigungen und exklusiven Einblicken.

  • Spezialrabatte: Genießen Sie exklusive Rabatte auf unsere neuesten Produkte.

  • Festliche Aktionen und Gewinnspiele: Nehmen Sie an Gewinnspielen und Feiertagsaktionen teil.

👉 Sind Sie bereit, mit uns zu erkunden und zu erschaffen? Klicken Sie auf [ hier ] und treten Sie heute bei!

OLED

Überblick

In dieser Lektion erfahren Sie mehr über OLED-Displays, die den SSD1306-Treiber verwenden. OLED-Displays (Organische Leuchtdioden) werden in verschiedenen elektronischen Geräten wie Smartwatches, Mobiltelefonen und sogar Fernsehern verwendet. Der SSD1306 ist ein Ein-Chip-CMOS-OLED/PLED-Treiber mit Controller für organische/polymerbasierte Leuchtdioden-Dot-Matrix-Grafikdisplaysysteme. Er bietet eine klare und deutliche visuelle Ausgabe durch organische, lichtemittierende Dioden, die bei Durchgang eines elektrischen Stroms leuchten.

Im bereitgestellten Code wird ein OLED-Display über das I2C-Protokoll mit einem Arduino-Board verbunden. Der Code verwendet die Adafruit SSD1306-Bibliothek zur Steuerung des Displays. Das Programm umfasst verschiedene Funktionalitäten wie:

  1. Textanzeige: „Hello world!“ wird auf dem Bildschirm angezeigt.

  2. Invertierter Text: Der Text „Hello world!“ wird in einem invertierten Farbschema dargestellt.

  3. Schriftgröße: Der Text „Hello!“ wird mit einer vergrößerten Schriftgröße angezeigt.

  4. Numerische Anzeige: Die Zahlen 123456789 werden angezeigt.

  5. ASCII-Zeichen: Eine Reihe von ASCII-Zeichen wird angezeigt.

  6. Scrollen: Text wird horizontal über das Display gescrollt.

  7. Bitmap-Anzeige: Ein vordefiniertes Bitmap-Bild wird auf dem OLED-Bildschirm angezeigt.

Dieses OLED-Display kann in einer Vielzahl von Anwendungen eingesetzt werden, einschließlich digitaler Uhren, Mini-Spielkonsolen, Informationsanzeigen und so weiter. Es bietet eine großartige Möglichkeit, eine Benutzeroberfläche in kompakten und tragbaren Geräten bereitzustellen.

Benötigte Komponenten

Für dieses Projekt benötigen wir die folgenden Komponenten.

Es ist definitiv praktisch, ein komplettes Kit zu kaufen, hier ist der Link:

Name

ARTIKEL IN DIESEM KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

Sie können die Komponenten auch separat über die untenstehenden Links kaufen.

KOMPONENTENBESCHREIBUNG

KAUF-LINK

Arduino Uno R4 WiFi

-

Jumperkabel

KAUFEN

OLED-Display-Modul

KAUFEN

Verdrahtung

../_images/15-oled_bb.png

Schaltplan

../_images/15_oled_schematic.png

Code

Bemerkung

  • Sie können die Datei 15-oled.ino direkt unter dem Pfad elite-explorer-kit-main\basic_project\15-oled öffnen.

  • Oder kopieren Sie diesen Code in die Arduino IDE.

Bemerkung

Um die Bibliothek zu installieren, verwenden Sie den Arduino Library Manager und suchen Sie nach „Adafruit SSD1306“ und „Adafruit GFX“ und installieren Sie diese.

15-oled.ino
  1/*
  2  This code initializes an OLED display (SSD1306) using the Adafruit SSD1306 library, 
  3  and displays various text, numbers, and scroll animations on the screen.
  4
  5  Board: Arduino Uno R4 (or R3)
  6  Component:  OLED (SSD1306)
  7  Library: https://github.com/adafruit/Adafruit_SSD1306 (Adafruit SSD1306 by Adafruit)  
  8           https://github.com/adafruit/Adafruit-GFX-Library (Adafruit GFX Library by Adafruit) 
  9*/
 10
 11#include <SPI.h>
 12#include <Wire.h>
 13#include <Adafruit_GFX.h>
 14#include <Adafruit_SSD1306.h>
 15
 16#define SCREEN_WIDTH 128  // OLED display width, in pixels
 17#define SCREEN_HEIGHT 64  // OLED display height, in pixels
 18
 19// Declaration for SSD1306 display connected using I2C
 20#define OLED_RESET -1  // Reset pin # (or -1 if sharing Arduino reset pin)
 21#define SCREEN_ADDRESS 0x3C
 22Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 23
 24//Bitmap data
 25static const unsigned char PROGMEM sunfounderIcon[] = {
 26	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xff, 
 27	0xff, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 
 28	0xff, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0xff, 
 29	0xff, 0xff, 0xff, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xff, 0xff, 
 30	0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x1f, 0xff, 0xff, 
 31	0xff, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x01, 0x00, 0x07, 0xff, 0xff, 
 32	0xff, 0xff, 0xc0, 0x07, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xff, 0x80, 0x0f, 0xe0, 0x00, 0xff, 0xff, 
 33	0xff, 0xff, 0x00, 0x1f, 0xf0, 0x00, 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xf8, 0x00, 0x3f, 0xff, 
 34	0xff, 0xfc, 0x00, 0x7f, 0xfe, 0x00, 0x0f, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0xff, 0x00, 0x07, 0xff, 
 35	0xff, 0xf8, 0x01, 0xfc, 0x7f, 0x80, 0x03, 0xff, 0xff, 0xf0, 0x03, 0xf8, 0x3f, 0xc0, 0x01, 0xff, 
 36	0xff, 0xe0, 0x07, 0xf0, 0x0f, 0xe0, 0x00, 0xff, 0xff, 0xc0, 0x0f, 0xe0, 0x07, 0xf8, 0x00, 0x3f, 
 37	0xff, 0x80, 0x0f, 0xc0, 0x03, 0xfc, 0x00, 0x7f, 0xff, 0x00, 0x1f, 0xc0, 0x01, 0xfe, 0x00, 0xff, 
 38	0xff, 0x00, 0x0f, 0xe0, 0x00, 0x7f, 0x81, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0x00, 0x3f, 0xc3, 0xff, 
 39	0xff, 0x80, 0x03, 0xfc, 0x00, 0x1f, 0xe7, 0xff, 0xff, 0xc0, 0x01, 0xfe, 0x00, 0x0f, 0xff, 0xff, 
 40	0xff, 0xe0, 0x00, 0xff, 0x00, 0x03, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x7f, 0x80, 0x01, 0xff, 0xff, 
 41	0xff, 0xf8, 0x00, 0x1f, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x0f, 0xf0, 0x00, 0x7f, 0xff, 
 42	0xff, 0xff, 0x00, 0x07, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x80, 0x03, 0xfc, 0x00, 0x0f, 0xff, 
 43	0xff, 0xff, 0xc0, 0x00, 0xff, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0x80, 0x03, 0xff, 
 44	0xff, 0xff, 0xf8, 0x00, 0x3f, 0xc0, 0x01, 0xff, 0xff, 0xf3, 0xfc, 0x00, 0x1f, 0xe0, 0x00, 0xff, 
 45	0xff, 0xe1, 0xfe, 0x00, 0x07, 0xf8, 0x00, 0x7f, 0xff, 0xc0, 0x7f, 0x00, 0x03, 0xf8, 0x00, 0x7f, 
 46	0xff, 0x80, 0x3f, 0xc0, 0x01, 0xfc, 0x00, 0xff, 0xff, 0x00, 0x1f, 0xe0, 0x03, 0xf8, 0x00, 0xff, 
 47	0xfe, 0x00, 0x0f, 0xf0, 0x03, 0xf0, 0x01, 0xff, 0xff, 0x80, 0x07, 0xf8, 0x07, 0xe0, 0x03, 0xff, 
 48	0xff, 0xc0, 0x01, 0xfc, 0x0f, 0xc0, 0x07, 0xff, 0xff, 0xe0, 0x00, 0xff, 0x1f, 0xc0, 0x0f, 0xff, 
 49	0xff, 0xf0, 0x00, 0x7f, 0xbf, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x3f, 0xff, 0x00, 0x3f, 0xff, 
 50	0xff, 0xfe, 0x00, 0x0f, 0xfe, 0x00, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x07, 0xfc, 0x00, 0xff, 0xff, 
 51	0xff, 0xff, 0x80, 0x03, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xf0, 0x03, 0xff, 0xff, 
 52	0xff, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff, 0xff, 
 53	0xff, 0xff, 0xfc, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x1f, 0xff, 0xff, 
 54	0xff, 0xff, 0xff, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x7f, 0xff, 0xff, 
 55	0xff, 0xff, 0xff, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x01, 0xff, 0xff, 0xff, 
 56	0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xff, 0xff, 0xff, 
 57	0xff, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
 58};
 59
 60void setup() {
 61  Serial.begin(9600);
 62
 63  // initialize the OLED object
 64  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
 65    Serial.println(F("SSD1306 allocation failed"));
 66    for (;;)
 67      ;
 68  }
 69
 70  // Clear the buffer.
 71  display.clearDisplay();
 72
 73  // Display Text
 74  display.setTextSize(1);       // Set text size
 75  display.setTextColor(WHITE);  // Set text color
 76  display.setCursor(0, 28);     // Set cursor position
 77  display.println("Hello world!");
 78  display.display();  // Display the content on the screen
 79  delay(2000);
 80  display.clearDisplay();  // Clear the screen
 81
 82  // Display Inverted Text
 83  display.setTextColor(BLACK, WHITE);  // 'inverted' text
 84  display.setCursor(0, 28);
 85  display.println("Hello world!");
 86  display.display();
 87  delay(2000);
 88  display.clearDisplay();
 89
 90  // Changing Font Size
 91  display.setTextColor(WHITE);
 92  display.setCursor(0, 24);
 93  display.setTextSize(2);
 94  display.println("Hello!");
 95  display.display();
 96  delay(2000);
 97  display.clearDisplay();
 98
 99  // Display Numbers
100  display.setTextSize(1);
101  display.setCursor(0, 28);
102  display.println(123456789);
103  display.display();
104  delay(2000);
105  display.clearDisplay();
106
107  // Display ASCII Characters
108  display.setCursor(0, 24);
109  display.setTextSize(2);
110  for (int i = 1; i < 8; i++) {
111    display.write(i);
112  }
113  display.display();
114  delay(2000);
115  display.clearDisplay();
116
117  // Scroll full screen
118  display.setCursor(0, 0);
119  display.setTextSize(1);
120  display.println("Full");
121  display.println("screen");
122  display.println("scrolling!");
123  display.display();
124  display.startscrollright(0x00, 0x07);  // Scroll the screen to the right
125  delay(5000);
126  display.stopscroll();
127  delay(1000);
128  display.startscrollleft(0x00, 0x07);  // Scroll the screen to the left
129  delay(5000);
130  display.stopscroll();
131  delay(1000);
132  display.clearDisplay();
133
134  // Scroll part of the screen
135  display.setCursor(0, 0);
136  display.setTextSize(1);
137  display.println("Scroll");
138  display.println("some part");
139  display.println("of the screen.");
140  display.display();
141  display.startscrollright(0x00, 0x00);  // Scroll the first column of the screen to the right
142  delay(4000);
143  display.stopscroll();
144  display.clearDisplay();
145
146  // Display bitmap
147  display.drawBitmap(32, 0, sunfounderIcon, 64, 64, WHITE);
148  display.display();
149}
150
151void loop() {
152}


Code-Analyse

  1. Einbindung der Bibliotheken und Anfangsdefinitionen: Die notwendigen Bibliotheken für die Anbindung an das OLED werden eingebunden. Anschließend werden Definitionen bezüglich der Abmessungen und der I2C-Adresse des OLEDs bereitgestellt.

    • Adafruit SSD1306: Diese Bibliothek wurde entwickelt, um die Anbindung des SSD1306 OLED-Displays zu unterstützen. Sie bietet Methoden zur Initialisierung des Displays, Steuerung seiner Einstellungen und Anzeige von Inhalten.

    • Adafruit GFX-Bibliothek: Dies ist eine Kerngrafikbibliothek zum Anzeigen von Text, Erzeugen von Farben, Zeichnen von Formen usw. auf verschiedenen Bildschirmen, einschließlich OLEDs.

    Bemerkung

    Um die Bibliothek zu installieren, verwenden Sie den Arduino Library Manager und suchen Sie nach „Adafruit SSD1306“ und „Adafruit GFX“ und installieren Sie diese.

    #include <SPI.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    
    #define SCREEN_WIDTH 128  // OLED display width, in pixels
    #define SCREEN_HEIGHT 64  // OLED display height, in pixels
    
    #define OLED_RESET -1
    #define SCREEN_ADDRESS 0x3C
    
  2. Bitmap-Daten: Bitmap-Daten zur Anzeige eines benutzerdefinierten Symbols auf dem OLED-Bildschirm. Diese Daten repräsentieren ein Bild in einem Format, das das OLED interpretieren kann.

    Sie können dieses Online-Tool namens image2cpp verwenden, um Ihr Bild in ein Array umzuwandeln.

    Das Schlüsselwort PROGMEM zeigt an, dass das Array im Programmspeicher des Arduino-Mikrocontrollers gespeichert ist. Die Speicherung von Daten im Programmspeicher (PROGMEM) anstelle des RAMs kann hilfreich sein, wenn es sich um große Datenmengen handelt, die sonst zu viel Platz im RAM einnehmen würden.

    static const unsigned char PROGMEM sunfounderIcon[] = {...};
    
  3. Setup-Funktion (Initialisierung und Anzeige): Die setup()-Funktion initialisiert das OLED und zeigt eine Reihe von Mustern, Texten und Animationen an.

    void setup() {
       ...  // Serial initialization and OLED object initialization
       ...  // Displaying various text, numbers, and animations
    }