ARDUINO Nano OLED Display 0,91 Zoll SSD1306 Chipsatz 128×32 Pixel

Von Sven
tutorial arduino oled display ssd1306 moba module

Eine Anleitung damit das OLED Display erwacht

Für das Projekt Mini Diorama in Spur N möchte ich ein OLED Display für verschiedene Anzeigen der Betriebszustände des Dioramas verwenden. Die Wahl fiel auf ein kleines 0,91 Zoll OLED Display mit 128 x 32 Pixel. Dies bekommt man derzeitig in allen gängigen Onlineshops zwischen 5 und 6 Euro zu kaufen. Damit die Programmierung später schnell erfolgen kann, soll hier ein kleines Testprogramm installiert und eine Inbetriebnahme / Funktionsprobe gezeigt werden.

Spezifikationen des Displays

  • Modul Abmaße: 40.00 mm × 12.00 mm × 3.00 mm (ohne Pinleiste)
  • Anzeigefläche: 22.384 mm × 5.584 mm
  • Pixel: 128 x 32
  • Pixelgröße: 0.152 × 0.152 mm
  • Display: monochrom (blaue Pixel)
  • Schnittstelle: I2C
  • Größe: 0.91 inch

Aufbau und Verdrahtung

oled display arduino

Die Verdrahtung der Schaltung ist einfach. Wie auf dem Bild zu erkennen werden die 9V DC + an den Vin Pin des Arduino Nano geführt und der GND (9 V Blockbatterie -) an den GND Pin. Bei dieser kleinen Schaltung würde es auch ausreichen den USB Stecker permanent verbunden zu lassen und die 5V USB Spannung des PC zu nutzen, dann entfällt die 9V Blockbatterie.

Der Pin 5V des Arduino Nano kommt an den VCC Pin des Displays und der GND Pin des Nano wird mit dem GND Pin des OLED Displays verbunden. Pins A4 und A5 sind die I2C Schnittstelle des Nano und werden an SDA und SCK des Displays angeschlossen.

Pinbelegungen / -verbindungen

Arduino NanoOLED Display
5VVCC
GNDGND
A4SDA
A5SCK

Bibliothek zur Ansteuerung auf GitHub

Wer in der Suchmaschine nach Bibliotheken zur Programmierung des OLED Displays sucht, wird schnell fündig werden. Es gibt hier viele freie Programme und Beispiele. Ich habe mich dann für eine Bibliothek auf GitHub entschieden. Der Entwickler hat hier wirklich eine schöne Arbeit geleistet. Die entwickelte Bibliothek kann sehr flexibel genutzt werden und ist sehr umfangreich. Er schreibt auf GitHub dazu folgendes:

Link > https://github.com/lexus2k/ssd1306

SSD1306 driver is Arduino style C/C++ library with unicode support. The library can be compiled for plain Linux (for example, raspberry spi), or you can use it with plain avr-gcc compiler without Arduino IDE. It supports monochrome and RGB oleds and has debug mode, allowing to execute code on PC, using SDL2.0. Initially the library is intended for very small microcontrollers (with a little of RAM). It was developed to use as few resources as possible, but still has powerful capabilities (NanoEngine), allowing to develop nice animation. It works on any powerful devices like raspberry pi, esp32; and can be easily ported to new platform.

Übersetzt in Deutsch:
Der SSD1306-Treiber ist eine C / C ++ – Bibliothek im Arduino-Stil mit Unicode-Unterstützung. Die Bibliothek kann für einfaches Linux (z. B. Himbeer-SPI) kompiliert werden, oder Sie können sie mit einem einfachen avr-gcc-Compiler ohne Arduino-IDE verwenden. Sie unterstützt Monochrom- und RGB-Oleds und verfügt über einen Debug-Modus, mit dem Code mithilfe von SDL2.0 auf dem PC ausgeführt werden kann. Die Bibliothek ist zunächst für sehr kleine Mikrocontroller (mit etwas RAM) gedacht. Sie wurde entwickelt, um so wenig Ressourcen wie möglich zu verbrauchen, verfügt jedoch über leistungsstarke Funktionen (NanoEngine), mit denen sich schöne Animationen entwickeln lassen. Es funktioniert auf allen leistungsstarken Geräten wie Himbeer-Pi, esp32; und kann leicht auf eine neue Plattform portiert werden.

Quelle: https://github.com/lexus2k/ssd1306

Sourcecode OLED Display SSD1306

lok
Bitmap der Lok

