|
| 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 | +{:.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 | +{:.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 | + |
| 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 | + |
| 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 | + |
| 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 %} |
0 commit comments