The micro:bit module has a 5x5 matrix, or grid, of LEDs on the front of it. An LED, or Light Emitting Diode, is an electrical component that allows electricity to flow through it in only one direction.
When an LED is hooked up in the proper direction and electricity is applied, the LED lights up. The 25 LEDs on the face of the micro:bit module are already connected so that we do not need to worry about their connected direction, however, additional LEDs can be connected to the cyber:bot using the breadboard.
Collectively these 25 LEDs form the display of the micro:bit module. The micro:bit module can be programmed so that the LEDs can show images, scroll text across the screen, or be individually illuminated to a variety of brightness levels.
Each LED row (x axis) and column (y axis) is numbered 0 through 4, so each LED can be identified individually. But first, we will try convenient, ready-made instructions for displaying scrolling text and predefined images.
The three pages that follow this one will cover what you need to know to work through the cyber:bot tutorials. But, it is by no means entirely comprehensive. Feel free to check the official MicroPython documentation any time.
Scrolling text across the display is a very useful way to get information from your micro:bit program. When you have your micro:bit on your cyber:bot robot, this is a good way to display values from the robot's sensors.
To scroll text across the display, use the aptly named display.scroll method. The text you want to display goes inside quotation marks " ", and that goes inside the method's parentheses ( ).
#hello from microbit import * display.scroll("Hello")
You should see the word Hello scroll across the display one character at a time.
The first line, #hello is a comment. It is just there for you to read the script's name, but is ignored by the micro:bit. We will look at comments again later!
The second line, from microbit import *, fetches the code that knows how to run the display methods. Every complete example script in these tutorials will have at least one import * statement at the top.
The third line makes the text appear on the display. The "Hello" text in quotation marks is a parameter of the display.scroll method, and so goes inside its parentheses.
The text you want to display isn't the only parameter you can use with display.scroll. You can control the speed at which the text scrolls across the display by adding a an additional parameter to the display.scroll method called delay. The larger the value for delay, the slower the text will scroll.
#hello_goodbye from microbit import * display.scroll("Hello", delay = 500) display.scroll("Goodbye", delay = 150)
You should see the word Hello scroll across the display slowly, then the word Goodbye scroll across the screen quickly.
Be cautious when copying and pasting code with quotation marks! Some types of documents use smart-quote characters “ and ” that will result in a syntax error if you try to use them in a script. If you are receiving a syntax error after copying and pasting code with quotation marks, try erasing and retyping the quotation marks directly in your programming environment, then re-flash the program.
Sometimes you might want the micro:bit module to simply show an image instead of scrolling text. We can do this with display.show method, using premade images from the Image object. For example: display.show(Image.DUCK).
#image from microbit import * display.show(Image.DUCK)
You should see a duck appear on your micro:bit module's LED matrix, like the picture shown above.
Image.HEART Image.HEART_SMALL Image.HAPPY Image.SMILE Image.SAD Image.CONFUSED Image.ANGRY Image.ASLEEP Image.SURPRISED Image.SILLY Image.FABULOUS Image.MEH Image.YES Image.NO Image.CLOCK12 Image.CLOCK11 Image.CLOCK10 Image.CLOCK9 Image.CLOCK8 Image.CLOCK7 Image.CLOCK6 |
Image.CLOCK5 Image.CLOCK4 Image.CLOCK3 Image.CLOCK2 Image.CLOCK1 Image.ARROW_N Image.ARROW_NE Image.ARROW_E Image.ARROW_SE Image.ARROW_S Image.ARROW_SW Image.ARROW_W Image.ARROW_NW Image.TRIANGLE Image.TRIANGLE_LEFT Image.CHESSBOARD Image.DIAMOND Image.DIAMOND_SMALL Image.SQUARE Image.SQUARE_SMALL Image.RABBIT |
Image.COW Image.MUSIC_CROTCHET Image.MUSIC_QUAVER Image.MUSIC_QUAVERS Image.PITCHFORK Image.XMAS Image.PACMAN Image.TARGET Image.TSHIRT Image.ROLLERSKATE Image.DUCK Image.HOUSE Image.TORTOISE Image.BUTTERFLY Image.STICKFIGURE Image.GHOST Image.SWORD Image.GIRAFFE Image.SKULL Image.UMBRELLA Image.SNAKE |
You have displayed a single image with a single line of code. To display a series of images, place the sleep function on a line of code between them. The sleep function pauses the micro:bit module for a specified number of milliseconds, giving you time to see the image on the display. For example:
display.show(Image.HEART) sleep(500) display.show(Image.HEART_SMALL) sleep(500)
...will show an image of a heart for half a second (500 milliseconds), then show an image of a small heart for another haf a second. Try it!
#images from microbit import * display.show(Image.HEART) sleep(500) display.show(Image.HEART_SMALL) sleep(500)
(500 ms) (500 ms)
In the script images, the heart looks as if it beats once.
You can create custom images from individual pixels on the micro:bit module’s display by using display.set_pixel.
The set_pixel method has three parameters: x, y, and b:
display.set_pixel(x, y, b)
The arguments for x and y indicate the pixel's position in the micro:bit module display's coordinate plane. The b argument controls the brightness (0-9) of the pixel.
To see how this works, try the following program which lights up two LEDs.
# two_pixels from microbit import * display.set_pixel(0, 0, 9) display.set_pixel(1, 3, 4)
The first statement display.set_pixel (0, 0, 9)lights up the pixel is located at (0,0) which is the top left of the display. It is set to the maximum brightness of 9.
The second statement display.set_pixel (1, 3, 4) lights up the pixel located at (1,3), which is in the second column from the left and down 4 rows. The brightness is only 4, so it is significantly dimmer than the first pixel.
Using the following script, see how a medium size box can be made by lighting up 8 pixels in the middle of the micro:bit module’s display.
# medium_box from microbit import * display.set_pixel(1, 1, 9) display.set_pixel(1, 2, 9) display.set_pixel(1, 3, 9) display.set_pixel(2, 1, 9) display.set_pixel(2, 3, 9) display.set_pixel(3, 1, 9) display.set_pixel(3, 2, 9) display.set_pixel(3, 3, 9)
Links
[1] https://microbit-micropython.readthedocs.io/en/latest/