title | layout | meta-description | share | author | about | cats | simple-description | date | date-updated |
---|---|---|---|---|---|---|---|---|---|
7 Segment Display with MAX7219 Backpack |
text-width-sidebar |
Using a MAX7219-driven 8-digit 7 Segment LED display module on the microbit. |
true |
jez |
Use a 7-segment display to show digits on the microbit |
external |
7-Segment Display (SPI) |
2016-12-23 10:20:00 UTC |
2016-12-23 10:20:00 UTC |
{:.ui .image .small .floated .right}
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).
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.
The microbit tells the MAX7219
chip which number to display over SPI. The chip then displays the digit.
Like the HT1633 8x8 module on this site, the LED displays are connected to a backpack containing the MAX7219
chip and wiring.
{:.ui .dividing .header}
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.
Additionally, you will need a microbit breakout board to access the SPI pins of the microbit.
{:.ui .dividing .header}
There are five wires to connect to the display module. Each of the pins do the following:
{:.ui .very .basic .table}
Pin Name | Purpose |
---|---|
VCC | for power |
GND | for ground |
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. |
DIN | Data transmitted over this wire. |
CLK | Tells the microbit when to transmit data. |
Connect the module to the microbit and its edge connector as below. Be aware the pin labels can change dependning on the manufacturer:
{:.ui .very .basic .table}
My Module Label | Possible Label Names | microbit Pin | Micropython Doc Names for pin |
---|---|---|---|
VCC | 3v | ||
GND | GND | ||
DIN | SOMI, SDI, DI, DIN, SI, MRST. | pin15 |
MOSI |
CS | nCS, CSN, nSS, STE, SYNC | pin0 |
n/a, but frequently called chip select |
CLK | SCLK | pin13 |
Serial Clock |
- Your module might also have a
DOUT
pin. Ignore it!
To connect to these pins, you will need the microbit edge connector. Here's my module wired up to my microbit:
{:.ui .dividing .header}
Download the Maxrix7seg
module for the microbit from Github.. This contains the code used to communicate with the 7-segment display.
The module is imported in the header of your Python script:
{% highlight python %} from microbit import spi
from matrix7seg import Matrix7seg
seg_display = matrix7seg(spi, pin0) {% endhighlight %}
Once your script is complete and has been flashed, the matrix7seg
module can be copied to the microbit:
- Flash your script.
- Click 'Files' within in the mu editor.
- From the
/mu_code/
directory, copy drag thematrix7seg.py
file across to the microbit. - Press reset on your microbit to reload the Python program with the
matrix7seg
module.
It's now possible to use the display module within the microbit:
{% highlight python %}
seg_display.write_number(1234)
seg_display.show() {% endhighlight %}
Each time .write_number()
is used, .show()
must be used to update the display.
{% highlight python %}
seg_display.write_number(1234) {% endhighlight %}
{% highlight python %}
seg_display.write_number(1234, zeroPad=True) {% endhighlight %}
{% highlight python %}
seg_display.write_number(1234, leftJustify=True) {% endhighlight %}
{% highlight python %}
for i in range(100) seg_display.write_number(i) seg_display.show() {% endhighlight %}
{% highlight python %}
while True: seg_display.write_number(temperature()) seg_display.show() sleep(1000) {% endhighlight %}
{% highlight python %}
from microbit import spi, accelerometer, sleep, pin0 from math import sqrt from matrix7seg import Matrix7seg
def total_acceleration(): """ return total acceleration in milli-g across all 3 axes. """ x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z()
# root of sum of squares
total = sqrt(x**2 + y**2 + z**2)
return total
highest_reading = 0
segment = Matrix7seg(spi, pin0)
while True: reading = total_acceleration() if reading > highest_reading: highest_reading = reading segment.write_number(highest_reading) segment.show() {% endhighlight %}