Skip to content

Commit e379774

Browse files
committed
initial
0 parents  commit e379774

File tree

111 files changed

+2517
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+2517
-0
lines changed

7-segment-display-max7219.md

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
title: "7 Segment Display with MAX7219 Backpack"
3+
layout: text-width-sidebar
4+
5+
# Meta description for google and sharing
6+
meta-description: "Using a MAX7219-driven 8-digit 7 Segment LED display module on the microbit."
7+
8+
#############
9+
## Options ##
10+
#############
11+
12+
share: true
13+
author: jez
14+
15+
#################################
16+
## Component Specific ##
17+
#################################
18+
19+
# About Box is on the left of the page
20+
about: "Use a 7-segment display to show digits on the microbit"
21+
22+
# Type of component: internal or external
23+
cats: external
24+
25+
# Name of Component for index page
26+
simple-description: 7-Segment Display (SPI)
27+
28+
---
29+
30+
![microbit edge connector](images/7-segment-display-max7219.jpg){:.ui .image .small .floated .right}
31+
32+
This module will display 8 numerical digits on its display. Each digit is a 7-segment display comprised of 8 LEDs (7 for the number, and one for a decimal place).
33+
34+
It's possible to drive each of these component LEDs either directly, or through a shift register. However, these modules use the `MAX7219` driver chip to simplify the process.
35+
36+
The microbit tells the `MAX7219` chip which number to display over SPI. The chip then displays the digit.
37+
38+
Like the HT1633 8x8 module on this site, the LED displays are connected to a backpack containing the `MAX7219` chip and wiring.
39+
40+
{:.ui .dividing .header}
41+
### Components
42+
43+
These 7-segment MAX7219 displays are available on eBay, Amazon and other sellers. Searching for `MAX7219 7-segment display` reveals the components. They're around £2 each.
44+
45+
Additionally, you will need a microbit breakout board to access the SPI pins of the microbit.
46+
47+
{:.ui .dividing .header}
48+
### Electronics
49+
50+
#### Purpose of Each Pin
51+
52+
There are five wires to connect to the display module. Each of the pins do the following:
53+
54+
{:.ui .very .basic .table}
55+
| Pin Name | Purpose |
56+
|--- |--- |
57+
| VCC | for power |
58+
| GND | for ground |
59+
| CS / Chip Select | Goes low when data is being transmitted to the device. Goes high at the end of transmission. `.write_digital()` is used for this. |
60+
| DIN | Data transmitted over this wire. |
61+
| CLK | Tells the microbit when to transmit data. |
62+
63+
#### Hookup Table
64+
65+
Connect the module to the microbit and its edge connector as below. Be aware the pin labels can change dependning on the manufacturer:
66+
67+
{:.ui .very .basic .table}
68+
| My Module Label | Possible Label Names | microbit Pin | Micropython Doc Names for pin |
69+
|--- |--- |--- |--- |
70+
| VCC | | 3v | |
71+
| GND | | GND | |
72+
| DIN | SOMI, SDI, DI, DIN, SI, MRST. | `pin14` | MISO (master in, slave out) |
73+
| CS | nCS, CSN, nSS, STE, SYNC | `pin0` | n/a, but frequently called _chip select_ |
74+
| CLK | SCLK | `pin13` | Serial Clock |
75+
76+
* *Your module might also have a `DOUT` pin. Ignore it!*
77+
78+
To connect to these pins, you will need the microbit edge connector. Here's my module wired up to my microbit:
79+
80+
![microbit edge connector](images/7-segment-display-max7219.jpg){:.ui .image}
81+
82+
{:.ui .dividing .header}
83+
### Code
84+
85+
#### Download the Module
86+
87+
Download the `Maxrix7seg` [module for the microbit from Github.](https://github.com/microbit-playground/matrix7seg/blob/master/matrix7seg.py). This contains the code used to communicate with the 7-segment display.
88+
89+
#### Import Module
90+
91+
The module is imported in the header of your Python script:
92+
93+
{% highlight python %}
94+
from microbit import spi
95+
96+
# from matrix7seg.py import Matrix7seg class
97+
from matrix7seg import Matrix7seg
98+
99+
# Initialise an instance of the Matrix7seg class
100+
# and call it 'seg_display'.
101+
# It's connected to default SPI pins. pin0 is chip select
102+
# pin1 or pin2 etc could be used instead.
103+
seg_display = matrix7seg(spi, pin0)
104+
{% endhighlight %}
105+
106+
#### Add the `matrix7seg` Module
107+
108+
Once your script is complete and has been flashed, the `matrix7seg` module can be copied to the microbit:
109+
110+
1. Flash your script.
111+
2. Click 'Files' within in the mu editor.
112+
3. From the `/mu_code/` directory, copy drag the `matrix7seg.py` file across to the microbit.
113+
4. Press reset on your microbit to reload the Python program with the `matrix7seg` module.
114+
115+
#### Using the Module
116+
117+
It's now possible to use the display module within the microbit:
118+
119+
{% highlight python %}
120+
# Number must be 8 or fewer digits
121+
seg_display.write_number(1234)
122+
123+
# Update display with '1234'
124+
seg_display.show()
125+
{% endhighlight %}
126+
127+
Each time `.write_number()` is used, `.show()` must be used to update the display.
128+
129+
#### Code Examples
130+
131+
##### `.write_number()`
132+
133+
![default display](images/7-segment-display-max7219-default.jpg)
134+
135+
{% highlight python %}
136+
# display a number
137+
seg_display.write_number(1234)
138+
{% endhighlight %}
139+
140+
##### `.write_number(n, zeroPad=True)`
141+
142+
![zerofill](images/7-segment-display-max7219-zerofill.jpg)
143+
144+
{% highlight python %}
145+
# empty 7-segments filled with 0
146+
seg_display.write_number(1234, zeroPad=True)
147+
{% endhighlight %}
148+
149+
##### `.write_number(n, leftJustify=True)
150+
151+
![go left](images/7-segment-display-max7219-left.jpg)
152+
153+
{% highlight python %}
154+
# justify numbers to left
155+
seg_display.write_number(1234, leftJustify=True)
156+
{% endhighlight %}
157+
158+
#### Example Programs
159+
160+
##### Increment Numbers to 100
161+
162+
{% highlight python %}
163+
# sequentially write numbers to 100
164+
for i in range(100)
165+
seg_display.write_number(i)
166+
seg_display.show()
167+
{% endhighlight %}
168+
169+
##### Show the Temperature
170+
{% highlight python %}
171+
# read and display the temperature every second
172+
while True:
173+
seg_display.write_number(temperature())
174+
seg_display.show()
175+
sleep(1000)
176+
{% endhighlight %}
177+
178+
##### G-Force Game
179+
180+
{% highlight python %}
181+
# record total acceleration experienced by the microbit
182+
# if it's the highest value recorded by the microbit,
183+
# display the reading on the screen.
184+
# importing as little as possible to save memory
185+
186+
from microbit import spi, accelerometer, sleep, pin0
187+
from math import sqrt
188+
from matrix7seg import Matrix7seg
189+
190+
def total_acceleration():
191+
"""
192+
return total acceleration in milli-g across all 3 axes.
193+
"""
194+
x = accelerometer.get_x()
195+
y = accelerometer.get_y()
196+
z = accelerometer.get_z()
197+
198+
# root of sum of squares
199+
total = sqrt(x**2 + y**2 + z**2)
200+
return total
201+
202+
highest_reading = 0
203+
204+
segment = Matrix7seg(spi, pin0)
205+
206+
while True:
207+
reading = total_acceleration()
208+
if reading > highest_reading:
209+
highest_reading = reading
210+
segment.write_number(highest_reading)
211+
segment.show()
212+
{% endhighlight %}

8x8-matrix-HT16K33-microbit.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: "Control a 8x8 LED matrix with an I2C backpack on the microbit"
3+
layout: text-width-sidebar
4+
5+
# Meta description for google and sharing
6+
meta-description: "Use the microbit with a HT16K33 I2C backpack from Adafruit to control a 8x8 LED matrix display."
7+
8+
#############
9+
## Options ##
10+
#############
11+
12+
share: true
13+
author: jez
14+
15+
#################################
16+
## Component Specific ##
17+
#################################
18+
19+
# About Box is on the left of the page
20+
about: "Use 8x8 LED matrix with HK16K33 driver "
21+
22+
# Type of component: internal or external
23+
cats: external
24+
25+
# Name of Component for index page
26+
simple-description: LED Matrix (I2C)
27+
28+
acknowledgements: All images by Adafruit (CC-BY).
29+
30+
categories: I2C
31+
32+
tags:
33+
- I2C
34+
- cat
35+
36+
---
37+
38+
An 8x8 LED Matrix is a component where 64 LEDs are arranged in a grid.
39+
40+
In this page the LED matrix plugs into a 'backpack' which drives the display.
41+
42+
The backpack uses the HT16K33 IC which interprets I2C instructions given to it by the microbit.
43+
44+
{:.ui .image}
45+
![Image of Adafruit I2C Backbacok](images/8x8-matrix-HT16K33-microbit-messy.jpg)
46+
47+
The LED matrix plugs into the backpack or is soldered depending on the version.
48+
49+
It is possible to drive just the matrix without the backpack using a shift register but this can be messy.
50+
51+
Instead we send I2C commands to the backpack which in turn lights the 8x8 matrix.
52+
53+
{:.ui .dividing .header}
54+
### Components
55+
56+
There are two components needed: an LED matrix and HT16K33-based backpack. Usually these are sold together as a module. A search on eBay or Amazon reveals many results to `HK16K33 backpack` which range from £3 -- £10.
57+
58+
Alternatively, you could shop at [Adafruit](https://www.adafruit.com/product/1052) who kindly provided many of the photos used in this page!
59+
60+
In some instances you will need to solder the LED matrix to the backpack. There's a fantastic [tutorial on Adafruit](https://learn.adafruit.com/adafruit-led-backpack/1-2-8x8-matrix) on how to do it.
61+
62+
{:.ui .dividing .header}
63+
### Electronics
64+
65+
A microbit edge connector is required to access the I2C pins on the microbit. These are `pin19` and `pin20` of the microbit.
66+
67+
{:.ui .basic .small .table}
68+
| Backpack Pin | Microbit Pin |
69+
|--------|----------|
70+
| `GND` | `GND` |
71+
| `VCC+` | `3V` |
72+
| `DAT` or `SDA` | `PIN 20` |
73+
| `CLK` or `SCA` | `PIN 19` |
74+
75+
The I2C naming conventions are different than usual. `SCL = CLK` & `SDA = DAT`.
76+
77+
#### I2C Addresses
78+
79+
The I2C bus is accessed through `pin19` and `pin20`. There are already components on the bus. These are:
80+
81+
{:.ui .basic .small .table}
82+
| Device | I2C Address |
83+
|--------|----------|
84+
| Accelerometer | `0x1D` |
85+
| Magnetometer | `0x0E` |
86+
87+
Each device on the bus must occupy a different address. The HT16K33 module has its own I2C address. The address is usually found on the datasheet or product page of the component. On the Adafruit backpack it's possible to [change the I2C address](https://learn.adafruit.com/adafruit-led-backpack/changing-i2c-address) if required.
88+
89+
Alternatively, the microbit's I2C bus can be scanned for devices. There is a [guide to scanning the I2C bus on this website](/howto/scan-i2c-bus).
90+
91+
{:.ui .dividing .header}
92+
### Code
93+
94+
[Radomir Dopieralski](https://bitbucket.org/thesheep/) has written a Python module to control HT16K33-based backpacks. [Download the HT16k33 driver](https://bitbucket.org/thesheep/microbit-ht16k33/raw/a5b7961f0b57ba226ab98edc2c5f8d95d8954d00/ht16k33.py) and save it to your computer.
95+
96+
Upload the saved module to the microbit in mu. See [here for tutorial on how to add modules](/howto/add-python-module-microbit-micropython).
97+
98+
{% highlight python %}
99+
from microbit import i2c, sleep
100+
101+
from ht16k33 import Matrix8x8
102+
103+
# address of HT16K33 is 0x70
104+
display = Matrix8x8(i2c, address=0x70)
105+
106+
# Run through each pixel individually and turn it on.
107+
for x in range(8):
108+
for y in range(8):
109+
display.pixel(x, y, 1)
110+
display.show()
111+
sleep(50)
112+
113+
# Clear the display
114+
display.fill(0x00)
115+
display.show()
116+
sleep(1000)
117+
118+
# Fill display
119+
display.fill(0xff)
120+
display.show()
121+
sleep(1000)
122+
123+
# Cycle through the brightness levels
124+
for x in range(15):
125+
display.brightness(x)
126+
sleep(50)
127+
128+
{% endhighlight %}
129+
130+
<div class="ui message">
131+
<div class="header">
132+
Alternative: MAX7219 LED Driver
133+
</div>
134+
<p>The MAX7219 is an alternative driver that uses SPI. Multiple LED displays can be linked together. I've not managed to slim a module down yet for the microbit. </p>
135+
</div>

0 commit comments

Comments
 (0)