Jython
Deutsch   English 

18. OLED Display


The matrix display of the micro:bit very limited for writing out data and text. The 4-digit seven-segment display described here, is a bit more convenient. However, the displayable character set is small and only 4 characters can be displayed at the same time. A real graphic display, where each pixel can be controlled individually, offers much more possibilities. The cheap OLED display based on the SSD1306 controller has a resolution of 128 x 64 pixels, whereby each pixel can be addressed individually as black (off) or white (on). Text as well as simple graphics can be displayed.

Data transmission uses I2C and therefore requires only 4 lines GND, VCC, SCL and SDA. The connection to the micro:bit is made as with  sensors via an I2C hub with a Grove plug which is connected to pin GND, 3V, P19 (SCL), P20 (SDA). Cut a cable with Grove plugs in two pieces and solder it to the display as shown in the figure.

 

Connection options for I2C devices with Grove connectors:

Didel Hub (Source of supply to be given)

Breadboard with Piromoni pinbit and

Grove bridge connector

For writing text or graphics to the display the module oled.py is used, which is quite tricky due to the small memory space of the micro:bit. (In particular, the character font is taken from the class Image, which is included in the module microbit and is normally used for writing text on the matrix display).  The library can be downloaded from here. In the following example a 4-line text is written, which remains on the display even if the program ends.

# Oled1.py
import oled

oled.init()
oled.text(0, 0, "Hello Python")
oled.text(0, 1, "Programming")
oled.text(0, 2, "is fun!")
n = 123456789123
oled.text(0, 3, str(n))
► Copy to clipboard
 

 

To rewrite a line, you should write a total of 12 characters, e.g. with a format string. The periodic deletion with clear() causes a flickering.

No flickering   Flickering
# Oled2.py
import oled

oled.init()
n = 1000
while True:
    oled.text(0, 0, "v: %-9d" %n)
    n -= 1
    if n == 0:
        n = 1000
► Copy to clipboard
 
# Oled2a.py
import oled

oled.init()
n = 1000
while True:
    oled.clear()
    oled.text(0, 0, str(n))
    n -= 1
    if n == 0:
        n = 1000
► Copy to clipboard

The display is often used to indicate measured values, e.g. when using the Sensirion temperature/humidity sensor. Since the update only takes place every 5 s, the display is erased with clear().

# Oled3.py
import oled
from microbit import sleep
import sht

oled.init()
while True:
    v = sht.getValues()
    oled.text(0, 0, "t: %.1f C" %v[0])
    oled.text(0, 1, "h: %d%%" %v[1])
    sleep(5000)
    oled.clear()
► Copy to clipboard
   

 

Image display:

Black and white images of 128 x 64 = 8196 pixels can be shown on the display. The image file must be binary encrypted and stored in the micro:bit file system. Each byte corresponds to 8 display pixels, which are either switched on or off. Thus 1024 bytes are used and the order is defined as show here:

 

To create the binary file in this format, proceed as follows:

  • Create an image (e.g. einstein.bmp) of size 128x64 with any image editor and save it as BMP. The conversion program will convert pixels with the brightness h = (r + g + b) / 3 < 100 to black and the others to white pixels. So it is best to use only the colors black (h = 0) and white (h = 255) when editing.
  • Start TigerJython and write a program that contains the single line
    import bmp2oled
    and starts it. In the file dialog select einstein.bmp. The binary image file einstein
    without extension) is created according to the format specified above.
  • Copy einstein with TigerJython under the menu option Tools | Download module and the option File to micro:bit.
  • With the following program the image is shown on the display.
# Oled4.py
import oled

oled.init()
oled.image("einstein")
► Copy to clipboard