Mel Lester Jr
Published © GPL3+

7 Segment LED Displays 101 - How To Make One Work

Seven Segment LED Displays are everywhere and are available in many configurations. This tutorial will show you how they work.

BeginnerProtip2 hours35,857
7 Segment LED Displays 101 - How To Make One Work

Things used in this project

Hardware components

SparkFun RedBoard
SparkFun RedBoard
I just happen to be using a RedBoard, but any Arduino compatible microcontroller board with at least 9 Digital I/O pins will work
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Single Digit 7 Segment LED Display
Almost any single digit 7 Segment LED Display you have.
×1
Resistor 330 ohm
Resistor 330 ohm
×8
AA Batteries
AA Batteries
You may need a battery holder or use a Benchtop Supply instead.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

SevenSegmentLEDdisplays101.ino

Arduino
Sketch to demonstrate how 7 Segment LED Displays works.
//**************************************************************//
//  Name    : SevenSegmentLEDdisplays101.ino                                
//  Author  : Mel Lester Jr. 
//  Date    : 2017-02-11
//  Modified:                                  
//  Version : 0.1                                             
//  Notes   : Sketch to demonstrate how 7 Segment LED Displays works. 
//          : This is intended as a Tutorial, not a production quality
//          : example.  No attempt has been made to optomize the code.                          
//****************************************************************

// Globals
/* Arduino Uno/Nano I/O pin assignments, You may change these
 *  but the program logic expects these pins to be in an unbroken 
 *  numerical sequence if the eight segments are to light correctly */
const int segA = 4;
const int segB = 5;
const int segC = 6;
const int segD = 7;
const int segE = 8;
const int segF = 9;
const int segG = 10;
const int segP = 11;  // Decimal point or Dp

// uncomment one of the following lines that describes your display
const char common = 'a';    // common anode
//const char common = 'c';    // common cathode

bool decPt = true;  // decimal point display flag

void setup() {
  // initialize I/O pins used in this sketch
  for (int i = segA; i <= segP; i++) {
    pinMode(i, OUTPUT);
    if (common == 'c') {  // test light every segment
      digitalWrite(i, HIGH);    // display is common cathode 
    } else {
      digitalWrite(i, LOW);     // common anode display
    }  
    delay(500);
  }
}

void loop() {
  decPt = !decPt; // display decimal point every other trip through loop
  
  // generate characters to display for hexidecimal numbers 0 to F
  for (int i = 0; i <=15; i++) {
    byte bits = myfnNumToBits(i) ;
    if (decPt) {
      bits = bits | B00000001;
    }
    myfnUpdateDisplay(bits);
    delay(750);
  }
}

void myfnUpdateDisplay(byte eightBits) {
  int thisBit = 7;  // eight bits in a byte counting from 0
  for (int i = segA; i <= segP; i++) {
    byte lightSegment = bitRead(eightBits, thisBit--);
    if (common == 'a') {
      digitalWrite(i, !lightSegment); 
    } else {
      digitalWrite(i, lightSegment);   
    }
  }
}

byte myfnNumToBits(int someNumber) {
  switch (someNumber) {
    case 0:
      return B11111100;
      break;
    case 1:
      return B01100000;
      break;
    case 2:
      return B11011010;
      break;
    case 3:
      return B11110010;
      break;
    case 4:
      return B01100110;
      break;
    case 5:
      return B10110110;
      break;
    case 6:
      return B00111110;
      break;
    case 7:
      return B11100000;
      break;
    case 8:
      return B11111110;
      break;
    case 9:
      return B11100110;
      break;
    case 10:
      return B11101110; // Hexidecimal A
      break;
    case 11:
      return B00111110; // Hexidecimal B
      break;
    case 12:
      return B10011100; // Hexidecimal C or use for Centigrade
      break;
    case 13:
      return B01111010; // Hexidecimal D
      break;
    case 14:
      return B10011110; // Hexidecimal E
      break;
    case 15:
      return B10001110; // Hexidecimal F or use for Fahrenheit
      break;  
    default:
      return B10010010; // Error condition, displays three vertical bars
      break;   
  }
}

SevenSegmentLEDdisplays101.ino

Arduino
Sketch to demonstrate how 7 Segment LED Displays works.
//**************************************************************//
//  Name    : SevenSegmentLEDdisplays101.ino                                
//  Author  : Mel Lester Jr. 
//  Date    : 2017-02-11
//  Modified:                                  
//  Version : 0.2                                             
//  Notes   : Sketch to demonstrate how 7 Segment LED Displays works. 
//          : This is intended as a Tutorial, not a production quality
//          : example.  No attempt has been made to optomize the code.                          
//****************************************************************

// Globals
/* Arduino Uno/Nano I/O pin assignments, You may change these
 *  but the program logic expects these pins to be in an unbroken 
 *  numerical sequence if the eight segments are to light correctly */
const int segA = 4;
const int segB = 5;
const int segC = 6;
const int segD = 7;
const int segE = 8;
const int segF = 9;
const int segG = 10;
const int segP = 11;  // Decimal point or Dp

// uncomment one of the following lines that describes your display
const char common = 'a';    // common anode
//const char common = 'c';    // common cathode

bool decPt = true;  // decimal point display flag

void setup() {
  // initialize I/O pins used in this sketch
  for (int i = segA; i <= segP; i++) {
    pinMode(i, OUTPUT);
    if (common == 'c') {  // test light every segment
      digitalWrite(i, HIGH);    // display is common cathode 
    } else {
      digitalWrite(i, LOW);     // common anode display
    }  
    delay(500);
  }
}

void loop() {
  decPt = !decPt; // display decimal point every other trip through loop
  
  // generate characters to display for hexidecimal numbers 0 to F
  for (int i = 0; i <=15; i++) {
    byte bits = myfnNumToBits(i) ;
    if (decPt) {
      bits = bits | B00000001;
    }
    myfnUpdateDisplay(bits);
    delay(750);
  }
}

void myfnUpdateDisplay(byte eightBits) {
  int thisBit = 7;  // eight bits in a byte counting from 0
  for (int i = segA; i <= segP; i++) {
    byte lightSegment = bitRead(eightBits, thisBit--);
    if (common == 'a') {
      digitalWrite(i, !lightSegment); 
    } else {
      digitalWrite(i, lightSegment);   
    }
  }
}

byte myfnNumToBits(int someNumber) {
  switch (someNumber) {
    case 0:
      return B11111100;
      break;
    case 1:
      return B01100000;
      break;
    case 2:
      return B11011010;
      break;
    case 3:
      return B11110010;
      break;
    case 4:
      return B01100110;
      break;
    case 5:
      return B10110110;
      break;
    case 6:
      return B10111110;
      break;
    case 7:
      return B11100000;
      break;
    case 8:
      return B11111110;
      break;
    case 9:
      return B11110110;
      break;
    case 10:
      return B11101110; // Hexidecimal A
      break;
    case 11:
      return B00111110; // Hexidecimal B
      break;
    case 12:
      return B10011100; // Hexidecimal C or use for Centigrade
      break;
    case 13:
      return B01111010; // Hexidecimal D
      break;
    case 14:
      return B10011110; // Hexidecimal E
      break;
    case 15:
      return B10001110; // Hexidecimal F or use for Fahrenheit
      break;  
    default:
      return B10010010; // Error condition, displays three vertical bars
      break;   
  }
}

Credits

Mel Lester Jr

Mel Lester Jr

5 projects • 10 followers
Long-time UNIX System Administrator and embedded software developer.

Comments