The following section will show how to control the LED backpack from the board's Python prompt / REPL. You'll walk through how to control the LED display and learn how to use the CircuitPython module built for the display.
First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
Initialization
First you'll need to initialize the I2C bus for your board. It's really easy, first import the necessary modules. In this case, we'll use board
and Seg7x4
.
Then just use board.I2C()
to create the I2C instance using the default SCL and SDA pins (which will be marked on the boards pins if using a Feather or similar Adafruit board).
Then to initialize the display, you just pass i2c
in.
When using the STEMMA QT port, some board may have an alternate I2C such as board.STEMMA_I2C().
import board from adafruit_ht16k33.segments import Seg7x4 i2c = board.I2C() display = Seg7x4(i2c)
If you bridged the address pads on the back of the display, you could pass in the address. The addresses for the HT16K33 can range between 0x70 and 0x77 depending on which pads you have bridged, with 0x70 being used if you haven't bridged any of them. For instance, if you bridge only the A0 pad, you would use 0x71
like this:
display = Seg7x4(i2c, address=0x71)
If you intend to chain multiple displays together, you will need to alter the address of subsequent boards by bridging the address pins in various combinations. If you have an unsoldered board, and a board with the A0 pad solder-bridged, you would initialisz the two displays as follows.
display = Seg14x4(i2c, address=(0x70, 0x71))
To add further displays, ensure the address is different on all of them, and initialize it the same way as above, but add more comma-separated addresses to the address=()
tuple.
Setting the Brightness
You can set the brightness of the display, but changing it will set the brightness of the entire display and not individual segments. If can be adjusted in 1/16 increments between 0 and 1.0 with 1.0 being the brightest. So to set the display to half brightness, you would use the following:
display.brightness = 0.5
Setting the Blink Rate
You can set the blink rate of the display, but changing it will set the brightness of the entire display and not individual segments. If can be adjusted in 1/4 increments between 0 and 3 with 3 being the fastest blinking. So to set the display to blink at full speed, you would use the following:
display.blink_rate = 3
Printing Text
To print text to the display, you just use the print function. For the 7-segment display, valid characters are 0-9, letters A-F, a period, and a hyphen. So if we want to print ABCD, we would use the following:
display.print("ABCD")
Printing Numbers
Printing numbers is done similar to printing text, except without the quotes, though you can still print numbers in a string as well.
display.print(1234)
display.print_hex(0x1A2B)
Setting Individual Characters
To set individual characters, you simply treat the display
object as a list and set it to the value that you would like.
display[0] = '1' display[1] = '2' display[2] = 'A' display[3] = 'B'
Setting Individual Segments
To set individual segments to turn on or off, you would use the set_digit_raw function to pass the digit that you want to change and the bitmask. This can be really useful for creating your own characters. The bitmask corresponds to the following diagram:
The bitmask is a single 8-bit number that can be passed in as a single Hexidecimal, Decimal, or binary number. This will use a couple different methods to display 8.8.EE
:
display.set_digit_raw(0, 0xFF) display.set_digit_raw(1, 0b11111111) display.set_digit_raw(2, 0x79) display.set_digit_raw(3, 0b01111001)
Filling all Segments
To fill the entire display, just use the fill() function and pass in either 0 or 1 depending on whether you want all segments off or on. For instance, if you wanted to set everything to on, you would use:
display.fill(1)
Scrolling Display Manually
If you want to scroll the displayed data to the left, you can use the scroll()
function. You can pass in the number of places that you want to scroll. The right-most digit will remain unchanged and you will need to set that manually. After scrolling, you will need to call the show function. For example if you wanted to print an A and then scroll it over to spaces, you would do the following.
display.print("A") display.scroll(2) display[3] = " " display.show()
Displaying the Colon
There are a couple of different ways to display a colon on the 7-segment display. The first and easiest way is to use the print function:
display.print("12:30")
The other way to control it is to access the colon with the colon property and set it to True
or False
:
display.colon = False
Displaying an Automatic Scrolling Marquee
To make displaying long text easier, we've added a marquee function. You just pass it the full string. Optionally, you can pass it the amount of delay between each character. This may be useful for displaying an IP address, a phone number, or other numeric data:
display.marquee('192.168.100.102... ')
By default it is 0.25 seconds, but you can change this by providing a second parameter. You can optionally pass False
for a third parameter if you would not like to have it loop. So if you wanted each character to display for half a second and didn't want it to loop, you would use the following:
display.marquee('192.168.100.102... ', 0.5, False)
Text editor powered by tinymce.