Introduction
I bought a couple of Adafruit 8x8 displays in a sale and tried them out with various microcontrollers. This post is about using them with the BBC micro:bit. It assumes you have already assembled the display by soldering the LED matrix to the backpack. If not, refer to this Adafruit guide.They consist of an 8x8 LED matrix soldered onto an I2C "backpack" that contains an HT16K33 LED driver/controller. The I2C protocol is a serial protocol that allows different data to be sent to different devices along a "bus". Each device must have its own address on the bus. The Adafruit backpack has a default address of 0x70 but this can be changed to one of seven other addresses by soldering across one or more of the three jumpers on the backpack. This would allow you to have several LED matrices running from the same micro:bit,and controlled separately.
Connecting things up
The first step is to connect the display to the micro:bit. You'll need to use an edge-connector board to access the I2C pins (P19 and P20). Some ready-made edge-connectors don't have those two pins soldered in place so you might need to get the soldering iron out. Assuming you have them, connecting is easy (SDA=P20, SCL=P19) but I used a separate power supply for the display as it will struggle to light up all the pixels if you draw power from the micro:bit supply. Don't forget to connect the earth (- supply) connections on the two power sources togther (i.e. connect an extra wire from -ve on the backpack to -ve on the micro:bit edge connector). Micropython on the micro:bit automatically enables the internal pullup resistors on the I2C lines, so the usual 5.1k resistors on the SCL and SDA connections are not needed.Python code
I was happy to find that deshipu from the MicroPython forum had already ported a Python library for the HT16K33 LED driver/controller to work with MicroPython. This library allows the same I2C backpack to be used with three different display types. You will need to copy the library to the users\mu_code folder on your PC.Here's some code I used to test the various functions that the display uses. It clears the display, then fills it again, then dims the display in steps and clears it again. Then it draws a "smiley face" (using the byte array image() expressed in binary to make it more obvious which pixels should be lit up) before flashing the display on and off before finally clearing it again.
You can design your pattern on a piece of paper and put a '1 where you want the pixel lit and a '0' where you want a pixel off. Once you have got the pattern you want, you could convert the binary to hex to save space as Micropython on the micro:bit doesn't leave much space for programs/data.
from microbit import i2c, sleep
from ht16k33 import Matrix8x8
# address of HT16K33 is 0x70
display = Matrix8x8(i2c, address=0x70)
# Clear the display
display.fill(0x00)
display.show()
sleep(1000)
# Fill the display
display.fill(0xff)
display.show()
sleep(1000)
#step through some different brightness levels
display.brightness(1)
sleep(3000)
display.brightness(7)
sleep(3000)
display.brightness(15)
sleep(3000)
# Clear the display
display.fill(0x00)
display.show()
sleep(1000)
# define the bitmap image to be displayed (in this case a smiley face)
image = (
0b01111110,
0b10000001,
0b10100101,
0b10000001,
0b10100101,
0b10011001,
0b10000001,
0b01111110,
)
#draw the image
for y, line in enumerate(image):
for x in range(8):
if line & (1 << x):
display.pixel(y, x, 1)
display.show()
sleep(3000)
# Blink the display
display.blink_rate(0x83)
sleep(3000)
# Stop blinking?
display.blink_rate(0x81)
sleep(3000)
# Clear the display
display.fill(0x00)
display.show()
sleep(1000)
Getting it onto the micro:bit is a two-stage process. First "flash" the test code to the micro:bit in the usual way using Mu. This will result in an error as the required library is not on the micro:bit yet. Once the error message has finished scrolling on the micro:bit's display, press the "Files" button in Mu. If you don't see that button, you probably have an old version of Mu, so replace it with the current version. This will open up a new window below the code window. Provided you have put the library file in the mu_code folder on your PC, you should see it in the right-hand panel in Mu (along with any other files in your mu_code folder). Using the mouse, drag it and drop it into the left-hand window. This should copy it to the micro:bit. Now, if you disconnect the programming cable, power the micro:bit off and on again and the code should run.