7-Segment LED

The first mildly complicated thing to do is set up a single digit readout. This will be the foundation for many more digits to come, but we have to start somewhere.


7-Segment LED Digit

Part# 696-LDS-HTA514RI “Lumex LED Display .56 7-seg,c/a H-Tmp 635nm Red Gry/Wh” $1.09 at Mouser (Datasheet)

It all starts with a digit to display.  I have chosen a Common Anode type because that’s what came in the Beginner Parts Kit from Sparkfun.  Common Cathode units are also available, but they work slightly differently.

Resistor Array

Part# 652-4116R-1LF-1K “Bournes Resistor Network 16pin 1Kohms Isolated Low Profile”  $0.60 at Mouser (Datasheet)

We will also need a resistor for each segment.  Rather than fiddling with 7 separate resistors, I find it handy to use an isolated resistor array.  This guy behaves just like 8 resistors next to each other.  A resistor is needed on each LED segment so you don’t blow up your LED.


Each of the seven segments comprising our digit is a separate LED.  The Arduino will turn each segment on or off so the correct digit is displayed.  To keep things simple, I choose 7 pins in a row to connect up the segments.  Pin #3 will go to the ‘G’ segment, all the way up to pin #9 to the ‘A’ segment.  Consult the data sheet to determine which pin on the display corresponds to the named segment.  Note that we connect from the Arduino to the resistor array, and then out the other side of the resistor array to the correct pin.

The “common anode” display means we connect a single pin to 5V on our Arduino.


The Arduino sketch loops through the hex numbers 0-F, displaying a new digit every 500ms.

To display a digit, we look up its value in our font table.  This will give us a byte, each bit of which corresponds to a single segment that we control with an Arduino pin.

Here is the font table:

const byte ledCharSet[] =
  // 00-0F: Hex digits
  B01111110, B00110000, B01101101, B01111001,	// 0123
  B00110011, B01011011, B01011111, B01110000,	// 4567
  B01111111, B01111011, B01110111, B00011111,	// 89AB
  B01001110, B00111101, B01001111, B01000111	// CDEF

And here is how we use it when looking it up.

  // One bit in 'pins_byte' represents a single segment
  uint8_t pins_byte = ledCharSet[current_value];

Then we will need to push each bit out to the correct pin, so we loop through the pins one at a time, and send the bit out. Each time through the loop we send the lowest bit out to the current pin, and shift the values down so the next-lowest bit is now the lowest bit.

As noted above, if the font has a ‘1’ in a segment, it must be lit. In our common-anode display, in order to light a segment, we have to connect the pin to ground, which we do by writing LOW to the pin. To keep it off, we keep it HIGH.

  // Display one bit of the pins_byte on each consecutive segment
  int pin = G_segment_pin;
  while ( pin < G_segment_pin + num_segments )
    digitalWrite( pin, (pins_byte & 1)?LOW:HIGH );
    pins_byte >>= 1;

Find the whole code on github here: https://github.com/maniacbug/Arduino/tree/master/_7_Segment_Counter

Further reading

There are lots of great resources out there for 7-Segment LED’s. Here are just a few:


Leave a comment

Filed under 7-Segment, Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s