Deutsch English |
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:
|
|
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)) |
|
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 |
# 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 |
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() |
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:
# Oled4.py import oled oled.init() oled.image("einstein") |