Micro:bit OLED Game
Introduction: Micro:bit OLED Game
There was a time when every kid wanted a Nintendo Game & Watch handheld electronic game. This combined a very simple LCD screen with a very simple game mechanic. An early example was the Fire game, where you moved the firemen’s safety net left or right to bounce residents from a burning building on the left into a waiting ambulance on the right.
Let's add an OLED screen to the BBC Micro:bit to create an homage to these games. We'll need to use a breakout board and a breadboard to connect the Micro:bit to a 128x64 OLED display, and we'll need to write the game in Python. We’ll call the game Post, and bounce letters from left to right using a paddle.
Supplies
Because we're using an OLED screen, we'll need to create the real thing rather than use Tinkercad. We'll need at least the following:
* A Micro:bit (ideally version 2, to include sound with our game without requiring additional components);
* A Micro:bit breakout board which exposes its Inter-Integrated Circuit (I²C) ports 19 and 20, such as the Keyestudio Prototype Breakout Board;
* A simple breadboard with power rails, such as the Kitronik Small Prototype Breadboard;
* A 1.3" 128x64 OLED display, supporting I²C, such as the Keyestudio OLED Display;
* Two buttons suitable for a printed circuit board (PCB), such as the Kitronik push switches; and
* Male-to-male breadboard jumper wires, such the Kitronik pack.
If you'd like to go beyond a breadboard prototype, and make a real handheld game, you'll also need the following:
* A simpler Micro:bit breakout board which exposes I²C ports 19 and 20, such as the Keyestudio Breakout Board Adapter;
* A printed circuit board (PCB) breadboard with power rails, such as the Adafruit Perma-Proto Breadboard;
* Optionally, a 3V button battery and holder, such as the Wurth battery holder; and
* Craft materials, such as cardboard, with which to create a case.
Step 1: Getting Started With the Mu Editor
The Mu editor is the go-to editor for the Micro:bit when you’re ready to use Python (it’s actually MicroPython on the Micro:bit). After installing Mu, open the editor, and then connect a Micro:bit to your computer. At the bottom-left of the Mu editor’s window, in its status bar, you should see confirmation that it “detected new BBC micro:bit device”.
Next we want to ensure that our Python code can be flashed onto the Micro:bit. 1003
In the Mu editor, click the New button to create a new Python file, and type the following simple code:
from microbit import *
display.scroll("Hello, World!")
Click on the Flash button in the Mu editor. The yellow LED on the Micro:bit should start flashing to indicate that a file operation is occurring. When it finishes, your Micro:bit should scroll “Hello, World!” on its 5x5 LED display.
Step 2: Installing the OLED Python Libraries
To have the Micro:bit send instructions to the OLED display, we need to install some Python libraries on our Micro:bit. The Mu editor makes this really easy.
First, find your home directory for Mu.
Second, copy all the Python files (all files with the .py extension) from fizban’s Github repository, and place the copies in your Mu home directory.
In the Mu editor, click on the Files button. This will open two panes at the bottom of the editor. On the left are the files on the Micro:bit—you might have one or more, depending on what you’ve done previously with your Micro:bit. On the right are the .py files in your home directory. You should see the ssd1306 files which you’ve copied from fizban’s repository.
Next, simply drag and drop the following three files from the right pane (your home directory) to the left pane (the Micro:bit), which installs these libraries on the Micro:bit.
* ssd1306.py
* ssd1306_px.py
* ssd1306_text.py
For this project, we only need those three files. Yet keep them all available in your home directory, as you can use them for other projects using the OLED display.
Click the Files button again to remove the bottom panes, and to make the Flash button available again. 991