Segment Display Tutorial for Arduino, ESP8266 and ESP32

Segment LED Display Tutorial for Arduino, ESP8266 and ESP32

In this tutorial you learn how the three most important segment LED displays work. We cover every segment display with examples and the correct wiring. The 3 different displays are:

  1. 7 Segment Display
  2. 4 7 Segment Display
  3. 8x8 Dot Matrix Display

Also this article cover the Shift Register which reduces the number of used pin on your microcontroller.

Segment LED Displays

Table of Contents

The following table gives you an overview of all components and parts that I used for this tutorial. If you want to support my work, you can buy something from the following links and I will earn a small commission. This does not affect the price you pay for the products.

If you are interested in components and parts that I used in other tutorials, visit the components and parts page.

 Arduino UnoAmazonBanggoodAliExpress
ORESP8266 NodeMCUAmazonBanggoodAliExpress
ORESP32 NodeMCUAmazonBanggoodAliExpress
AND7 Segment Display and 4x7 Segment Display in Sensor PackAmazon AliExpress
AND8x8 Dot Matrix DisplayAmazonBanggoodAliExpress

7 Segment Display

Seven segment displays are most used to display decimal numbers (0…9) in electronic displays like microwave ovens, calculators or washing machines. These kind of displays use light emitting diodes (LEDs) for every of the 7 segments. Therefore it is possible to display decimal numbers if the LEDs of the right segments are turned on.

The following picture shows the pinout of a 7 segment display to get a better understanding how it works.

7 Segment Display Pinout

The different segments are connected with the corresponding pins on the edge or backside of the display. The 2 middle pins are the common pins and connected to a common cathode or common anode. This depends of the kind of 7 segment display you have. In most cases you will have a common cathode display so that an LED turns on when the signal to the LED is HIGH and to common is connected to ground. If you have an anode segment display the LED turns on when the signal is LOW.

Note that your display might have a dot point on the right side so that in total you have 10 pins to connect to your microcontroller.

  • 1 pin for the dot pin
  • 7 pins for the different segments to display numbers
  • 2 pins to connect the anode or cathode

The following fritzing sketches shows the connection of the 7 segment LED display to an Arduino and an ESP8266.

The 220 Ohm resistor between the common pin and ground restricts the current to less than 20mA on the Arduino.

To display different kinds of numbers on the segment display it is useful to have a table for all numbers. Therefore the following table shows you the Bit and HEX Code for the anode and cathode configuration.

Anode configuration of display numbers
on a 7 segment display

Cathode configuration of display numbers
on a 7 segment display

In the example we want to display some values. I used to display the numbers 1 and 8 in a loop. With the provided table you can easily change the following sketch to display other digital numbers.

int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 6;
int pinE = 7;
int pinF = 9;
int pinG = 8;

int LEDs[] = {8,9,7,6,4,3,2};        // define LED pins
int one[] = {0, 0, 0, 0, 1, 1, 0};   // LED states to display number one
int eight[] = {1, 1, 1, 1, 1, 1, 1}; // LED states to display number eight


void setup() {
  for (int i = 0; i<7; i++) pinMode(LEDs[i], OUTPUT);
}

void loop() {
  // display number one
  for (int i = 0; i<7; i++) digitalWrite(LEDs[i], one[i]);
  delay(1000);
  // display number eight
  for (int i = 0; i<7; i++) digitalWrite(LEDs[i], eight[i]);
  delay(1000);
}

4x7 Segment Display

The 4 times 7 Segment Display is easy explained. It is 4 times the 7 segment display in a row. Therefore a lot of wiring is necessary. For me personally if the wiring gets to complicated, I try to find an other way to get to my goal. And for me I prefer an LCD display which is easy to wire with I2C instead of the complicated use and blocking pins of the 4 7 segment display. If you want to learn how to use the LCD display, I also have a tutorial for LCD displays.

Reduce the number of pins with Shift Registers

Also there is a possibility to reduce the number of pins you need to connect the segment displays to your microcontroller. With the use of shift registers like the 74HC595 it is possible to declare the status of each LED with one 8-bit number rather than use one bit for every LED. With the use of a shift register you can reduce the number of pins from 8 to only 3.