Für den Test des OLED Displays möchte ich eine Lok als Bitmap (Bild) und einen Text anzeigen lassen. Dafür erstelle ich mir in einem Grafikprogramm meine Bitmap. Das Bildchen ist 50 x 32 Pixel groß und in schwarz weiß. Gespeichert wird dieses Bild als .bmp. Wir benötigen jetzt noch ein Datenformat, welches unsere Programmierumgebung und der Arduino verstehen. Dazu benutze ich das Programm LCD Assistant. Dieses kleine Programm wandelt unsere Bilddaten in eine Hexadezimale Schreibweise um, welche wir dann im Speicher des Mikrocontroller ablegen können. Programm-Link > http://en.radzio.dxp.pl/bitmap_converter/.

bild in hex lcd assistent moba module

Das Endprodukt seht ihr weiter unten. Link > Quelldatei Bild der Lok Dieser Code muss dann in den Haupt-Sourcecode integriert werden. Diese Integration erfolgt über die Datei Lok.h. Link > Bibliothek Lokbild. Dieser Teil kümmert sich um die richtigen Einstellungen der IDE und Übertragungskommunikation.

Das eigentliche Programm bindet dann die erforderlichen Bibliotheken inklusive unserer für das Bild am Anfang ein.

/*
    MIT License
    
    For the SSDL306 / nano_gfx library, Copyright (c) 2017-2019, Alexey Dynda
    Copyright (C) 2021 Sven Häber / MOBA-Module.de
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/

#include "ssd1306.h"
#include "nano_gfx.h"
#include "Lok.h"

// Text Startsequenz
static void textStart()
{
    ssd1306_drawHLine(10 , 2, 118); //obere Linie zeichnen
    ssd1306_drawHLine(10 , 30, 118); //untere Linie zeichnen
    ssd1306_printFixed(19, 8, "ALTSTEINAER TAL", STYLE_NORMAL); //Text an Koordinaten xy ausgeben
    ssd1306_printFixed(22, 16, "moba-module.de", STYLE_NORMAL); //Text an Koordinaten xy ausgeben
    delay(5000); // Lass alles stehen und mache 5 Sekunden lang nichts
    ssd1306_clearScreen(); //Lösche die gesamte Anzeige
}

// Fahrende LOK Startsequenz
static void bitmapLok()
{
  for (int i=127; i>0; i--){ //wir rücken Pixelweise vorwärts
    ssd1306_drawBitmap(i, 0, 50, 32, Lok); //die Lok wird angezeigt an den Koordinaten
    ssd1306_clearScreen(); //Lösche die gesamte Anzeige
  }
  ssd1306_drawBitmap(0, 0, 50, 32, Lok); //Endposition Lok anzeigen
  delay(1000); // Lass alles stehen und mache 1 Sekunde lang nichts
  ssd1306_clearScreen(); //Lösche die gesamte Anzeige
}


//---------------------------------------------------
void setup()
{
  
  //Schriftartauswahl
    ssd1306_setFixedFont(ssd1306xled_font6x8);
  //Display Initialisierung
    ssd1306_128x32_i2c_init();    
}

//--------------------------------------------------
void loop()
{
    ssd1306_clearScreen();
    bitmapLok();
    textStart();
}

Ergebnisse

Nach dem Übertragen des Codes in den Mikrocontroller des Arduino Nano Boards wird abwechselnd die fahrende Lok und der Text angezeigt.

oled display ssd1306 arduino programmieren moba module

Quelldatei Bild der Lok

#include "Lok.h"

//------------------------------------------------------------------------------
// File generated by LCD Assistant
// http://en.radzio.dxp.pl/bitmap_converter/
//------------------------------------------------------------------------------

const uint8_t Lok [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x0E,
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0x0E,
0x0E, 0x00, 0x00, 0x80, 0xF0, 0xF0, 0xF0, 0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xFE, 0xFE, 0xFE, 0xFE, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xC0, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x3F, 0xBF, 0xBF, 0x3F, 0x3F,
0x3F, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x3F, 0xBF, 0x3F, 0x3F, 0x3F, 0x7F, 0xFF, 0xFF, 0x7F, 0x3F,
0x3F, 0xBF, 0x3F, 0x3F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x3F, 0xBF, 0xBF, 0x3F, 0x3F,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x03, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x7F, 0x7F,
0x3F, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x3F, 0x7F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3F, 0x7F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x3F, 0x7F, 0x7F,
0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x00, 
};

Bibliothek für das Lokbild

/*
    MIT License

    Copyright (c) 2016-2018, Alexey Dynda

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/

#ifndef _LOK_H_
#define _LOK_H_

// ----------------------------------------------------------------------------
#include "ssd1306_hal/io.h"
#include <stdint.h>

extern const uint8_t Lok [] PROGMEM;

#endif