Adafruit 7-segment LED matrix backpacks

Adafruit offers a broad group of LED products that all communicate by I2C using a LED matrix backpack.   Based on the HT16K33, these products are backed by an easy to use library from Adafruit.   See the full Adafruit tutorial.   The code presented here is focused on the 7-Segment displays that are ideal for a four digit clock or numeric display.   I took their examples and created a demo that fullyd emonstrates each of the functions in the Adafruit library.  

////////////////////////////////////////////////////////////////////////
// Adafruit 7-segment LED matrix backpacks
// Demonstrates how to use all of the Adafruit library functions.
// Mark Kiehl
//
// Portions derived from Adafruit examples.
/*************************************************** 

  Adafruit 7-segment LED matrix backpacks
  HT16K33

  AF #865; 0.56"; 7-seg clock display; + #877 backpack
  AF #1264; 1.2"; 7-seg clock display; + #1271 backpack

  This is a library for our I2C LED Backpacks

  Adafruit tutorial:
  https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack

  Designed specifically to work with the Adafruit LED 7-Segment backpacks 
  ----> http://www.adafruit.com/products/881
  ----> http://www.adafruit.com/products/880
  ----> http://www.adafruit.com/products/879
  ----> http://www.adafruit.com/products/878

  These displays use I2C to communicate, 2 pins are required to 
  interface. There are multiple selectable I2C addresses. For backpacks
  with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
  with 3 Address Select pins: 0x70 thru 0x77

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

// Enable one of these two #includes and comment out the other.
// Conditional #include doesn't work due to Arduino IDE shenanigans.
#include  // Enable this line if using Arduino Uno, Mega, etc.
//#include  // Enable this line if using Adafruit Trinket, Gemma, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment();
bool blinkColon = false;
int hours = 0;
int minutes = 0;
int seconds = 0;
////////////////////////////////////////////////////////////////////////

void setup() {
  
  //Adafruit 7-segment backpack
  #ifndef __AVR_ATtiny85__
    Serial.begin(9600);
    Serial.println("7 Segment Backpack Test");
  #endif
  matrix.begin(0x70);

  MatrixTest();

  //print a decimal number
  matrix.clear();
  matrix.print(3456,DEC);
  matrix.writeDisplay();
  delay(5000);

  //print a hexadecimal number
  matrix.clear();
  matrix.print(0xBEEF,HEX);
  matrix.writeDisplay();
  delay(5000);

  //try to print a number thats too long
  //result is "----"
  matrix.clear();
  matrix.print(12345, DEC);
  matrix.writeDisplay();
  delay(5000);

  //Draw a raw 8-bit mask using writeDigitRaw(location,bitmask);
  //The segments are labelded A to G.  These correspond to bits
  //in the bitmask, starting with A as the low-order bit.
  //Bitmasks can be specified as 8-bit binary numbers using
  //the "B" notation.  To turn on segment A (the top segment) in
  //the first position, you would call:
  matrix.clear();
  matrix.writeDigitRaw(0,B00000001);
  matrix.writeDisplay();
  delay(5000);
  //To turn on segments C,G,E, and F (to make an 'h')
  //you would call:
  matrix.clear();
  matrix.writeDigitRaw(0,B01110100);
  matrix.writeDisplay();
  delay(5000);
  

  // writeDigitNum(location, number) - this will write the number (0-9) to a single location. 
  // Location #0 is all the way to the left
  // Location #2 is the colon dots;  use drawColon(true or false)
  // Location #4 is all the way to the right.
  // Write the time 23:40
  matrix.clear();
  matrix.writeDigitNum(0,2);
  matrix.writeDigitNum(1,3);
  matrix.drawColon(true);
  matrix.writeDigitNum(3,4);
  matrix.writeDigitNum(4,5);
  matrix.writeDisplay();
  delay(5000);

  //Note missing digit at position 0
  //The .clear() insures position 0 does not still have last value.
  //Note that the colon is not visible either.
  matrix.clear();
  matrix.print(2.34);
  matrix.setBrightness(7);
  matrix.writeDisplay();
  delay(5000);

  // Must execute .writeDisplay() after .clear()
  matrix.clear();
  matrix.writeDisplay();
  delay(5000);
  
  matrix.writeDisplay();
  matrix.writeDigitNum(0,9);
  matrix.writeDigitNum(1,8);
  matrix.drawColon(false);
  matrix.writeDigitNum(3,7);
  matrix.writeDigitNum(4,6);
  matrix.writeDisplay();
  delay(5000);

  // change the brightness; 0 = low, 15 = bright
  matrix.setBrightness(0);
  delay(2000);

  // change the brightness; 0 = low, 15 = bright
  matrix.setBrightness(15);
  delay(2000);

  // change the brightness; 0 = low, 15 = bright
  matrix.setBrightness(7);
  delay(2000);

  // blink at rate = 1 (fastest)
  matrix.blinkRate(1);
  delay(5000);

  // blink at rate = 2
  matrix.blinkRate(2);
  delay(5000);

  // blink at rate = 3  (slowest)
  matrix.blinkRate(3);
  delay(5000);

  // blink at rate = 0 (no blink)
  matrix.writeDisplay();
  matrix.writeDigitNum(0,4);
  matrix.writeDigitNum(1,5);
  matrix.drawColon(false);
  matrix.writeDigitNum(3,6);
  matrix.writeDigitNum(4,7);
  // matrix.writeDisplay(); not needed when .blinkRate() is used
  matrix.blinkRate(0);
  delay(5000);

  //writeDigitNum(location, number, true) will paint the decimal point. 
  matrix.writeDisplay();
  matrix.writeDigitNum(0,8,true);
  matrix.writeDigitNum(1,7,true);
  matrix.writeDigitNum(3,6,true);
  matrix.writeDigitNum(4,5,true);
  matrix.writeDisplay();
  delay(5000);
    
  //writeDigitNum(location, number, true) will paint the decimal point. 
  matrix.writeDisplay();
  matrix.writeDigitNum(0,8,false);
  matrix.writeDigitNum(1,7,false);
  matrix.writeDigitNum(3,6,false);
  matrix.writeDigitNum(4,5,false);
  matrix.writeDisplay();
  delay(5000);
}

void loop() {

  // blink the colon every 1 second.
  blinkColon = !blinkColon;
  matrix.drawColon(blinkColon);
  matrix.writeDisplay();
  delay(1000);

} // loop()

void MatrixTest(){
  // A quick test that the 7-segment LED is working.
  uint8_t counter = 0;
  for (uint8_t d=0; d<25; d++) {
    // paint one LED per row. The HT16K33 internal memory looks like
    // a 8x16 bit matrix (8 rows, 16 columns)
    for (uint8_t i=0; i<8; i++) {
      // draw a diagonal row of pixels
      matrix.displaybuffer[i] = _BV((counter+i) % 16) | _BV((counter+i+8) % 16)  ;
    }
    // write the changes we just made to the display
    matrix.writeDisplay();
    delay(100);
    counter++;
    if (counter >= 16) counter = 0;  
    delay(1);
  }
  // Must execute .writeDisplay() after .clear()
  matrix.clear();
  matrix.writeDisplay();
}

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.