Using an Adafruit 7-Segment Display with I2C Backpack

I recently used an Adafruit 4 character, 7-Segment display with I2C backpack on a project, and had a hard time finding good info on how to address it for my needs. So based on what I learned, I thought I would make a post for anyone else who may be looking.

I used it with a project where I modified a guitar effect pedal to have MIDI control, and needed something to use as a display for a breakout box.

The Adafruit Display seemed perfect for my needs, although trying to figure out how to display the info I wanted was difficult.

To use one of these, you need two libraries:

  • Adafruit_GFX.h
  • Adafruit_LEDBackpack.h

Both of these libraries were easy to find, but information on how to write code to display exactly what I wanted wasn't so easy.

Essentially, how this works is you send data to a buffer, then give a command to write the buffer to the display. The display will hold whatever you send it, until you send something else from the buffer.

After adding the #include lines to call the libraries above, a few additional things need to be added to the code:

Adafruit_7segment matrix = Adafruit_7segment();  // Not sure what this does, but apparently it's needed
matrix.begin(0x70);                                          // Starts the display driver

The "0x70" relates to the I2C address of the backpack, and is the default address. This address is configurable, by connecting various solder pads on the backpack itself. Buth that info is readily available and can be found easily with a Google search.

After having the above in your code, the display is ready to be used.

So, as stated above, you send data to the buffer, then write the buffer to the display. Here is the command to write the buffer out to the display:

matrix.writeDisplay();   // Writes the display buffer to the physical display

So to send data to the buffer, there are a couple of options, but here are the two I found most useful:

matrix.print(Value-To-Print);                                  // Sends data to the display buffer
matrix.writeDigitRaw(address, segments-to-display);  // Sends data to the display buffer

the matrix.print() command can be used with a decimal value, or a variable, or even a HEX value. I primarily used it with variables containing decimal values.

The matrix.writeDigitRaw() command is structured with the 1st argument being the character location to write to, and the 2nd argument the value you want to write.

The decimal point is value 0x80, and the character positions are 0, 1, 3, 4. Position 2 is the colon in the middle.

What I've noticed is that the matrix.print() overwrites the buffer with new data, and matrix.writeDigitRaw() adds additional info the buffer. So, for example, if you want to send the number "62" to the display, and want the 2nd place decimal point to show, you can do it like this:

matrix.print(62);                             // Send the decimal '62' to the display buffer
matrix.writeDigitRaw(1, 0x80);           // Send - character position 1, data '0x80' - to the display buffer.
matrix.writeDisplay();                       // Write the display buffer to the physical display

If you reversed the matrix.print() and matrix.DigitRaw() order, then you would only get "62", since the matrix.print() command would clear the buffer, hence losing the 0x80 value.

Because I wanted to display numbers and some special characters, I frequently used matrix.print() and matrix.writeDigitRaw() together. It was difficult to find the "Raw" values to send until I figured out how this works.

So here is the breakdown of the individual segments:

So to display the center line only, send the value "64" with the matrix.writeDigitRaw command. To display multiple segments using "Raw", just add the values in the above image together.

So if you wanted to display the letter 'E' for example, add up the values:

and you get 121.

So the following would display 'E' in the 1st character position, a decimal in the 2nd character position, and '33' in the last two character positions:

matrix.print(33);                             // Send the decimal '33' to the display buffer
matrix.writeDigitRaw(0, 121);          // Send - character position 1, data value '121' - to the display buffer 
matrix.writeDigitRaw(1, 0x80);        // Send - character position 2, data '0x80' - to the display buffer.
matrix.writeDisplay();                     // Write the display buffer to the physical display

There are two more commands I used as well:

  matrix.setBrightness(number);        // Set brightness for display as whatever the variable 'number' holds
  matrix.clear();                                // Clear the display buffer

The matrix.setBrightness() command is used to set the brightness, and any value from 0 - 15 is valid. It has 16 steps of brightness.

The matrix.clear() command clears the display buffer out, which is important if you want to do something like flash the display a set number of times as user feedback.

So there it is, short and sweet. Hopefully this helps someone else in the future.

You might consider putting this in the playground section of the website or at least putting a link to this post on their.

Sure Mike, I added a link to this post in the "Hardware | Output | LED Displays | Seven Segment" section of the playground.

Thank you so much for these essential programming tips. Adafruit should add this information to their backpack write up. It would save customers a lot of frustration.

thx men

eBay prices at least, appear to have at least doubled recently on these modules!