In this tutorial, we are going to learn how the seven-segment displays work. We will then discuss the interfacing of the 7 segment display with the Arduino Uno. Let’s start with a brief revision of LEDs and how they work. If you are well aware of how an LED works and would like to skip the revision, just click on the next suitable title from the table of content below.
Contents
How does an LED work?
An LED stands for Light Emitting Diode. It is an electrical component that gives off light when current passes through it. The light-emitting diode is a PN junction diode that is made up of semiconductor materials. It can emit light when electrons pass through the junction. To be a bit pedantic, when we apply a suitable voltage to the two leads or pins of an LED, electrons can recombine with the holes. Therefore, this transition emits energy in the form of visible photons.
LEDs can emit light in various colors depending upon the constituting semiconductor. Some possible colors are blue, green, orange and red. The latter being the most common for its clarity.
What is a seven-segment display?
Alright, with that brief intro to LEDs, we can segue right to our main topic. The seven-segment display is a bunch of eight LEDs arranged in a particular pattern. The pattern traces an outline of the digit ‘8’. Out of these eight LEDs, seven are line-shaped arranged in the shape of number eight, and one is a circular LED. The circular LED is mostly used to indicate decimal points.
Each of these eight LEDs is individually controllable via separate pins on the display module. Due to this particular arrangement, it can display numbers from 0 to 9 and alphabets from A to F. Since LEDs are cheap and easy to use, a seven-segment display is sufficient for simple projects.
Understanding the 7 segment display
To properly understand how to use a seven-segment display in tandem with an Arduino or any system for that matter, we need to understand its internal as well as external construction.
The internal circuit of a seven-segment display
Study the internal circuitry of a seven-segment display, as shown in the picture above carefully. You’ll notice the carefully laid out LEDs. Notice how in both the configurations, one off the two ends of the LEDs is connected to a common line. This common line is either GND (left) or VCC (right). Let’s talk about that a bit. Your 7-segment display is possibly one of two types:
- Common cathode
- Common anode
What is a common anode seven-segment display?
For an LED to light up, the anode needs to connect with a power source. And the cathode needs to connect with the ground. That’s the basic working of an LED.
In a common anode display, all the eight LEDs have their anodes interconnected. This means that the power supply to all the LEDs will be via one common pin. But don’t we need to control each of these LEDs separately to display different characters? Correct!
Thus the individual control comes from the free multiple cathode pins. To switch on any of the eight LEDs, its cathode pin needs to be grounded. And since the LEDs retain their individual cathode pins, we can use them to control them.
All you need to remember is this: To light up a common anode seven-segment display, you have to ground/write a LOW output on the pins that need to be lit up.
What is the common cathode seven-segment display?
This type is just a reverse design of the common anode type. In a common cathode seven-segment display, all of the cathode pins are connected to a common ground (GND). Thus the individual control comes from the free multiple anode pins. To switch on any of the eight LEDs, its anode pin needs to be given power.
All you need to remember is this: To light up a common cathode seven-segment display, you have to write a HIGH output on the pins that need to be lit up.
Working principle of the seven-segment display
The LED segments are selected based on the decimal number. For example, if we want to display the number 8, we should select all of the LEDs a, b, c, d, e, f, g.
You can turn on the needed LED segments, depending on the number or alphabet you wish to display. You can select the LED segments according to the table shown below. To display any character from the table below, you just need to send the corresponding hex codes to the right pins.
A | B | C | D | E | F | G | DP | Numbers/ characters | HEX code |
1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0x3F |
0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0x06 |
1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 2 | 0x5B |
1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 3 | 0x4F |
0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 4 | 0x66 |
1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 5 | 0x6D |
1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 6 | 0x7D |
1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 7 | 0x07 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 8 | 0x7F |
1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 9 | 0x67 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | . | |
1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | A | 0x77 |
0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | b | 0x7C |
1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | C | 0x39 |
0 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | d | 0x5E |
1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | E | 0x79 |
1 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | F | 0x71 |
If you realize that there should be another table, kudos to you. This table turns on the LEDs by writing a ‘1’ to them. So this table works for the common cathode seven-segment display. For the common anode version, you need to invert the binary values of the table above. For the purpose of this Arduino tutorial, we will be using a common cathode.
What are the features of the 5161AS 7-segment display?
- Size: It is available in different sizes like 9.14mm, 14.20mm(Widely used), 20.40mm, 38.10mm, 57.0mm and 100mm.
- Emitting color: Seven-segment LEDs are available in several colors — for example, Blue, White, Yellow, Ultra-bright Red (Widely used), Green.
- Mode : Common-Cathode (CC) or Common-Anode(CA)
- Digit: It can represent only Single Digit.
- Operation voltage: 5V.
- Peak current: 70mA. It works well even in low current.
- Segment current: 30mA
Pin configuration of a standard seven-segment display
Check out what the different pins on your seven-segment display internally connect to. You need to make sure that you connect to the right pins to get accurate data on display.
Pin Number | Name | Description |
1 | E(digital pin) | It controls the left bottom LED |
2 | D(digital pin) | It controls the center bottom LED |
3 | Com(common pin) | You can connect it to the Ground/Vcc based on whether it is a common cathode or common anode. |
4 | C(digital pin) | It controls the right bottom LED |
5 | DP(decimal point) | It controls the decimal point LED |
6 | B(digital pin) | It controls the upper right LED |
7 | A(digital pin) | It controls the top center LED |
8 | Com(common pin) | You can connect it to the Ground/Vcc based on whether it is a common cathode or common anode. |
9 | F(digital pin) | Controls the upper left LED |
10 | G(digital pin) | Controls the middle LED |
Dimensions of a seven-segment display
This is information you might need when you design a compact PCB for your Arduino project.
What are some display alternatives to the seven-segment display?
There are other display modules apart from the seven-segment display. You can check them out and choose an appropriate device that fulfills the needs of your Arduino project.
- 16×2 LCD (Interfacing of a 16×2 LCD with the Arduino Uno)
- OLED display
- TFT LCD screen display
- Dot Matrix LED display
Components required for interfacing 7-segment display with Arduino
[geoip_detect2_hide_if country=”IN”]- Arduino Uno
- Breadboard.
- Jumper wires – as required.
- 5161AS common cathode 7-segment LED display-1
- Seven 330Ω resistors
- Shift register (optional)
- Decimal to BCD decoder IC (optional)
- Seven segment display array
- Arduino Uno
- Breadboard.
- Jumper wires – as required.
- 5161AS common cathode 7-segment LED display-1
- Seven 330Ω resistors
- Shift register (optional)
- Decimal to BCD decoder IC (optional)
- Seven segment display array
How to interface a 7-segment display with Arduino without using the driver IC?
Using this method, you can drive the 7-segment display directly using the Arduino Uno. There are advantages and disadvantages to connecting a seven-segment display directly with the Arduino Uno. An advantage is that it eliminates the need for a special driver and thus reduces the cost. On the other hand, a direct connection requires more pins. And pin estate a real concern for all engineers.
Circuit for interfacing a 7-segment display with Arduino without using any driver IC
- You must connect the resistors between the LED pins and the Arduino digital I/O pins. Tweaking the value of these resistors can help increase/decrease the brightness of the LEDs.
- You can connect the LED pins with any of the Arduino’s digital I/O pins.
- Knowing whether your segment is a common cathode or common anode type is essential. You can cross verify using a datasheet.
- In the case of the common cathode version, you should give a LOW signal to the drive pin for activating the corresponding segment.
- In the case of the common anode version, you should give a HIGH signal to the drive pin for activating the corresponding segment.
Code for interfacing the seven-segment display with an Arduino Uno without using the driver IC
Code for up-counting
#define A 6 #define B 7 #define C 8 #define D 9 #define E 10 #define F 11 #define G 12 int COUNT = 0; void setup() { for (int i = 2; i < 9; i++) { pinMode(i, OUTPUT); } } void loop() { for(int i=0;i<=9;i++) { switch (COUNT) { case 0: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, HIGH); digitalWrite(G, LOW); break; case 1: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); digitalWrite(E, LOW); digitalWrite(F, LOW); digitalWrite(G, LOW); break; case 2: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, LOW); digitalWrite(G, HIGH); break; case 3: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, LOW); digitalWrite(F, LOW); digitalWrite(G, HIGH); break; case 4: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); digitalWrite(E, LOW); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 5: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, LOW); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 6: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 7: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); digitalWrite(E, LOW); digitalWrite(F, LOW); digitalWrite(G, LOW); break; case 8: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 9: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, LOW); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; break; } delay(1000); }
}
Code for the down counting
#define A 2 #define B 3 #define C 4 #define D 5 #define E 6 #define F 7 #define G 8 void setup() { for (int i = 2; i < 9; i++) { pinMode(i, OUTPUT);// taking all pins from 2-8 as output } } void loop() { for(int i=9;i>=0;i--) { switch (i) { case 0: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, HIGH); digitalWrite(G, LOW); break; case 1: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); digitalWrite(E, LOW); digitalWrite(F, LOW); digitalWrite(G, LOW); break; case 2: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, LOW); digitalWrite(D, HIGH); digitalWrite(E, HIGH); digitalWrite(F, LOW); digitalWrite(G, HIGH); break; case 3: digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, LOW); digitalWrite(F, LOW); digitalWrite(G, HIGH); break; case 4: digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, HIGH); digitalWrite(D, LOW); digitalWrite(E, LOW); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 5: digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, HIGH); digitalWrite(D, HIGH); digitalWrite(E, LOW); digitalWrite(F, HIGH); digitalWrite(G, HIGH); break; case 6: digitalWrite(A, HIGH); digitalWrite(segB, LOW); digitalWrite(segC, HIGH); digitalWrite(segD, HIGH); digitalWrite(segE, HIGH); digitalWrite(segF, HIGH); digitalWrite(segG, HIGH); break; case 7:// when count value is 7 show”7” on disp digitalWrite(segA, HIGH); digitalWrite(segB, HIGH); digitalWrite(segC, HIGH); digitalWrite(segD, LOW); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, LOW); break; case 8:// when count value is 8 show”8” on disp digitalWrite(segA, HIGH); digitalWrite(segB, HIGH); digitalWrite(segC, HIGH); digitalWrite(segD, HIGH); digitalWrite(segE, HIGH); digitalWrite(segF, HIGH); digitalWrite(segG, HIGH); break; case 9:// when count value is 9 show”9” on disp digitalWrite(segA, HIGH); digitalWrite(segB, HIGH); digitalWrite(segC, HIGH); digitalWrite(segD, HIGH); digitalWrite(segE, LOW); digitalWrite(segF, HIGH); digitalWrite(segG, HIGH); break; break; } delay(1000); }}
How to Interface 7-segment display with Arduino using SN7446AN driver?
To reduce the number of pins, driver ICs are used for interfacing 7-segment display with Arduino. Let’s take a look at one of these configurations.
What is the SN7446AN driver?
It is a special decoder IC used specifically to convert binary-coded decimal to decimal numbers that can be displayed on a seven-segment display. BCD is a fast and efficient digital number system. We can interconvert BCD to decimal and vice versa. Only four pins are used instead of the eight we needed when we were connecting the display directly. This saves four valuable pins of your Arduino Uno. You can now use these extra pins to increase the functionality of your project.
DECIMAL | 8 | 4 | 2 | 1 | BCD |
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 1 | 1 |
2 | 0 | 0 | 1 | 0 | 2 |
3 | 0 | 0 | 1 | 1 | 3 |
4 | 0 | 1 | 0 | 0 | 4 |
5 | 0 | 1 | 0 | 1 | 5 |
6 | 0 | 1 | 1 | 0 | 6 |
7 | 0 | 1 | 1 | 1 | 7 |
8 | 1 | 0 | 0 | 0 | 8 |
9 | 1 | 0 | 0 | 1 | 9 |
10 | 1 | 0 | 1 | 0 | Invalid |
11 | 1 | 0 | 1 | 1 | Invalid |
12 | 1 | 1 | 0 | 0 | Invalid |
13 | 1 | 1 | 0 | 1 | Invalid |
Circuit for interfacing a 7-segment display with an Arduino Uno using the SN7446AN driver IC
Code for interfacing 7-segment display with Arduino Uno using SN7446AN driver
int bcd_pins[4]; /* array for driver IC pins*/ void bcd_control_pins(int a, int b, int c, int d) /* Assigns driver IC pins to Arduino board */ { bcd_pins[0] = a; bcd_pins[1] = b; bcd_pins[2] = c; bcd_pins[3] = d; } void displaydigits(int num) /* Function for displaying number from 0 to 9) */ { switch(num) { case 0: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], LOW); break; case 1: digitalWrite(bcd_pins[0], HIGH); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], LOW); break; case 2: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], HIGH); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], LOW); break; case 3: digitalWrite(bcd_pins[0], HIGH); digitalWrite(bcd_pins[1], HIGH); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], LOW); break; case 4: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], HIGH); digitalWrite(bcd_pins[3], LOW); break; case 5: digitalWrite(bcd_pins[0], HIGH); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], HIGH); digitalWrite(bcd_pins[3], LOW); break; case 6: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], HIGH); digitalWrite(bcd_pins[2], HIGH); digitalWrite(bcd_pins[3], LOW); break; case 7: digitalWrite(bcd_pins[0], HIGH); digitalWrite(bcd_pins[1], HIGH); digitalWrite(bcd_pins[2], HIGH); digitalWrite(bcd_pins[3], LOW); break; case 8: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], HIGH); break; case 9: digitalWrite(bcd_pins[0], HIGH); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], HIGH); break; default: digitalWrite(bcd_pins[0], LOW); digitalWrite(bcd_pins[1], LOW); digitalWrite(bcd_pins[2], LOW); digitalWrite(bcd_pins[3], LOW); break; } } void setup() { pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); bcd_control_pins(11,10,9,8); } void loop() { int i=0; for(i = 9; i>=0; i--) { displaydigit(i); delay(1000); } for(i = 0; i<=9; i++) { displaydigit(i); delay(1000); } }
How to Interface a 7-segment display with Arduino using the 74HC595 shift register?
We saw the working of shift registers in our digital electronics course. Essentially, it is a device that takes in data in a certain way (serially or parallelly) and outputs it in a certain way (serially or parallelly). Using a shift register to interface a seven-segment display with the Arduino, brings down the number of digital pins to three.
What is the 74HC595 shift register?
The 74HC595 is a shift register. It works based on the Serial IN Parallel OUT protocol. Hence, it receives serial data from the microcontroller. And sends outputs parallelly to the seven-segment display.
74HC595 pin configuration
- Pin 1-7 => output pins Q1-Q7
- Pin 15 => output pins Q0
- Pin 8 => GND
- Pin 9 => Output serial data Q7’
- Pin 10 => Master reset
- Pin 11 => SHCP (Shift Register Clock Input)
- Pin 12 => STCP (Storage Register Clock Input)
- Pin 13 => OE (Output Enable)
- Pin 14 => DS (Serial data input)
- Pin 16 => Vcc
Circuit for interfacing a 7-segment display with an Arduino Uno using the 74HC595 shift register
Make the connections according to the layout below.
Code for interfacing a 7-segment display with the Arduino Uno using the 74HC595 shift register
const int bit_clock_pin = 10; const int digit_clock_pin = 9; const int data_pin = 8; const byte digit_pattern[16] = { B00111111, // 0 B00000110, // 1 B01011011, // 2 B01001111, // 3 B01100110, // 4 B01101101, // 5 B01111101, // 6 B00000111, // 7 B01111111, // 8 B01101111, // 9 B01110111, // A B01111100, // b B00111001, // C B01011110, // d B01111001, // E B01110001 // F }; unsigned int counter = 0; void setup() { pinMode(data_pin, OUTPUT); pinMode(bit_clock_pin, OUTPUT); pinMode(digit_clock_pin, OUTPUT); } void update_one_digit(int data) { int i; byte pattern; pattern = digit_pattern[data]; digitalWrite(digit_clock_pin, LOW); shiftOut(data_pin, bit_clock_pin, MSBFIRST, ~pattern); digitalWrite(digit_clock_pin, HIGH); } void loop() { int i; unsigned int digit_base; counter++; digit_base = 16; update_one_digit(counter % digit_base); delay(500); }
How to interface a module of a 7-segment display module with Arduino?
Usually, Arduino shields, breakout boards, and modules are easy to interface. This 7-segment display module requires less amount of pins and wires compared to the individual 7-segment display. You can communicate with this module using SPI communication. (Related: Arduino communication protocols)
Pin configuration of the 7-segment display module?
CLK =>D3
DIO =>D2
Vcc =>3.3V
GNG=>GND
Circuit for interfacing a 7-segment display module with an Arduino Uno
Code interfacing a 7-segment display module with an Arduino Uno
It requires a special library named as TM1637Display.h. You can learn how to install a library to your Arduino IDE here.
#include <TM1637Display.h> const int CLK = 3; const int DIO = 2; int numCounter = 0; TM1637Display display(CLK, DIO); void setup() { display.setBrightness(0x0a); } void loop() { for(numCounter = 0; numCounter < 1000; numCounter++) { display.showNumberDec(numCounter); delay(1000); }
Functions used in this code are,
- setSegments() – Set the raw value of the segments of each digit.
- showNumberDec() – Display a decimal number.
- showNumberDecEx() – Display a decimal number with decimal points or colon.
- setBrightness() – Sets the brightness of the display.
What are some common errors, and how to troubleshoot them?
- Sometimes, the problem occurs while uploading the recent default firmware to the small-sized 7-segment display. In this case, use the older version of the firmware.
- If you have any issues while uploading the example codes, you could reset the firmware. Moreover, you can send a special command through serial port otherwise re-upload the firmware that contains FTDI. Ultimately, if that does not work, you can try a fresh reinstall of the Arduino bootloader.
- In addition to the above, sending fast commands by microcontroller also cause some problems. Hence, it causes the display to show random numbers. In this case, you need to use the sequence of commands to clear the display, to set the mode, brightness and adjusting the cursor point.
- It also occurs due to Arduino compile issues. In this case, you can store the byte into a definite variable.
What are the applications of a seven-segment display?
- It is useful in designing counters and displaying the counts.
- It is a micro-controller independent. Hence you can use it in small projects.
- Moreover, it has a bright illumination. Hence helps in light or dark condition.
- Also useful in digital clocks, calculators and wristwatches.
- Furthermore, useful in speedometers, odometers.
- Mainly it helps to display the data that are created by many devices.