LED Matrix (8x8)ΒΆ
- A small 8x8 LED matrix to display images, characters or small animations.
Raspberry Pi set up: https://learn.adafruit.com/adafruit-led-backpack/1-2-8x8-python-wiring-and-setup
##
# Quick start, define image and then mmove round
#
import time
import board
import busio
from adafruit_ht16k33 import matrix
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# Create the matrix instance.
matrix = matrix.Matrix8x8(i2c, address=0x71, auto_write=True)
# Clear the matrix.
matrix.fill(0)
# Set a pixel in the origin 0, 0 position.
matrix[0, 0] = 1
time.sleep(3)
matrix.fill(0)
# Define a ball
img = [
[0,0,1,1,1,1,0,0],
[0,1,0,0,0,0,1,0],
[1,0,0,0,0,0,0,1],
[1,0,0,1,1,1,0,1],
[1,0,1,1,1,1,1,1],
[0,1,1,1,1,1,1,0],
[0,0,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0]
]
for x in range(8):
for y in range(8):
matrix.pixel(x, y, img[x][y])
# Move the image round
while True:
for frame in range(8):
matrix.shift_right(True)
time.sleep(0.1)
for frame in range(8):
matrix.shift_down(True)
time.sleep(0.1)
for frame in range(8):
matrix.shift_left(True)
time.sleep(0.1)
for frame in range(8):
matrix.shift_up(True)
time.sleep(0.1)
##
# Quick start, scroll dot, load images from files
#
from time import sleep
import board
from adafruit_ht16k33.matrix import Matrix8x8
from PIL import Image
# Define width and height of matrix
WIDTH = HEIGHT = 8
matrix = Matrix8x8(board.I2C(), address=0x71, auto_write=False)
# Set brightness for whole matrix 0.0-1.0
matrix.brightness = 0.5
# Define some stripes
imgs = [
[1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1]
]
# Set blink rate for whole matrix 0-3 by 0.25
# matrix.blink_rate = 1
# Running LED
for i in range(2):
for x in range(8):
for y in range(8):
matrix[x, y] = 1
matrix.show()
sleep(0.04)
matrix[x,y] = 0
matrix.show()
# Load two images
images = [Image.open("arrow1.png"), Image.open("arrow2.png")]
for i in range(10):
matrix.image(images[i % 2])
matrix.show()
sleep(0.5)
sleep(1)
# Animate stripes
for i in range(15):
count = 0
for x in range(WIDTH):
for y in range(HEIGHT):
matrix[x, y] = imgs[i%2][count]
count += 1
matrix.show()
sleep(0.2)
# Clear matrix
matrix.fill(0)
matrix.show()
##
# Space invader animation
#
# SPDX-FileCopyrightText: 2018 Phillip Burgess/paintyourdragon for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import adafruit_ht16k33.matrix
import board
import busio as io
matrix = adafruit_ht16k33.matrix.Matrix8x8(io.I2C(board.SCL, board.SDA), address=0x71, auto_write=False)
WIDTH = HEIGHT = 8
x = y = 0
matrix.fill(0)
matrix.show()
# Seconds to pause between frames
frame_delay = [.25, .25, .25, .25, .25, .25, .25, .25, .25, .25, .25, .25,
.125, .125, .125, .125, .125, .125, .125, .125, .125]
# Counter for animation frames
frame_count = 0
# Animation bitmaps
animation = [
# frame 0 - alien #1 frame 1
[[0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 1, 0, 1]],
# frame 1 - alien #1 frame 2
[[0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0], [0, 1, 0, 0, 0, 0, 1, 0]],
# frame 2 - alien #1 frame 1 (duplicate frame 1)
[[0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 1, 0, 1]],
# frame 3 - alien #1 frame 2 (duplicate frame 2)
[[0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0], [0, 1, 0, 0, 0, 0, 1, 0]],
# frame 4 - alien #2 frame 1
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [1, 1, 0, 0, 0, 0, 1, 1]],
# frame 5 - alien #2 frame 2
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0]],
# frame 6 - alien #2 frame 1 (duplicate frame 5)
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [1, 1, 0, 0, 0, 0, 1, 1]],
# frame 7 - alien #2 frame 2 (duplicate frame 6)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0]],
# frame 8 - alien #3 first frame
[[0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 0, 0]],
# frame 9 - alien #3 second frame
[[0, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0]],
# frame 10 - alien #3 first frame (duplicate frame 9)
[[0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 0, 0]],
# frame 11 - alien #3 second frame (duplicate frame 10)
[[0, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0]],
# frame 12 - alien #4 first frame
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
# frame 13 - alien #4 second frame
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]],
# frame 14 - alien #4 third frame (not a repeat)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]],
# frame 15 - alien #4 fourth frame (not a repeat)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
# frame 16 - alien #4 first frame (duplicate frame 12)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 0, 1, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
# frame 17 - alien #4 second frame (duplicate frame 13)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]],
# frame 18 - alien #4 second frame (duplicate frame 14)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0]],
# frame 19 - alien #4 second frame (duplicate frame 15)
[[0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
]
# Run until we are out of animation frames
# Populate matrix
while True:
if frame_count < len(animation):
for x in range(WIDTH):
for y in range(HEIGHT):
matrix.pixel(x, y, animation[frame_count][x][y])
# next animation frame
frame_count += 1
# show animation
matrix.show()
# pause for effect
time.sleep(frame_delay[frame_count])
else:
matrix.fill(0)
matrix.show()
time.sleep(.1)
frame_count = 0