microbit.display
Support for the onboard LED matrix display of Microbit.
Overview
The microbit.display module is a general purpose device driver for controlling LED matrix displays. LED matrix displays are simple, inexpensive devices that use a single LED as a display pixel. They therefore tend to be of relatively low resolution, but often provide visually attractive displays due to the high coherence and brightness of the LEDs.
LEDs in matrix displays are connected to general purpose I/O (GPIO) pins on CPU. Although they could be connected such that each LED has its own GPIO pin, this is not usually the case, as the scarce resource of GPIO pins would soon be used up. For example, on the micro:bit, the LED matrix has 25 LEDs. If this required 25 GPIO pins, then there would be none left for anything else!
Instead, these LEDs are controlled by 12 GPIO pins. Three of those pins provide power to LEDs, the other nine provide a route to ground. The pins that source power are called rows. The pins that sink power are called columns. The following diagram shows how the 5x5 grid is connected into 3 logical 'rows' and 9 'columns':

So, if we wanted to light up the middle LED, we would need to put a HIGH voltage (logic 1) on row 2, and a LOW voltage (logic 0) on column 3. Notice that when row 2 is HIGH, we the value we write to the 9 column pins affect all of the LEDs 2.1 to 2.7, without affecting any of the LEDS on row 1 or row 3. Sharing GPIO pins in this way is known as multiplexing. Moreover, if we scan through the different rows fast enough - faster than the eye can see - then we can provide the illusion of all the LEDS being on at the same time! This is a technique known as persistence of vision... and was also the same basis of the very first TV sets (although that did not use LEDs!).
In it's normal mode, the display will update every 18ms (55 Hz). If the display is in light saving mode the period is changed to 15ms (66 Hz).
Capabilities
The microbit.display module provides a driver for a general purpose matrix display, and also several high level features that make creating animations and visual effects on the micro:bit LED display easy and fun! This module lets you:
- Control the LED matrix on the micro:bit.
- Use an optimised typeface (font) so you can show upper and lower case letters, numbers of symbols on the display.
- Set Display wide or per-pixel brightness control up to 256 levels per pixel.
- Create, move, paste and animate images.
- Scroll and print images and text.
- Access the screen buffer directly, so you can manipulate individual pixels.
Using the Display
When using Microbit-Lua, the display is automatically set up, and ready for you to use. Use any or all of the functions listed in the API section below to create effects on the LED display. Here are a few examples to get you started though!
Scrolling Text
Simply use the scroll function to specify the message you want to scroll, and sit back and watch the result. The message you provide will be scrolled, pixel by pixel across the display from right to left. If you take a look at the documentation for the scroll function in the API below, you will notice that you can also specify the speed of the scroll as an optional final parameter. The lower the delay, the faster your text will scroll across the screen.
microbit.display.scroll("HELLO!")
microbit.display.scroll("HELLO!", 100)
Notice that you can also scroll numbers (either constants of variables):
c = 42
microbit.display.scroll(c)
Printing Text
Sometimes it is better to show the letters/numbers in turn on the screen rather than scrolling them. If you want to do this, the 'print' function has exactly the same parameters as 'scroll', but with this behavior. e.g.
microbit.display.print("HELLO!")
microbit.display.print("HELLO!", 100)
microbit.display.print(42)
Do notice that print behaves slightly differently if you provide a single character or numeric digit though. if you do this, the value you provide will stay on the screen until you explicitly change the screen. If you ask the runtime to print a string with two or more characters, then each will appear in turn, then disappear. e.g. try this and you will find it stays on the screen:
microbit.display.print("X")
microbit.display.print(7)
Showing Images
It is also possible to show and pan bitmap images on the display. Images are represented in the runtime by using a table. These can easily be created, just as you create any variable. Once created, you can then provide them as a parameter to the `pan` and `show` functions. Unlike the text based animation functions, you can also specify exactly where in the screen you would like the image to appear - and you can even treat pixel values of zero as transparent if you like! Here are a few simple examples:
-- show your smiley on the screen...
smiley = {height = 5, width = 5, data = string.char(0, 255, 0, 255, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255, 255, 0)}
microbit.display.show(smiley)
-- make your smiley peep up from the bottom of the screen...
smiley = {height = 5, width = 5, data = string.char(0, 255, 0, 255, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255, 255, 0)}
for y = 4, 0, -1 do
microbit.display.show(smiley, 0, y, 100)
end
-- scroll your smiley across the screen...
smiley = {height = 5, width = 5, data = string.char(0, 255, 0, 255, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255, 255, 0)}
microbit.display.pan(smiley)
Running in the Background...
By now you have probably noticed that the scroll, print, pan, show and animate functions all wait for the effect requested to finishes before returning. This is by design, to allow you to easily synchronise your programs. However, sometimes you want to launch an effect, and let it run in the background while your program does something else. For this, you can use the Async variations of the scroll, print, pan, show and animate functions. These all have identical parameters and capabilities, but will return immediately. Try some of the examples above with their Async equivalents to understand this different behaviour. For example:
-- scroll your smiley across the screen, without waiting for it to finish...
smiley = {height = 5, width = 5, data = string.char(0, 255, 0, 255, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255, 255, 0)}
microbit.display.pan(smiley)
Module
Functions
- microbit.display.animate
- microbit.display.animateAsync
- microbit.display.clear
- microbit.display.disable
- microbit.display.enable
- microbit.display.getBrightness
- microbit.display.getDisplayMode
- microbit.display.getHeight
- microbit.display.getPixelValue
- microbit.display.getWidth
- microbit.display.pan
- microbit.display.panAsync
- microbit.display.print
- microbit.display.printAsync
- microbit.display.readLightLevel
- microbit.display.rotateDown
- microbit.display.rotateLeft
- microbit.display.rotateRight
- microbit.display.rotateUp
- microbit.display.screenShot
- microbit.display.scroll
- microbit.display.scrollAsync
- microbit.display.setBrightness
- microbit.display.setDisplayMode
- microbit.display.setPixelValue
- microbit.display.setSleep
- microbit.display.show
- microbit.display.showAsync
- microbit.display.stopAnimation