You can view the datasheet of the 74HC595 under this link. The operating voltage is between 2V and 6V. Therefore the 74HC595 can be used with all Arduino and all ESP8266 micocontroller. Also the power consumption is low with 80 mycro A and the shift register can directly clear the complete output.

The following table shows the connection between the 74HC595 and the 7 segment display.

SymbolDescriptionConnection ArduinoConnection ESP8266
QBShift register output for LED b7 segment display Pin b7 segment display Pin b
QCShift register output for LED c7 segment display Pin c7 segment display Pin c
QDShift register output for LED d7 segment display Pin d7 segment display Pin d
QEShift register output for LED e7 segment display Pin e7 segment display Pin e
QFShift register output for LED f7 segment display Pin f7 segment display Pin f
QGShift register output for LED g7 segment display Pin g7 segment display Pin g
QHShift register output for LED P  
GNDGroundGNDGND
QHOutput if more  
SRCLRClear the register when LOW  
SRCLKStorage register clockPin 2 CLOCKPin 15 (D8) SPI Clock
RCLKShift register clockPin 3 LATCHPin 13 (D7) SPI Data MISO
OEOutput enable when groundGNDGND
SERSerial input for next pinPin 4 DATAPin 12 (D6) SPI Data MOSI
QAShift register output for LED a7 segment display Pin a7 segment display Pin a
VCC5V supply5VVIN

If you want to know more about the pinout of different microcontroller, I wrote an article about the different pinouts of EPS8266 boards, Arduino Nano, Arduino Uno, Arduino Mega.

The function of a shift register is the following:

  • The shift register loads the state (HIGH or LOW) of each LED one by one with the data pin (SER or DATA) as long as the clock (RCLK or LATCH) is set to LOW.
  • The loading of each LED is controlled by the storage register clock (SRCLK or CLOCK).
  • After all 8 LEDs states are loaded, the shift register clock (RCLK or LATCH) is set from LOW to HIGH.


For the following sketch we have one thing left to define. The shift register has to know if want to load the most significant bit first (MSBFIRST) or last (LSBFRIST). The following tables explain the difference. We want to load the number 3 into the shift register:

MSBFIRST the number 3 in binary is 01001111

 QAQBQCQDQEQFQGQH
Clear        
Shift 1 → 0        
Shift 2 → 11       
Shift 3 → 0 1      
Shift 4 → 0  1     
Shift 5 → 11  1    
Shift 6 → 111  1   
Shift 7 → 1111  1  
Shift 8 → 11111  1 

LSBFRIST the number 3 in binary is 11110010

 QAQBQCQDQEQFQGQH
Clear        
Shift 1 → 11       
Shift 2 → 111      
Shift 3 → 1111     
Shift 4 → 11111    
Shift 5 → 0 1111   
Shift 6 → 0  1111  
Shift 7 → 11  1111 
Shift 8 → 0 1  1111

We use the MSBFIRST in the following example to display the values 0 to 4. Note that you can either define the decimal number in binary or HEX format. I used the HEX format in the following sketch.

int clockPin = 2;
int latchPin = 3;
int dataPin = 4;

int num[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66};


void setup() {
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  for (int i = 0; i<5; i++) // range of i = #num
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, num[i]);
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }
}

8x8 Dot Matrix Display

The 8x8 Dot Matrix Display has in total 64 LEDs and normally 16 pins. But I bought the module version of the Dot Matrix Display, so that shift register and everything is connected right to the display and there are only 5 pins left to connect to your microcontroller. The following tables shows the connection between 8x8 Dot Matrix Display in the module version and the Arduino or ESP8266 microcontroller.

8x8 Dot Matrix Display ModuleConnection ArduinoConnection ESP8266
VCC5VVIN
GNDGNDGND
DINPin 8Pin D7
CSPin 9Pin D8
CLKPin 10Pin D5

The module has the same connection on both sides so that you are able to connect multiple 8x8 Dot Matrix Display modules in a row.
In the following picture you see that the module that I use has only 5 connection pins instead of 6 pins in the fritzing sketch. Therefore the last pin in the fritzing sketch is not connected to the microcontroller.

8x8 Dot Matrix Display module

The easiest way to control the display is with the LedControl library. If you do not know how to install an external library to your Arduino IDE, here is a step by step tutorial.

