HT16K33Segment

This is a hardware driver for the Adafruit 0.56-inch 4-digit, 7-segment LED display, which is based on the Holtek HT16K33 controller. The driver communicates using I2C.

'Adafruit 0.56-inch 4-digit, 7-segment LED. Image copyright Adafruit'

Compatibility

HT16K33Segment is compatible with MicroPython and CircuitPython.

Importing the Driver

The driver comprises a parent generic HT16K33 driver and a child driver for the 7-segment display itself. All your code needs to do is import the latter:

from ht16k33segment import HT16K33Segment

You can then instantiate the driver.

You will need both the display driver file and ht16k33.py in your project folder.

Characters

The class incorporates its own (limited) character set, accessed through the following codes:

  • Digits 0 through 9: codes 0 through 9

  • Characters A through F: codes 10 through 15

  • Space character: code 16

  • Minus character: code 17

  • Degree character: code 18

Display Digits

The display’s digits are numbered 0 to 3, from left to right.

Method Chaining

Most methods return a reference to the driver instance (self) to allow method chaining with dot syntax:

Class Constructor

class HT16K33Segment(i2C_bus, i2c_address=0x70)

To instantiate an HT16K33Segment object pass the I2C bus to which the display is connected and, optionally, its I2C address if you have changed the display’s address using the solder pads on rear of the LED’s circuit board.

The passed I2C bus must be configured before the HT16K33Segment object is created.

Parameters
  • i2c_bus (I2C bus object) – The I2C bus to which the display is connected.

  • i2c_address (Integer) – An optional I2C address. Default: 0x70.

Examples

# MicroPython
from ht16k33segment import HT16K33Segment
from machine import I2C

# Update the pin values for your board
DEVICE_I2C_SCL_PIN = 5
DEVICE_I2C_SDA_PIN = 4

i2c = I2C(scl=Pin(DEVICE_I2C_SCL_PIN), sda=Pin(DEVICE_I2C_SDA_PIN))
led = HT16K33Segment(i2c)
# CircuitPython
from ht16k33segment import HT16K33Segment
import busio
import board

i2c = busio.I2C(board.SCL, board.SDA)
while not i2c.try_lock():
    pass
led = HT16K33Segment(i2c)

Class Methods

HT16K33Segment.set_brightness(brightness=15)

Set the LED’s brightness (its duty cycle). If you don’t pass a value, the method will default to maximum brightness.

Parameters

brightness (Integer) – An optional brightness value between 0 (dim) and 15 (maximum brightness). Default: 15.

Example

# Turn down the display brightness
led.set_brightness(1)

This method can be used to flash the display.

The value passed into rate is the flash rate in Hertz. This value must be one of the following values, fixed by the HT16K33 controller: 0.5Hz, 1Hz or 2Hz. You can also pass in 0 to disable flashing, and this is the default value.

Parameters

rate (Integer/Float) – The flash rate in Hertz. Default: 0.

Example

# Blink the display every second
led.set_blink_rate(1)
HT16K33Segment.set_colon(is_set=True)

Specify whether the display’s center colon symbol is illuminated (True) or not (False).

Parameters

is_set (Bool) – Is the colon set? Default: False.

Returns

The instance (self).

Example

# Set the display to --:--
led.set_char("-", 0).set_char("-", 1).set_char("-", 2).set_char("-", 3)
led.set_colon().draw()
HT16K33Segment.rotate()

Rotate the display orientation by 180 degrees. Once called, the display will remain rotated until you call rotate() again. It is expected that rotate() will be called once, right after instantiation, but it does give some scope for interesting effects.

rotate() does not update the display, only the buffer. Call draw() to refresh the LED.

Returns

The instance (self).

Example

# Present error 2 -> 'Err2'
led.set_character("E", 0).set_glyph(0x50, 1)
led.set_glyph(0x50, 2).set_number(2, 3).draw()

# Flip message upside down
led.rotate().draw()
HT16K33Segment.set_glyph(glyph, digit=0, has_dot=False)

Write a character that is not in the character set — see Characters, above — to a specified digit.

Calculate the glyph pattern value using the following chart. The segment number is the bit that must be set to illuminate it (or unset to keep it unlit):

    0
    _
5 |   | 1
  |   |
    - <----- 6
4 |   | 2
  | _ |
    3

For example, to define the letter P, we need to set segments 0, 1, 4, 5 and 6. In bit form that makes 0x73, and this is the value passed into glyph.

By default, the decimal point is not lit.

Parameters
  • glyph (Integer) – A glyph-definition pattern.

  • digit (Integer) – The digit to show the number. Default: 0.

  • has_dot (Bool) – Is the character’s decimal point set? Default: False.

Returns

The instance (self).

Example

# Set the display to --:--
led.set_char("-", 0).set_char("-", 1).set_char("-", 2).set_char("-", 3)
led.set_colon().draw()
HT16K33Segment.set_number(number, digit=0, hasDot=False)

Write a decimal number to a single digit.

By default, the decimal point is not lit.

Parameters
  • number (Integer) – The decimal number to display.

  • digit (Integer) – The digit to show the number. Default: 0.

  • has_dot (Bool) – Is the character’s decimal point set? Default: False.

Returns

The instance (self).

Example

# Display '42.42' on the LED
led.set_number(4, 0).set_number(2, 1, True)
led.set_number(4, 2).set_number(2, 3).draw()
HT16K33Segment.set_character(character, digit=0, hasDot=False)

Write a character from the display’s hexadecimal character set to a single digit.

Pass the the letter to be displayed (“0” to “9”, “A” to “F”, “-” or ” “) as a atring. You can also pass the string “deg” for a degree symbol. If you need other letters or symbols, these can be generated using set_glyph().

By default, the decimal point is not lit.

Parameters
  • character (String) – The alphanumeric character to display.

  • digit (Integer) – The digit to show the number. Default: 0.

  • has_dot (Bool) – Is the character’s decimal point set? Default: False.

Returns

The instance (self).

Example

# Display 'bEEF' on the LED
led.set_character("b", 0).set_character("e", 1)
led.set_character("e", 2).set_character("f", 3).draw()
HT16K33Segment.clear()

Wipe the class’ internal display buffer.

clear() does not update the display, only the buffer. Call draw() to refresh the LED.

Returns

The instance (self).

Example

# Clear the display
led.clear().draw()
HT16K33Segment.draw()

Call draw() after changing any or all of the internal display buffer contents in order to reflect those changes on the display itself.