Adafruit LED Backpack: the HT16K33 seven-segment display
Adafruit’s LED backpacks put an HT16K33 driver behind a display so it needs only the two I2C wires. The library has a class for each kind: Adafruit_7segment for the four-digit clock display, Adafruit_8x8matrix and Adafruit_BicolorMatrix for square grids, Adafruit_24bargraph and Adafruit_AlphaNum4 for fourteen-segment letters. Several backpacks can share one bus at addresses 0x70 to 0x77.
Built in on Mokxi: #include <Adafruit_LEDBackpack.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit LED Backpack Library does
Nothing you write reaches the display at once. print(), writeDigitRaw() and drawColon() change a buffer in the board’s RAM, and writeDisplay() sends the whole buffer in one I2C transfer. On the seven-segment backpack the buffer has five positions: 0 and 1 are the left digits, 2 is the colon, and 3 and 4 are the right digits, which is why raw writes skip position 2.
Mokxi’s HT16K33 part is the four-digit seven-segment backpack. The matrix, bar graph and alphanumeric classes compile and send the chip the same bytes the real library would, but no part draws them as a grid or as letters. The runnable project is an air quality monitor that shows CO2, temperature and humidity on the seven-segment display.
| Library Manager name | Adafruit LED Backpack Library |
|---|---|
| Author | Adafruit |
| Include | #include <Adafruit_LEDBackpack.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Runs, with a gap the note below names. |
| Parts it drives | Four-digit display, HT16K33 backpack |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Adafruit_LEDBackpack.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit LED Backpack Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit LED Backpack Library to lib_deps in PlatformIO. Mokxi follows the calls of release 1.5.1, so a sketch written here builds there too.
What to know in the simulator: Mokxi’s HT16K33 part is the seven-segment backpack: the matrix, bar graph and alphanumeric classes send the chip what upstream sends, and no part draws them. The fourteen-segment letters are Mokxi’s own.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Adafruit_7segment display = Adafruit_7segment()
Makes the four-digit seven-segment backpack. It inherits Print, so print() takes whole numbers, floats and text.
display.begin(0x70)
Starts the oscillator, turns the display on without blinking and sets full brightness. 0x70 is the address with no jumpers bridged.
display.print(1234, DEC)
Right-aligns a number in the four digits, in any base up to 16. A number that does not fit shows four dashes.
display.writeDigitRaw(3, 0x39)
Lights segments directly at one position, segment A in bit 0 to G in bit 6 and the point in bit 7. 0x39 is a C, useful for units like degrees C.
display.drawColon(true)
Turns the center colon on or off for a clock layout.
display.writeDisplay()
Sends the buffer to the chip. Call it after every change you want to see.
display.print(hours * 100 + minutes, DEC);
display.drawColon(seconds % 2 == 0);
display.writeDisplay();display.setBrightness(10)
Brightness from 0 to 15, in sixteen steps of the chip’s own dimming.
display.blinkRate(HT16K33_BLINK_1HZ)
Makes the chip blink the whole display by itself: HT16K33_BLINK_OFF, _2HZ, _1HZ or _HALFHZ. Good for an alarm that needs no code in loop().
Example: Air quality monitor
On the Arduino Uno R3. It runs in the browser exactly as printed.
// Air quality monitor: an SGP30 gas sensor, an SHT31 thermometer and an
// HT16K33 four-digit LED display share the I2C bus, the way a home-built CO2
// monitor is made.
//
// SGP30, SHT31, display SDA -> A4 SCL -> A5 VIN or VCC -> 5 V GND -> GND
// red LED pin 8 through 220 ohms
//
// The SGP30 reads 400 ppm and 0 ppb for its first 15 seconds while its hot
// plate warms up, so the display counts that down after "--". Then it takes
// turns: the CO2eq in ppm for four seconds, the temperature ("23*C") and the
// humidity ("45rh") for one each. Above 1000 ppm, where a room wants a
// window opening, the red LED comes on and the display blinks.
//
// The SHT31's humidity makes the SGP30 more accurate: the sensor wants it as
// milligrams of water in a cubic meter of air, which is worked out here from
// a table of how much water air can hold, because this board has no exp().
//
// Drag the sliders on the two sensors and watch the display follow.
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_SHT31.h"
#include "Adafruit_SGP30.h"
const int ALERT_LED = 8;
const uint16_t ALERT_PPM = 1000;
// Segments for the units: A is bit 0, G is bit 6.
const uint8_t DEGREE = 0x63;
const uint8_t LETTER_C = 0x39;
const uint8_t LETTER_R = 0x50;
const uint8_t LETTER_H = 0x74;
Adafruit_7segment display = Adafruit_7segment();
Adafruit_SHT31 sht31 = Adafruit_SHT31();
Adafruit_SGP30 sgp;
// How much water saturated air holds, in mg per cubic meter, at 0, 5, 10 ...
// 50 degrees Celsius.
const uint32_t SATURATION[] = {4847, 6797, 9399, 12830, 17300, 23050,
30380, 39630, 51120, 65330, 82770};
uint32_t absoluteHumidity(int32_t centiC, int32_t centiRh) {
if (centiC < 0) centiC = 0;
if (centiC > 4999) centiC = 4999;
uint8_t i = centiC / 500;
uint32_t frac = centiC % 500;
uint32_t sat = SATURATION[i] + (SATURATION[i + 1] - SATURATION[i]) * frac / 500;
return sat * (uint32_t)centiRh / 10000;
}
// Two digits on the left, two unit segments on the right.
void showWithUnit(int32_t value, uint8_t left, uint8_t right) {
display.print(value * 100, DEC);
display.writeDigitRaw(3, left);
display.writeDigitRaw(4, right);
}
unsigned long started;
uint8_t tick = 0;
bool alarm = false;
void setup() {
pinMode(ALERT_LED, OUTPUT);
display.begin(0x70);
display.setBrightness(10);
if (!sht31.begin(0x44) || !sgp.begin()) {
// "Err": a sensor did not answer.
display.writeDigitRaw(0, 0x79);
display.writeDigitRaw(1, LETTER_R);
display.writeDigitRaw(3, LETTER_R);
display.writeDisplay();
while (1) delay(10);
}
started = millis();
}
void loop() {
bool climate = sht31.measure();
int32_t centiC = sht31.getTemperatureCentis();
int32_t centiRh = sht31.getHumidityCentis();
if (climate) sgp.setHumidity(absoluteHumidity(centiC, centiRh));
if (!sgp.IAQmeasure()) {
delay(1000);
return;
}
unsigned long seconds = (millis() - started) / 1000;
tick = (tick + 1) % 6;
if (seconds < 15) {
display.print(15 - seconds, DEC);
display.writeDigitRaw(0, 0x40);
display.writeDigitRaw(1, 0x40);
} else if (tick == 4) {
showWithUnit((centiC + 50) / 100, DEGREE, LETTER_C);
} else if (tick == 5) {
showWithUnit((centiRh + 50) / 100, LETTER_R, LETTER_H);
} else {
display.print(sgp.eCO2, DEC);
}
display.writeDisplay();
bool high = seconds >= 15 && sgp.eCO2 >= ALERT_PPM;
if (high != alarm) {
alarm = high;
display.blinkRate(alarm ? HT16K33_BLINK_1HZ : HT16K33_BLINK_OFF);
}
digitalWrite(ALERT_LED, alarm ? HIGH : LOW);
// One measurement a second, as the SGP30's baseline algorithm wants.
delay(1000 - 70);
}Open it in the editor, wired and ready to run · Air quality monitor: the project page
Common errors and how to fix them
The display stays dark although print() runs
writeDisplay() is missing. The library only changes its buffer until that call sends it, so add display.writeDisplay() after the last change of each update.
A raw segment pattern shows up in the wrong digit
Position 2 is the colon, not a digit. The four digits are positions 0, 1, 3 and 4, so the rightmost digit is writeDigitRaw(4, ...).
Two backpacks on the bus show the same thing
Both are at 0x70. Bridge an address jumper on one of them to move it to 0x71 or higher, then call begin() with that address for the second object.
Keep going
Learn
More displays libraries
Try Adafruit LED Backpack Library without installing anything
Open the example, change the sketch, and keep your version in a free account.