In the following example we display a sad, a neural and a happy smiley. First we define your LEDs in binary format which should turn on if we want to display the smiley. Then the LedControl library is configured and we set a medium brightness. The display of the smileys is done in the function drawFaces which runs in a loop.

#include "LedControl.h"
#include "binary.h"

/*
 DIN connects to pin 8
 CLK connects to pin 10
 CS connects to pin 9 
*/
LedControl lc=LedControl(8,10,9,1);

// delay time between faces
unsigned long delaytime=1000;

// happy face
byte hf[8]= {B00111100,B01000010,B10100101,B10000001,B10100101,B10011001,B01000010,B00111100};
// neutral face
byte nf[8]={B00111100, B01000010,B10100101,B10000001,B10111101,B10000001,B01000010,B00111100};
// sad face
byte sf[8]= {B00111100,B01000010,B10100101,B10000001,B10011001,B10100101,B01000010,B00111100};

void setup() {
  lc.shutdown(0,false);
  // Set brightness to a medium value
  lc.setIntensity(0,5);
  // Clear the display
  lc.clearDisplay(0);  
}

void drawFaces(){
  // Display sad face
  lc.setRow(0,0,sf[0]);
  lc.setRow(0,1,sf[1]);
  lc.setRow(0,2,sf[2]);
  lc.setRow(0,3,sf[3]);
  lc.setRow(0,4,sf[4]);
  lc.setRow(0,5,sf[5]);
  lc.setRow(0,6,sf[6]);
  lc.setRow(0,7,sf[7]);
  delay(delaytime);
  
  // Display neutral face
  lc.setRow(0,0,nf[0]);
  lc.setRow(0,1,nf[1]);
  lc.setRow(0,2,nf[2]);
  lc.setRow(0,3,nf[3]);
  lc.setRow(0,4,nf[4]);
  lc.setRow(0,5,nf[5]);
  lc.setRow(0,6,nf[6]);
  lc.setRow(0,7,nf[7]);
  delay(delaytime);
  
  // Display happy face
  lc.setRow(0,0,hf[0]);
  lc.setRow(0,1,hf[1]);
  lc.setRow(0,2,hf[2]);
  lc.setRow(0,3,hf[3]);
  lc.setRow(0,4,hf[4]);
  lc.setRow(0,5,hf[5]);
  lc.setRow(0,6,hf[6]);
  lc.setRow(0,7,hf[7]);
  delay(delaytime);
}

void loop(){
  drawFaces();
}
8x8 Dot Matrix Display Smiley

Do you think that the way to choose the LEDs which should turn on is complicated? Yes you are right, but there is an easier way to make the program sketch. On this website you can select each LED and insert different patterns into a recorder.

The following video shows how to select different patterns. On the right side you copy the code into the following sketch which will display the record in the loop.

#include "LedControl.h"
#include "binary.h"

/*
 DIN connects to pin 8
 CLK connects to pin 10
 CS connects to pin 9 
*/
LedControl lc=LedControl(8,10,9,1);

const uint64_t IMAGES[] = {
  0x0000000000000000,
  0xff000000000000ff,
  0x8142241818244281
};
const int IMAGES_LEN = sizeof(IMAGES)/8;

void setup() {
  lc.clearDisplay(0);
  lc.shutdown(0, false);
  lc.setIntensity(0, 10);
}

void displayImage(uint64_t image) {
  for (int i = 0; i < 8; i++) { byte row = (image >> i * 8) & 0xFF;
    for (int j = 0; j < 8; j++) { lc.setLed(0, i, j, bitRead(row, j)); } } } int i = 0; void loop() { displayImage(IMAGES[i]); if (++i >= IMAGES_LEN ) {
    i = 0;
  }
  delay(200);
}
8x8 Dot LED Display
8x8 Dot Matrix Display Patterns in Record

Conclusion

In this article you learned how to use the 7 Segment Display as well as the 8x8 Dot Matrix Display. We discovered how to use the Shift Register to reduce the number of pins on your microcontroller and why you should not use the 4 7 Segment Display.
If you have any questions regarding segment displays or the sketches in this article, feel free to ask your question in the comment section below. I will answer all questions as soon as possible.

Leave A Comment